I lifted a script which I’m using to create regular AMIs of my production servers and clear out old AMIs .
The script works well but I would like it to use the friendly name instead of the instance ID. Currently the line in question looks like this:
AMIid = ec.create_image(InstanceId=instance[‘InstanceId’], Name="Lambda - " + instance[‘InstanceId’] + " from " + create_fmt, Description="Lambda created AMI of instance " + instance[‘InstanceId’] + " from " + create_fmt, NoReboot=True, DryRun=False)
This procudes an AMI described thus: Lambda - i-f4f8f869 from 2017-06-28 so if I’m launching an AMI I need to cross-reference the instanceID against the Name tag.
I’m assuming I need to swap out 'InstanceID for something like Name but that doesn’t work.
When I get-help on describe-instance I can’t find a lable for ‘name’ so I’m thinking maybe it can’t be done.
Any help would be appreciated as I’m a noob with this.
Name is usually a Tag and you should probably use Tags anyways so you can filter however you want, now and in the future. You can then query by whatever tags you want.
create_image will only take an InstanceId as a way to specify an instance, can’t use the instance’s name directly.
The Name field is actually a tag, so you’ll see it under Tags in the response. It looks like this script is written in Python; here’s the documentation on the describe_instances call:
Sector95 is right here. You are going to want to use describe_instances first with your instance[‘InstanceId’] to get the Name tag of the instance in question and then maybe add that to your dict as instance[‘Name’] to use later in the create_image call.