Dynamic created resources in CloudFormation template

0

I am learning CloudFormation with some basic resources : VPC, subnet, EC2,.. and have some questions:

  1. How to define to create 1, 2 or 3 EC2 for each time creates stacks with only one template. Each EC2 will have differences properties (EBS size, AMI,...)?
  2. How to define Yes/No resources. For example, if I choose Yes in Parameter, this resources will be created. And if I choose No, this resources will not be created. Thank you so much for your help.
asked 6 months ago261 views
2 Answers
0
  1. For complex requirements like this - where you might want to loop through a set of resources and assign different properties to each one you might want to look at CDK. For simpler requirements you might look at the ForEach intrinsic function.
  2. Have a look at conditionally creating resources.
profile pictureAWS
EXPERT
answered 6 months ago
profile pictureAWS
EXPERT
reviewed 6 months ago
0

you can define your EC2 properties as mappings in the beginning of your cloudformation template, something like this:

Mappings: AMI_Mappings: EC2Name: ec21: ami21 ec22: ami22 ec23: ami23 ec24: ami24 ec25: ami25

and use Fn::ForEach intrinsic function to loop through these properties and define the EC2 resource inside the loop.

answered 6 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions