John John - 2 months ago 11
PHP Question

aws SDK 3, PHP, runInstance does not create the supplied Tags

$options = array(
'UserData' => base64_encode('test'),
'SecurityGroupIds' => [AWS_REGIONS[$region]['security_group']],
'InstanceType' => AWS_REGIONS[$region]['instance_type'],
'ImageId' => AWS_REGIONS[$region]['ami'],
'MaxCount' => $to_launch,
'MinCount' => 1,
//'EbsOptimized' => true,
'SubnetId' => AWS_REGIONS[$region]['subnet_id'],
'Tags' => [['Key' => 'task', 'Value' => $task],['Key' => 'Name', 'Value' => $task]],
'InstanceInitiatedShutdownBehavior' => 'terminate'
);
$response = $client->runInstances($options);


I am using the "latest" Ec2Client

It launches fine but the Tags are completely ignored.

I suspect an error within the EC2 API but I am not that experienced.

Maybe someone with experience can help me out ?

Answer

This is because Ec2Client::runInstances does not have tags option http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-ec2-2015-10-01.html#runinstances

You would need to make a separate call to tag newly created instance(s) using Ec2Client::createTags:

$result = $client->createTags(array(
    'DryRun' => true || false,
    // Resources is required
    'Resources' => array('string', ... ),
    // Tags is required
    'Tags' => array(
        array(
            'Key' => 'string',
            'Value' => 'string',
        ),
        // ... repeated
    ),
));

Read more here: http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-ec2-2015-10-01.html#createtags

Comments