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
gefragt vor 2 Jahren706 Aufrufe
1 Antwort
0
Akzeptierte Antwort

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
EXPERTE
beantwortet vor 2 Jahren
profile pictureAWS
EXPERTE
Toni_S
überprüft vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen