boto3 vpc_exists服务员在VPC存在之前返回?

时间:2018-04-18 09:49:10

标签: python python-3.x amazon-web-services amazon-ec2 boto3

在创建VPC标签后尝试设置VPC标签时出现以下错误(一次尝试几次。大部分时间都有效,但并非总是如此):

botocore.exceptions.ClientError: An error occurred (InvalidVpcID.NotFound) when calling the CreateTags operation: The vpc ID 'vpc-4240de24' does not exist

我事后检查过,VPC vpc-4240de24确实存在,所以CreateTags太早被调用了。

以下方法发生错误:

def create_vpc(self, region, vpc_name):
    """create VPC in region and attach tags (threaded)"""
    ec2_client = aws.ec2_client(region) # Custom method, essentially calls boto3.client('ec2')
    vpc_id = ec2_client.create_vpc(
        CidrBlock="172.31.0.0/16",
        AmazonProvidedIpv6CidrBlock=False
    )["Vpc"]["VpcId"]
    # TODO: Attach tags on VPC creation when (if) it becomes supported
    ec2_client.get_waiter("vpc_exists").wait(VpcIds=[vpc_id])
    ec2_client.create_tags(
        Resources=[vpc_id],
        Tags=[
            {
                "Key": "Namespace",
                "Value": config.NAMESPACE
            },
            {
                "Key": "Name",
                "Value": vpc_name
            }
        ]
    )

我不明白如何获得该错误。 vpc_exists服务器不应该仅在VPC存在时返回,否则会引发WaiterError异常?我会在服务员之后设置一个睡眠1秒钟,但有些东西我做得不对吗?

1 个答案:

答案 0 :(得分:0)

您现在可以在创建时标记 VPC

response = client.create_vpc(
    CidrBlock='string',
    AmazonProvidedIpv6CidrBlock=True|False,
    Ipv6Pool='string',
    Ipv6CidrBlock='string',
    DryRun=True|False,
    InstanceTenancy='default'|'dedicated'|'host',
    Ipv6CidrBlockNetworkBorderGroup='string',
    TagSpecifications=[
        {
            'ResourceType': 'vpc',
            'Tags': [
                {
                    'Key': 'string',
                    'Value': 'string'
                },
            ]
        },
    ]
)
相关问题