简体   繁体   English

Amazon AWS SDK PHP 2-按实例过滤标签?

[英]Amazon AWS SDK PHP 2 - Filter tags by instance?

I have been able to retrieve a full list of tags from all my EC2 instances using the PHP SDK but I'm struggling to filter the results down to a particular instance... 我已经能够使用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();

I've tried changing the call to describeTags to... 我尝试将对describeTags的调用更改为...

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

But that seems to make no difference. 但这似乎没有什么区别。 Could someone shed some light on this for me please? 有人可以帮我一下吗?

The API docs for Ec2Client.describeTags show that Value should actually be Values and should be an array. Ec2Client.describeTagsAPI文档显示Value实际上应该是Values并且应该是一个数组。 Try the following: 请尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM