Why are Availability Zone Names Inconsistent?

0

I'm having trouble creating a Terraform Plan that creates subnets. Specifically, I sometimes get errors that the Availability Zones don't exist.

Doing some troubleshooting I noticed really strange behavior in AWS when it comes to AZ name to AZ ID mappings, and just AZ names in general.

When I do this in the aws cli: aws ec2 describe-availability-zones --region us-west-1 I see the following: ZoneName: us-west-1b ZoneID: usw1-az3

ZoneName: us-west-1c ZoneID: usw1-az1

In the AWS GUI Console I'm logged in to two different accounts, but both are set to N. California. When I go to create a new subnet, in one account the two available AZs are: us-west-1b (usw1-az3) us-west-1c (usw1-az1)

But in the other account when I create a subnet the two AZs listed are: us-west-1a (usw1-az1) us-west-1c (usw1-az3)

I don't really care as long as the subnets get created in two diff AZs, but I don't know whether to use a & b, b & c, or a & c.

What gives?

AJCruz
asked 2 years ago677 views
1 Answer
0
Accepted Answer

Availability Zones represented by a particular letter can mean different AZs to different AWS accounts. For example for Account A us-west-1a can be different from what us-west-1a is for account B.

In Cloudformation, you can use

PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 0, !GetAZs '' ] 
      CidrBlock: !Ref PublicSubnet1CIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} Public Subnet (AZ1)

PublicSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 1, !GetAZs  '' ]
      CidrBlock: !Ref PublicSubnet2CIDR
      MapPublicIpOnLaunch: true
      Tags:
        - Key: Name
          Value: !Sub ${EnvironmentName} Public Subnet (AZ2)

Note how different array indexes are chosen for the two Public Subnets in the AvailabilityZone: !Select [ 0, !GetAZs '' ] statement in the Cloudformation template snippet above, thereby ensuring the two subnets are created in two different AZs

I see a similar concept being used in Terraform - https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones

# Declare the data source
data "aws_availability_zones" "available" {
  state = "available"
}

# e.g., Create subnets in the first two available availability zones

resource "aws_subnet" "primary" {
  availability_zone = data.aws_availability_zones.available.names[0]

  # ...
}

resource "aws_subnet" "secondary" {
  availability_zone = data.aws_availability_zones.available.names[1]

...

}

profile pictureAWS
EXPERT
answered 2 years ago
profile pictureAWS
EXPERT
Toni_S
reviewed 2 years 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.

Guidelines for Answering Questions