AWS EC2 Spot Instance PHP Adds Tag When Creating Spot Request

I would like to be able to include the tag when creating a spot request via PHP. When instantiated on demand, you can instantiate and then use it to produce the following:

$ec2->create_tags($instance_id, array(
      array('Key' => 'Name', 'Value' => 'MyTestMachine'),


However, when issuing a dot claim, the instance does not start immediately, so you need to create a watcher tag to handle this ... if you cannot add the tag in the request phase. I haven't found any documentation to show how it would look or how it looks like, does it exist?


source to share

2 answers

the answer is that you cannot assign a tag until an instance is created. To tag this, I used a listener daemon to watch for new instances and tag them after starting them.



For future people looking for a solution without a listener:

You can also have your own instance after creating it by including the CLI tag request in the user data. This is executed in the EC2 instance as a script at boot for many of the default EC2 AMIs (which also have the CLI installed).

To do this (using a stock image):

  • Create an IAM role with permission to create tags on EC2.
  • Specify the role in your instance request.
  • In your object instance request data, include the CLI command to create tags (for Linux - you can also do the equivalent with powershell if you are using windows). You will see that there is a built-in command to get the Instance ID from EC2 Metadata Service:

    aws ec2 create-tags --resources `wget -q -O -` --tags Key=somekey1,Value=somevalue1 Key=somekey2,Value=somevalue2

You may need to enode the user data above as base64 if you are using the CLI or SDK to make a slick request. The AWS Web Console can do this for you, otherwise you can transfer custom data like this:

user="$(cat /scripts/ | base64 -w 0)"

aws ec2 request-spot-instances \"UserData\": \"$user\",\"InstanceType\": \"m1.small\"}"


What is it!



All Articles