amazon-web-servicescommand-line-interfaceboto3zoneavailability

AWS CLI or boto3: Trying to get the availability-zone id?


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"
    },

Solution

  • 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.