AWS CLI 或 boto3:尝试获取可用性区域 ID?

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

我正在尝试从 AWS CLI 或 boto3 中获取可用区 ID。但是,尽管文档显示了它,但该命令仅 returns AZ,而不是 AZ 的 id。我错过了一个步骤还是这只是糟糕的文档等?

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

文档显示:

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

这对我来说适用于 awscli 和 boto3。例如:

import boto3

client = boto3.client('ec2')

subnets = client.describe_subnets()

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

输出为:

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

我认为您安装的 awscli 和 boto3 可能已过时。

如果您希望查看可用区 ID,请使用:

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

它将输出:

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

然后您可以将此信息映射到您创建的任何子网。