TravelingLex
TravelingLex

Reputation: 477

Data availability zones skipping AZ

I am trying to create subnets per AZ per region in AWS. I'm able to read the aws_availability_zones data source but when I iterate through the list it returns us-west-1a and us-west-1c (which doesn't exist).

data "aws_availability_zones" "available" {}

resource "aws_subnet" "example" {
  count = length(data.aws_availability_zones.all.names)

  vpc_id                  = aws_vpc.main.id
  cidr_block              = cidrsubnet(var.cidr, 8, count.index)
  availability_zone       = element(data.aws_availability_zones.all.names, count.index)
  map_public_ip_on_launch = true
}

I expect it to return us-west-1a and us-west-1b.

It currently returns us-west-1a and us-west-1c.

Upvotes: 1

Views: 767

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56997

Availability zones names are randomised:

To ensure that resources are distributed across the Availability Zones for a Region, we independently map Availability Zones to names for each account. For example, the Availability Zone us-east-1a for your AWS account might not have the same location as us-east-1a for another AWS account. For more information, see Regions and Availability Zones in the Amazon EC2 User Guide.

To identify the location of your resources relative to your accounts, you must use the AZ ID, which is a unique and consistent identifier for an Availability Zone. For example, use1-az1 is an AZ ID for the us-east-1 Region and it is the same location in every AWS account.

us-west-1 is a slightly weird region that has 2 availability zones: usw1-az1 and usw1-az3 and these are given randomly a, b or c per account.

aws ec2 describe-availability-zones --region us-west-1
{
    "AvailabilityZones": [
        {
            "State": "available", 
            "ZoneName": "us-west-1a", 
            "Messages": [], 
            "ZoneId": "usw1-az3", 
            "RegionName": "us-west-1"
        }, 
        {
            "State": "available", 
            "ZoneName": "us-west-1b", 
            "Messages": [], 
            "ZoneId": "usw1-az1", 
            "RegionName": "us-west-1"
        }
    ]
}

Ran from another AWS account:

aws ec2 describe-availability-zones --region us-west-1
{
    "AvailabilityZones": [
        {
            "State": "available", 
            "ZoneName": "us-west-1b", 
            "Messages": [], 
            "ZoneId": "usw1-az3", 
            "RegionName": "us-west-1"
        }, 
        {
            "State": "available", 
            "ZoneName": "us-west-1c", 
            "Messages": [], 
            "ZoneId": "usw1-az1", 
            "RegionName": "us-west-1"
        }
    ]
}

I don't know what happened to usw1-az2 but it's not a valid AZ anymore and looking at an old gist of mine it hasn't been for a long time.

Upvotes: 2

Related Questions