【问题标题】:List availability zones in a subnet python boto3列出子网python boto3中的可用区
【发布时间】:2020-01-21 08:41:37
【问题描述】:

我有一个创建 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
                ]
            }
        ]
        )

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

【问题讨论】:

    标签: python amazon-web-services amazon-ec2 boto3


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

    虽然我没有确切的答案,但我认为您可以在 boto 中找到返回与 aws ec2 describe-subnets 相似结果的函数,然后从中创建一个验证函数。

    您应该研究 terraform,这是我重新发明的轮子,但不需要。

    HTH

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 2021-11-25
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多