0

I am trying to get the Availability Zone ID out of either the AWS CLI or from boto3. However, despite the documentation showing it, the command only returns the AZ, not the id for the AZ. Am I missing a step or is this just bad documentation, etc?

aws ec2 describe-subnets --region us-east-1

{
        "VpcId": "vpc-054c741523f481755",
        "CidrBlock": "10.150.3.32/27",
        "MapPublicIpOnLaunch": false,
        "State": "available",
        "Ipv6CidrBlockAssociationSet": [],
        "AssignIpv6AddressOnCreation": false,
        "SubnetId": "subnet-0a36ed4643fb511d1",
        "AvailabilityZone": "us-east-1a",
        "DefaultForAz": false,
        "AvailableIpAddressCount": 27,
        "Tags": [
            {
                "Key": "aws:cloudformation:stack-id",
                "Value": "arn:aws:cloudformation:us-east-1:186940489315:stack/dantooine-a-elastic-subnets/dc3f7500-7b39-11ea-a67d-0e763951b664"
            },
            {
                "Key": "aws:cloudformation:stack-name",
                "Value": "dantooine-a-elastic-subnets"
            },
            {
                "Key": "Name",
                "Value": "dantooine-a-elastic-subnets-endpointSubnet"
            },
            {
                "Key": "aws:cloudformation:logical-id",
                "Value": "endpointSubnet"
            }
        ]
    }

The documentation shows:

{
"Subnets": [
    {
        "AvailabilityZone": "us-east-2c",
        "AvailabilityZoneId": "use2-az3",
        "AvailableIpAddressCount": 251,
        "CidrBlock": "10.0.2.0/24",
        "DefaultForAz": false,
        "MapPublicIpOnLaunch": false,
        "State": "available",
        "SubnetId": "subnet-0bb1c79de3EXAMPLE",
        "VpcId": "vpc-0ee975135dEXAMPLE",
        "OwnerId": "111122223333",
        "AssignIpv6AddressOnCreation": false,
        "Ipv6CidrBlockAssociationSet": [],
        "SubnetArn": "arn:aws:ec2:us-east-2:111122223333:subnet/subnet-0bb1c79de3EXAMPLE"
    },
Lyle Reger
  • 15
  • 3

3 Answers3

2

If you wish to view the Availability Zone IDs, use:

aws ec2 describe-availability-zones --region us-east-1

It will output:

{
    "AvailabilityZones": [
        {
            "State": "available",
            "OptInStatus": "opt-in-not-required",
            "Messages": [],
            "RegionName": "us-east-1",
            "ZoneName": "us-east-1a",
            "ZoneId": "use1-az1",
            "GroupName": "us-east-1",
            "NetworkBorderGroup": "us-east-1"
        },
...

You can then map this information to any subnets you have created.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
0

This works fine for me with both the awscli and boto3. For example:

import boto3

client = boto3.client('ec2')

subnets = client.describe_subnets()

for subnet in subnets['Subnets']:
    print(subnet['AvailabilityZone'], subnet['AvailabilityZoneId'])

Output is:

us-east-1b use1-az2
us-east-1e use1-az3
us-east-1d use1-az6
...

I think your installation of awscli and boto3 may be out of date.

jarmod
  • 71,565
  • 16
  • 115
  • 122
0

Here is an example for boto3 in Python:

import json
import boto3

def lambda_handler(event, context):

    ec2 = boto3.client('ec2', region_name="us-east-1")

    azs = ec2.describe_availability_zones()["AvailabilityZones"]
    for az in azs:
        print (az['ZoneName'], az['ZoneId'])

This is the output:

us-east-1a use1-az4
us-east-1b use1-az6
us-east-1c use1-az1
us-east-1d use1-az2
us-east-1e use1-az3
us-east-1f use1-az5
e.gad
  • 1,088
  • 19
  • 22