amazon-web-servicesaws-cloudformationvpc

Creating subnet per availability zone using cloudformation


I need to create subnet per availability zone in particular region using cloudformation.

For example if the region is Mumbai, it is with three availability zones and the CF template should create a public and a private subnet in each availability zone: 1a,1b and 1c. Is it really possible? I have done the same using terraform but have no idea how can I achieve this in CF.

It would be great if someone could help on this.

Thanks in advance.


Solution

  • Sadly, there are no loops in plain CloudFormation. Thus you can't create any constructs that would loop over AZs, get their IDs and create a pair of private-public subnets in each AZ.

    If you really want to keep everything in CloudFormation than you would have to look at custom resources or marcros.

    Both of them would require you to write your own lambda function that would use AWS API to get the number of AZs, their names and perform iteration to create the subnets.

    If you already are using terraform successful, maybe its worth considering to keep using it, as it has loops useful in your use-case.