2

我已经能够使用 PHP SDK 从我的所有 EC2 实例中检索完整的标签列表,但我正在努力将结果过滤到特定实例......

    // Collect instance information
    $sInstanceId = file_get_contents('http://169.254.169.254/latest/meta-data/instance-id'); // 'i-52da5b1f'
    $sAvailabilityZone = file_get_contents('http://169.254.169.254/latest/meta-data/placement/availability-zone'); // 'eu-west-1b'
    $sRegion = preg_replace('/^(.*)([0-9]{1})([a-zA-Z]{1})/', '$1$2', $sAvailabilityZone);

    use Aws\Common\Aws;
    use Aws\Ec2\Command\DescribeTags;
    use Aws\Common\Enum\Region;

    // Set up the global AWS factory
    $oAWS = Aws::factory(array(
            'key'    => CONST_AWS_ACCESS_KEY,
            'secret' => CONST_AWS_SECRET_KEY,
            'region' => $sRegion
    ));

    // Query EC2 for tags
    $oEC2Client = $oAWS->get('ec2');
    $oModel = $oEC2Client->describeTags()->toArray();

我尝试将对 describeTags 的调用更改为...

    $oModel = $oEC2Client->describeTags(array(
            "Filters" => array(
                    array("Name" => "resource-id", "Value" => $sInstanceId)
            )
    ))->toArray();

但这似乎没有什么区别。有人可以帮我解释一下吗?

4

1 回答 1

5

Ec2Client.describeTags的API 文档显示它Value实际上应该是Values并且应该是一个数组。尝试以下操作:

$oModel = $oEC2Client->describeTags(array(
    "Filters" => array(
        array("Name" => "resource-id", "Values" => array($sInstanceId))
    )
))->toArray();
于 2013-06-28T00:01:33.807 回答