简体   繁体   中英

boto3 vpc_exists waiter returns before VPC exists?

I got the following error when trying to set a VPC's tags after creating it (once out of a few dozen tries. It works most of the time, but not always):

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

I checked afterwards, and the VPC vpc-4240de24 did exist, so CreateTags was called too early.

The error occurred in the following method:

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

I do not understand how getting that error is even possible. Shouldn't the vpc_exists waiter return only when the VPC exists, and raise the WaiterError exception otherwise? I would set a sleep for 1 second after the waiter, but is there something I'm not doing correctly?

You can now tag the VPC on creation

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'
                },
            ]
        },
    ]
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM