Ken Lee
Ken Lee

Reputation: 11

AWS Lambda Python for AMI Tagging after Creation

Good day to all,

I know this has been asked before, I have gone through most (if not all) of the results online, most are outdated and some wouldn't work. I am using an AWS Codepipeline to deploy some EC2 and adding steps to create the AMI before and after the deployments. The AMI creation part is all well and good but there's no tagging on the AMIs so it's hard to determine easily which one was pre-deploy which is post-deploy. Which is why I needed to add this tagging function in the python code.

Previously I tried this code but tagging not working: Serverless-AMI-Baker

It works on AMI creation, however the tagging function doesn't. It was supposed to add tags but there was none after the AMI was created.

This is the code I'm currently using to create AMI: Amiautomation

I have also found the same question here in stackoverflow but I don't understand the answer at all, I know it's a generic solution and needed some modification to that to suite what I'm using but this is where I'm stuck since I know jack about python: don't understand this potential solution

All the help you can provide is GREATLY APPRECIATED. Thank you!

update, i've tried TagSpecifications but not sure it's the right way to do it or not, anyhow, this method doesn't work. append TagSpecifications into existing code

I also tried putting it in the middle but does not work: enter image description here

Upvotes: 0

Views: 560

Answers (1)

omuthu
omuthu

Reputation: 6333

You can use 'TagSpecifications' parameter in create_image API to specify tags

tag_spec=[
    {
        'ResourceType': 'image',
        'Tags': [
            {
                'Key': 'string',
                'Value': 'string'
            },
        ]
    },
]

image = client.create_image(TagSpecifications=tag_spec, Description=description, InstanceId=instanceid, Name=name, NoReboot=True)

Reference: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.create_image

Upvotes: 1

Related Questions