简体   繁体   English

AWS EC2竞价型实例PHP在发出现场请求时添加标记

[英]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. 我希望能够在通过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. 通过在用户数据中向CLI添加标记请求,您也可以在创建实例标记后使用它。 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). 这在EC2实例上作为许多EC2默认AMI(默认情况下也安装了CLI)的引导脚本执行。

To do that (using a stock image): 要做到这一点(使用股票图像):

  1. Create an IAM role with permission to create tags on EC2. 创建具有在EC2上创建标记的权限的IAM 角色
  2. In your spot instance request, specify the role. 在您的spot实例请求中,指定角色。
  3. 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). 在您的spot实例请求用户数据中,包括create tags CLI命令(对于Linux - 如果您使用的是Windows,也可以使用powershell执行等效操作)。 You'll see that there is an inline command to get the instance ID from the EC2 metadata service: 您将看到有一个内联命令可以从EC2元数据服务获取实例ID:

     #!/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. 如果您使用CLI或SDK发出现场请求,则可能需要将上面的用户数据作为base64进行编码。 The AWS web console can do this for you. AWS Web控制台可以为您执行此操作。

That's it! 而已!

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

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