Python 列出子网中的可用性区域
我有一个脚本,可以创建AWS实例并将它们放在子网和可用性区域中 但可用性区域必须对应于子网,否则会出现一个错误,显示: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
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帖子的回答解释了故障原因。