All i am struggling to add Name tags for my newly created EC2 instance,
Is there way in AWS to identify the last created instance?
reservations = ec2.get_all_instances()
instance = reservations[0].instances[0]
create_tags([instance.id], {"Name": tag})
This is isn't setting tags for recently deployed instance.
To name a newly created instance, you could add the name tag immediately after creating the vm:
import boto.ec2
conn = boto.ec2.connect_to_region("eu-west-1",aws_access_key_id='key',aws_secret_access_key='sectret')
reservations = conn.run_instances("ami-a10897d6", min_count=1, max_count=1, key_name="key", security_group_ids=["sg-123"], instance_type="t2.micro", subnet_id="subnet-123")
instance = reservations.instances[0]
conn.create_tags([instance.id], {"Name":"foo"})
Alternatively, if you know excactly the start time, you can use the filters
parameter with *
as a wildcard. A more dynamic example may be:
from datetime import datetime
now = datetime.utcnow()
filter_now = "%s*" % (now.strftime("%Y-%m-%dT%H:%M"))
reservations = conn.get_all_instances(filters={"launch_time":filter_now})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.