Fn::GetAZs for Availability Zones
What you'll learn: How to dynamically retrieve the list of availability zones in an AWS region using the Fn::GetAZs intrinsic function.
What Is Fn::GetAZs?
Fn::GetAZs is a CloudFormation intrinsic function that returns an array of availability zones for a specified AWS region. Instead of hardcoding AZ names like us-east-1a or us-east-1b in your template, this function fetches them automatically at stack creation time.
Think of it like asking AWS, "What availability zones can I use here?" rather than memorizing and typing them yourself.
Why Use Fn::GetAZs?
When you deploy infrastructure across multiple availability zones for high availability, you want your template to work in any region without modification. Fn::GetAZs makes your templates portable and region-agnostic.
Syntax
In YAML format:
!GetAZs region
Or using the full function syntax:
Fn::GetAZs: !Ref AWS::Region
The region parameter can be:
- An empty string
""to use the current stack's region !Ref AWS::Region(a pseudo parameter) to explicitly reference the current region- A specific region string like
us-west-2
Common Usage Pattern
You'll typically combine Fn::GetAZs with Fn::Select to pick specific availability zones from the list. Since Fn::GetAZs returns an array, you can select the first AZ with index 0, the second with index 1, and so on.
AvailabilityZone: !Select [0, !GetAZs '']
This selects the first availability zone in the current region.
Key Takeaway: Fn::GetAZs dynamically retrieves availability zones for a region, making your CloudFormation templates portable and eliminating hardcoded AZ names.