AWS
CloudFormation
Subnet
Availability Zones
Networking

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.

Practice system design

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.

yaml
AvailabilityZone: !Select
  - 0
  - !GetAZs ''

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.

yaml
1Resources:
2  PublicSubnetA:
3    Type: AWS::EC2::Subnet
4    Properties:
5      VpcId: !Ref Vpc
6      CidrBlock: 10.0.1.0/24
7      AvailabilityZone: !Select [0, !GetAZs '']
8
9  PublicSubnetB:
10    Type: AWS::EC2::Subnet
11    Properties:
12      VpcId: !Ref Vpc
13      CidrBlock: 10.0.2.0/24
14      AvailabilityZone: !Select [1, !GetAZs '']

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.

yaml
1Outputs:
2  ZoneA:
3    Value: !Select [0, !GetAZs '']
4  ZoneB:
5    Value: !Select [1, !GetAZs '']

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::GetAZs as 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::GetAZs in CloudFormation to retrieve Availability Zones for the current region.
  • Combine it with !Select to 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.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design