简体   繁体   English

使用boto3在AWS帐户中取消标记ec2实例的列表

[英]list of untagges ec2 instances in aws account using boto3

The code below is giving me the result for one specified region, can anyone help me how to get all untagged ec2 instances information across all regions in one aws account? 下面的代码为我提供了一个指定区域的结果,有人可以帮助我如何在一个AWS帐户中跨所有区域获取所有未标记的ec2实例信息吗?

#!/usr/bin/env python
import boto3
import json, ast

instances = [i for i in boto3.resource('ec2', region_name='us-east-2').instances.all()]

for i in instances:
    d = (i.tags[0])
    d2 = ast.literal_eval(json.dumps(d))
    if ( d2['Value'] == "" ):
            print i.instance_id

Get list of all regions. 获取所有区域的列表。 Loop through each region and execute your code. 遍历每个区域并执行您的代码。 Something like this: 像这样:

def do_tags(region):
  instances = [i for i in boto3.resource('ec2', region_name=region).instances.all()]

  for i in instances:
      d = (i.tags[0])
      d2 = ast.literal_eval(json.dumps(d))
      if ( d2['Value'] == "" ):
              print i.instance_id

regions = boto3.session.Session().get_available_regions('ec2')
for region in regions:
  print 'Checking region:', region
  do_tags(region)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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