【问题标题】:boto3 vpc_exists waiter returns before VPC exists?boto3 vpc_exists 服务员在 VPC 存在之前返回?
【发布时间】:2021-05-17 22:19:36
【问题描述】:

在创建 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 秒,但有什么我做的不对吗?

【问题讨论】:

  • 我不确定这是否可行,但你可以试试vpc_available 服务员吗?它具有比vpc_exists 服务员更大的等待超时和轮询间隔。
  • 所以你说的方法看起来不错。然后我将切换到 vpc_available。希望它能防止这个问题再次出现。
  • 如果您需要经常这样做,您应该考虑 cloudformation。只要 AWS 不提供 VPC 创建令牌 API 来允许对 VPC 的状态进行异步查询,cloudformation 就是摆脱繁琐服务员的唯一方法。

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


【解决方案1】:

您现在可以在创建时标记 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'
                },
            ]
        },
    ]
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-05
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多