0
votes

I have a requirement to launch multiple EC2 instances in the Tokyo region, based on the number of AMIs owned by our account in that same region. The AMIs are backed-up daily from another region.

What this CloudFormation needs to achieve is:

  • Retrieve a list of AMIs created today
  • Attempt to launch each of them in the same region

For example, if today there are 10 different AMIs created in the Tokyo region, then CloudFormation will then create 10 EC2 instances based on these 10 AMIs.

I have looked at some examples at Walkthrough: Looking Up Amazon Machine Image IDs - AWS CloudFormation but found the code does not suit the requirement.

I already have the Lambda function retrieve-today-ami.py, the challenge is to include them in the CF template found in Walkthrough: Looking Up Amazon Machine Image IDs - AWS CloudFormation

2
What is your actual high-level goal? Is it to create a high-availability/redundant infrastructure? How are the instances in the original region created? Could you reuse that method, rather than just copying AMIs? What's on the AMIs -- data should be stored in a database, so how do you replicate the data? Think about the high-level goal before figuring out how to simply launch EC2 instances. - John Rotenstein

2 Answers

0
votes

Normally, CloudFormation is used to launch pre-defined infrastructure. Your requirement to launch a variable number of instances with information that changes for each instance every day, does not match the model for using CloudFormation.

Based on your use-case, I would recommend writing a script to perform the operation you want.

For example, a Python scripts that lists the AMIs, identifies the ones you want to use, then launches EC2 instances using those AMIs.

0
votes

You might be able to achieve this by using a Lambda-backed custom resource to fetch the names of the AMIs. Then, the outputs of your custom resource could be used in the EC2 stanzas in the template. You could have the one template defining the Lambda export the values and import them on your EC2 templates.