AWS宝途的3-无法创建标签,例如ID未发现 - 不同的区域

问题描述 投票:0回答:1

我想设置标签上使用lambda函数的多个区域中的实例:

打印instance_ids给了我正确的ID

[ 'I-008a4292a5928c85f']

[ 'I-008a4292a5928c85f', 'I-03253cdbe35bfb1e2']

  instance_ids = []
    launch_date = ""
    launched = ""
    launched1 = ""
    ec = boto3.client('ec2')
    ec2_regions = [region['RegionName'] for region in ec.describe_regions()['Regions']]
    for region in ec2_regions:
     ec = boto3.client('ec2', region_name=region)
     ec2 = boto3.resource('ec2',region_name=region)
     reservations = ec.describe_instances().get('Reservations', []) 

     for reservation in reservations:
      for instance in reservation['Instances']:
         tags = {}
         for tag in instance['Tags']:
             tags[tag['Key']] = tag['Value']
             if tag['Key'] == 'Name':
               name=tag['Value']
         if not 'Owner' in tags or tags['Owner']=='unknown' or tags['Owner']=='Unknown':
              instance_ids.append(instance['InstanceId'])  
              if not 'TerminateOn' in tags:#, create it
                 print  instance_ids
                 ec2.create_tags(Resources=instance_ids ,Tags=[{'Key':'TerminateOn','Value':date_after_month.strftime('%d/%m/%Y')}])

[“I-008a4292a5928c85f”]是在不同的区域和λ为它创建标签:

I-03253cdbe35bfb1e2是在同一地区的λ和标记为该机没有创建尽管拉姆达抱怨无法找到实例,其标签创建

ClientError: An error occurred (InvalidInstanceID.NotFound) when calling the CreateTags operation: The instance ID 'i-008a4292a5928c85f' does not exist

如果手动指定实例ID,没有任何问题

所以创造了把INSTANCE_ID,而不是名单:

if not 'TerminateOn' in tags:
                  a = "'" + instance['InstanceId'] + "'"
                  #print a
                  ec2.create_tags(Resources=[a] ,Tags=[{'Key':'TerminateOn','Value':date_after_month.strftime('%d/%m/%Y')}])

but getting The ID ''i-008a4292a5928c85f'' is not valid"
python aws-lambda boto3
1个回答
0
投票

作品以这种方式(不知道为什么它不会采取列表作为参数),用实例ID指定变量

if not 'Owner' in tags or tags['Owner']=='unknown' or tags['Owner']=='Unknown':
              #ec2 = boto3.resource('ec2',region_name=region)
              instance_ids.append(instance['InstanceId'])  
              if not 'TerminateOn' in tags:

                  ec2.create_tags(Resources=[instance['InstanceId']] ,Tags=[{'Key':'TerminateOn','Value':date_after_month.strftime('%d/%m/%Y')}])
© www.soinside.com 2019 - 2024. All rights reserved.