Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列出子网中的可用性区域_Python_Amazon Web Services_Amazon Ec2_Boto3 - Fatal编程技术网

Python 列出子网中的可用性区域

Python 列出子网中的可用性区域,python,amazon-web-services,amazon-ec2,boto3,Python,Amazon Web Services,Amazon Ec2,Boto3,我有一个脚本,可以创建AWS实例并将它们放在子网和可用性区域中 但可用性区域必须对应于子网,否则会出现一个错误,显示: An error occurred (InvalidParameterValue) when calling the RunInstances operation: Value (us-east-1f) for parameter availabilityZone is invalid. Subnet 'subnet-87bd70ca' is in the availabil

我有一个脚本,可以创建AWS实例并将它们放在子网和可用性区域中

但可用性区域必须对应于子网,否则会出现一个错误,显示:

 An error occurred (InvalidParameterValue) when calling the RunInstances operation: Value (us-east-1f) for parameter availabilityZone is invalid. Subnet 'subnet-87bd70ca' is in the availability zone us-east-1c
这是我目前掌握的代码:

import boto3
import objectpath
subnet_id = input("Enter the subnet id: ")
public_ip = input("Create a public ip (True|False): ")
private_ip = input("Enter the private IP address: ")
availability_zones = ec2_client.describe_availability_zones()
tree = objectpath.Tree(availability_zones)
availability_zones = set(tree.execute('$..ZoneName'))
availability_zones = list(availability_zones)
availability_zones = str(availability_zones).replace('[','').replace(']','').replace('\'','')
availability_zone = input("Enter the availability zone: ")
        instances = ec2_resource.create_instances(
        ImageId=image_id,
        InstanceType=instance_type,
        KeyName=key_name,
        MaxCount=max_count,
        MinCount=1,
        DryRun=False,
        DisableApiTermination=True,
        EbsOptimized=False,
        Placement={
            'AvailabilityZone': availability_zone,
            'Tenancy': tenancy,
        },
        InstanceInitiatedShutdownBehavior='stop',
        NetworkInterfaces=[
            {
                'AssociatePublicIpAddress': public_ip,
                'DeleteOnTermination': True,
                'DeviceIndex': 0,
                'PrivateIpAddress': private_ip,
                'SubnetId': subnet_id,
                'Groups': [
                    sg_id
                ]
            }
        ]
        )

有没有办法找出子网所属的可用区?

虽然我没有确切的答案,但我想您可以在
boto
中找到函数,该函数返回与
aws ec2 description subnets
相似的结果,然后从中创建验证函数

你们应该看看地形,这是我重新发明的轮子,但不需要


HTH

如果指定子网,则无需指定可用性区域


这是因为子网仅存在于一个可用区域中。因此,提供子网也会告诉EC2要使用哪个AZ。

但是在一个可用性区域中可以有多个子网。我发现这页上的图表很有用。另一个相关stackoverflow帖子的回答解释了故障原因。