【发布时间】: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