当价格合适时,我有几个EC2竞价请求可以启动EC2实例。 我希望得到的实例使用Name和Role标签进行标记,以便我的configurationpipe理工具知道它是什么types的机器。
更新 :
我采取了超安东尼的build议并且实施了它 – 因为轮询将是容易出错和资源密集的我添加一个启动脚本到我的AMI当实例启动时更新标记,以下是我采取的步骤:
pip install boto
您正在启动的实例必须有一些方法来访问有关现场请求的信息。 使用IAMangular色或者使访问键可用于您的实例。 我附加到我使用的IAMangular色的策略是:
{ "Statement": [ { "Action": [ "ec2:CreateTags", "ec2:DescribeTags", "ec2:DescribeInstances" ], "Effect": "Allow", "Resource": [ "*" ], "Sid": "Stmt1432737176000" } ], "Version": "2012-10-17" }
def get_tags_from_spot_request(): instance_id = boto.utils.get_instance_identity()['document']['instanceId'] region = boto.utils.get_instance_identity()['document']['region'] conn = boto.ec2.connect_to_region(region) inst = boto.ec2.instance.Instance(connection=conn) inst.id = instance_id inst.update() spot_id = inst.spot_instance_request_id tags = conn.get_all_tags(filters={'resource-type': 'spot-instances-request', 'resource-id': spot_id}) for tag in tags: inst.add_tag(tag.name, tag.value)
竞价实例请求是一种EC2资源。 AWS文档注意到可以对这种types的资源进行标记 ,但结果标记不会转移到实际的实例上:
您为竞价型实例请求创build的标签仅适用于请求。 这些标签不会自动添加到竞价型服务启动以实现请求的竞价型实例。 当您创build竞价型实例请求或在竞价型实例启动之后,您必须自己将标签添加到竞价型实例。
所以你需要在实例启动后添加标签。 你在这里有一些select:
另一种可能性是使用Ansible作为你的configurationpipe理工具。 在ec2模块中,您可以启动特定和正常生命周期实例,您可以添加“instance_tags”属性来创build标签。 一个简单的手册将是:
- name: Provision Spot Instance hosts: localhost connection: local gather_facts: False tasks: - name: Launch the new Spot Instance local_action: module: ec2 spot_price: 0.02 group: testSG instance_type: m3.medium image: ami-12345 wait: true instance_tags: Name: TagValueForName Foo: TagValueForFoo region: us-east-1 keypair: mykeypair
有趣的是,我的抱怨是,它只标记实例,不标记现场请求,与您的问题相反。
您现在可以在创buildSpot Fleet请求时为您的SpotFleetLaunchSpecification提供标签,并且这些标签将自动应用于该队列中的新实例。
http://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_SpotFleetLaunchSpecification.html