Skip to content

Fn::GetAZs returns fewer AZs than expected

0

The below Cloudformation fails for certain regions and succeeds in others.

Resources:
  SourceSQLVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.150.0.0/16
  SourceSubnet3:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref SourceSQLVPC
      CidrBlock: 10.150.15.0/24
      AvailabilityZone:
        Fn::Select:
        - 2
        - Fn::GetAZs: "us-east-1"
2 Answers
1

The issue you're experiencing with Fn::GetAZs returning fewer Availability Zones (AZs) than expected in certain regions is a known behavior in AWS CloudFormation. This function doesn't always return all available AZs in a region, but rather returns a subset based on your account and region.

In some cases, especially for newer AWS accounts or in certain regions, Fn::GetAZs might return fewer than three AZs, causing the template to fail when trying to select the third AZ (index 2).

To address this, you have a few options:

  1. Use a parameter to specify the AZ explicitly, allowing more control over AZ selection.

  2. Implement error handling in your template to account for cases where fewer than three AZs are available.

  3. Use the AWS CLI or SDK to retrieve all available AZs in the region before creating the stack, and then pass these as parameters to your CloudFormation template.

  4. For the specific case of us-east-1, you could hardcode the AZ names, as this region typically has six AZs (a through f). However, this approach is less flexible and not recommended for portable templates.

  5. Create default subnets in the desired AZs if they don't already exist. This can sometimes increase the number of AZs returned by Fn::GetAZs.

Remember that the availability of AZs can vary based on your AWS account, the specific region, and potentially other factors. It's always a good practice to design your templates to be flexible and handle varying numbers of available AZs.
Sources
can not run this cloudform at my account in region us-east-1 | AWS re:Post
Datazone CloudFormation Region Enablement Error In Env Blueprint Configuration | AWS re:Post
Cloudformation: setting Availability Zone in target group creation | AWS re:Post

answered a year ago

EXPERT

reviewed a year ago

0
Accepted Answer

Fn:getAZs only returns AZs that have a default subnet in your default VPC. You might need to create default subnets in additional AZs.

answered a year ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.