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
질문됨 2년 전706회 조회
1개 답변
0
수락된 답변

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
전문가
답변함 2년 전
profile pictureAWS
전문가
Toni_S
검토됨 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠