【问题标题】:Check if an instance in AWS have absolutely no tags using python boto3使用 python boto3 检查 AWS 中的实例是否绝对没有标签
【发布时间】:2020-05-22 11:06:28
【问题描述】:

我正在使用 python boto3 从 AWS 实例中查询和更新标签。我正在尝试处理实例上存在“NO”标签的情况。我认为标签实际上是 boto3 ec2 查询响应中的字典列表,但是检查不存在的“列表”的方法都不起作用。我尝试了以下选项(i['Tags'] 是标签列表)

if i['Tags']:
if not i['Tags']:
if len(i['Tags']) != 0:

由于实例上不存在任何标签,因此上述所有条件均失败并显示以下错误消息。

Traceback (most recent call last):
  File "./tag-automation.py", line 106, in <module>
    read_instance_tags()
  File "./tag-automation.py", line 81, in read_instance_tags
    if not i['Tags']:
KeyError: 'Tags'

任何解决此问题的建议都会非常有帮助。谢谢。

【问题讨论】:

    标签: python amazon-web-services boto3


    【解决方案1】:

    如果没有标签,那么字典中就不会有Tags 条目。

    因此,使用:

    if 'Tags' not in i:
    

    【讨论】:

      猜你喜欢
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2017-02-27
      相关资源
      最近更新 更多