Is there a way for cloudformation to query available zones for subnet creation?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Yes. In plain CloudFormation, the usual way to get Availability Zones for subnet creation is the intrinsic function Fn::GetAZs. You do not need to hardcode zone names for the current region, and in most templates you should not.
Use Fn::GetAZs for the Current Region
Fn::GetAZs returns the Availability Zones for a region. If you pass an empty string, CloudFormation uses the region where the stack is being created.
This selects the first available zone from the list for the current region.
Example: Two Subnets in Two Zones
A common pattern is to create one subnet in the first zone and another in the second zone.
This is the standard answer for region-aware subnet placement in CloudFormation.
Why Hardcoding Zone Names Is Risky
Zone names such as us-east-1a are not guaranteed to map to the same physical zone across AWS accounts. That means hardcoding zone names can make templates less portable than they appear.
Using Fn::GetAZs is better because it lets CloudFormation resolve zones in the current account and region context.
When Fn::GetAZs Is Not Enough
Fn::GetAZs gives you the zone list, but it does not let you apply custom filtering logic such as:
- choose only opt-in zones with a certain property
- skip zones based on business rules
- query runtime conditions outside standard CloudFormation logic
If you need that level of selection, you usually move to a custom resource or generate the template input values elsewhere before deployment.
Parameterize the Count Carefully
Some templates assume a fixed number of zones and then blindly select indices 0, 1, and 2. That can fail in regions or partitions with different zone availability. If you need a variable number of subnets, pair your design with mappings, conditions, or higher-level tooling that knows how many zones you expect.
The simple pattern is fine for two-zone or three-zone setups when you control the target region assumptions.
Region-Specific Behavior Still Matters
Even though Fn::GetAZs makes the template region-aware, network design choices are still region-specific. CIDR planning, route tables, NAT design, and high-availability goals all depend on how many zones you actually want to span.
Fn::GetAZs solves discovery. It does not solve architecture.
Export the Chosen Zones When Useful
If other stacks or teams need to know which zones were selected, you can output them explicitly. That makes multi-stack networking setups easier to reason about and reduces guesswork during troubleshooting.
Common Pitfalls
- Hardcoding zone names makes templates less portable across accounts and regions.
- Assuming the zone list order has business meaning can create fragile infrastructure logic.
- Selecting more AZ indices than a region effectively exposes causes stack failures.
- Treating
Fn::GetAZsas a full query language leads to disappointment. It returns zones, not arbitrary filtered metadata. - Forgetting that multi-AZ subnet design also requires route, NAT, and failure-domain planning misses the bigger networking problem.
Summary
- Use
Fn::GetAZsin CloudFormation to retrieve Availability Zones for the current region. - Combine it with
!Selectto assign subnets to specific zone positions. - Avoid hardcoded zone names when portability matters.
- Use custom resources only when you need filtering logic beyond CloudFormation’s built-in capabilities.
- Zone discovery is only one part of designing reliable multi-subnet infrastructure.
Related reading
- Is there a way to export an AWS CLI Profile to Environment Variables?
- Is there a way to generate the AWS Console URLs for CloudWatch Log Group filters?
- Is there a way to have index.html functionality with content hosted on S3?
- Is there a way to list all resources in AWS
- Is there a way to add arbitrary records to kube-dns?
- Is there a way to broadcast to all clients except sender in python?
- Is there a way to manually set an ElasticSearch document id when inserting via AWS Kinesis Firehose?
- Is there a way to nuke all AWS resources in an AWS account?
System design interview problems on this topic
- Design Dropbox: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough
- Design Youtube or Netflix: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.