简体   繁体   中英

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...

    // 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...

    $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. Try the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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