0
votes

I have a usecase where the cloud formation has to identify AMI-ID based on tags (passed as inputs to cloud formation template) and likewise create an ec2 instance.

Can I run a script to identify (aws ec2 describe-images --filters "Name=tag:Name,Values=value1" --query 'Images[*].{ID:ImageId}' --output text) AMI-ID via cloud formation and pass it for ec2 instance creation..

Any thoughts or example on how to do this ?

2

2 Answers

1
votes

The suggested method by Amazon is provided here: Walkthrough: Looking Up Amazon Machine Image IDs

Summary: you create a custom resource in your CloudFormation template that queries a Lambda function. The Lambda function runs the AMI querying code and returns that to the CloudFormation template.

An alternative method is provided here: Integrating AWS CloudFormation with AWS Systems Manager Parameter Store

In this second method, you use CloudFormation to lookup values in AWS Parameter Store. How you decide what values to put into Parameter store is a different issue.

0
votes

You cannot run the script as part of your CloudFormation template.

You will need to execute the command before creating the CloudFormation stack to determine the AMI ID. Then pass the final AMI ID into the CloudFormation stack by way of an input parameter.

So it will be a 2-step process. But you can create a script to do it.