AWS EC2 Spot Instance PHP add tag when making spot request

I'd like to be able to include a tag when making a spot request via PHP. When creating on-demand instances, you can create the instance, then use it's instance to issue the following:

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

However, when issuing a spot bid, the instance isn't started right away, so you'd have to create a watcher tag to deal with this...unless you can add a tag in the request phase. I haven't found any documentation to show how this would go or look like, does it exist?


the answer is that you cannot assign a tag until the instance is actually created. In order to tag this I have used a listener daemon to watch new instances and tag them once they've started.


For future people looking for a solution to this without a listener:

You could also have the instance tag itself once its created, by including a tag request to the CLI in the user-data. This is executed on the EC2 instance as a script on boot for many of the EC2 default AMIs (which have the CLI installed by default, too).

To do that (using a stock image):

  • Create an IAM role with permission to create tags on EC2.
  • In your spot instance request, specify the role.
  • In your spot instance request user-data, include the create tags CLI command (for Linux - you could also do the equivalent with powershell, if you're using windows). You'll see that there is an inline command to get the instance ID from the EC2 metadata service:

    #!/bin/bash
    aws ec2 create-tags --resources `wget -q -O - http://169.254.169.254/latest/meta-data/instance-id` --tags Key=somekey1,Value=somevalue1 Key=somekey2,Value=somevalue2
    
  • You may need to enode the user-data above as base64, if you're using the CLI or an SDK to make the spot request. The AWS web console can do this for you.

    That's it!

    链接地址: http://www.djcxy.com/p/67162.html

    上一篇: 亚马逊网络服务

    下一篇: AWS EC2竞价型实例PHP在发出点击请求时添加标签