I’m new to CloudFormation and want to create a template using YAML. I need to figure out is there any way we can create multiple VPCs using UserInput.
As of now, I’ve used the following code:
Parameters:
EnvironmentName:
Description: An environment name that is prefixed to resource names
Type: String
vpcCIDR1:
Description: Please enter the IP range (CIDR notation) for this VPC
Type: String
Default: 10.3.0.0/16
vpcCIDR2:
Description: Please enter the IP range (CIDR notation) for this VPC
Type: String
Default: 10.4.0.0/16
Resources:
VPC1:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref vpcCIDR1
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: !Ref EnvironmentName
VPC2:
Type: AWS::EC2::VPC
Properties:
CidrBlock: !Ref vpcCIDR2
EnableDnsSupport: true
EnableDnsHostnames: true
Tags:
- Key: Name
Value: !Ref EnvironmentName
Instead of writing the same code again, I need the code to take user input for the VPCs count and create the VPCs according to the user input.
I’ve found the Count but when I use !Ref to pass parameter it is not working, it only works when I pass Count: 2 or any numeric value.
3
Answers
Yes, you can do that if you develop your own macro. There are no loops nor any complex processing of user input in CloudFormation. But with macro, you can program any logic you want. Alternatively, you could also use custom resources, if you don’t want macros.
What about using a rudimentary bash script like this (I haven’t fully tested the deployment and you’ll need to modify your file name etc), but something like this would be a starting point:
Please note that CloudFormation doesn’t has any function to create multiple resources using loops. CloudFormation has no loops.
You can however declare your resources and you can use
Condition
in CloudFormation Template along withIntrinsic Conditional Functions
. This is the way people have been doing.You can read more about using
Condition
in docs.You can read more about using
Intrinsic Conditional Functions
in docs.