1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| import json,boto3 def get_ec2_ebs_tag(region_name): ec2_client = boto3.client('ec2',region_name=region_name) ec2list=[] ec2_message=ec2_client.describe_instances() ''' 获取一些实例的信息: 1.实例ID 2.实例的Tag 3.EBS的ID ''' result_len=len(ec2_message['Reservations']) for i in range(0,result_len): ebsid=[] ec2id=ec2_message['Reservations'][i]['Instances'][0]['InstanceId'] for e in ec2_message['Reservations'][i]['Instances'][0]['BlockDeviceMappings']: ebsid.append(e['Ebs']['VolumeId']) '''如果源主机没有Tags,则自定义Tag''' if 'Tags' not in ec2_message['Reservations'][i]['Instances'][0]: keyname="源主机没有Tag" else: keyname=ec2_message['Reservations'][i]['Instances'][0]['Tags'][0]['Value'] test={ "EC2ID":ec2id, "KeyName":keyname, "EBSID":ebsid } ec2list.append(test) return ec2list def create_ebs_tag(region_name): ''' 这里的代码是根据EC2的Tag来未没有打Tag的资源打上Tag ''' ec2=boto3.resource('ec2',region_name=region_name) ec2Message=get_ec2_ebs_tag(region_name) for i in range(0,len(ec2Message)): value=ec2Message[i]['KeyName'] ebsid=ec2Message[i]['EBSID'] '''一个实例会有多个EBS,这里判断是否是多个EBS''' if len(ebsid)==1: volume_id = ec2.Volume(ebsid[0]) tag=volume_id.create_tags( DryRun=False, Tags=[ { 'Key': 'Name', 'Value': value }, ] ) else: for k in range(0,len(ebsid)): volume_id = ec2.Volume(ebsid[k]) tag=volume_id.create_tags( DryRun=False, Tags=[ { 'Key': 'Name', 'Value': value }, ] ) def get_eip_Create(region_name): '''获取EIP列表''' client = boto3.client('ec2',region_name=region_name) addr = client.describe_addresses() addr_len = len(addr['Addresses']) for i in range(0, addr_len): '''如果没有tag但是挂载到了实例上,则将实例的tag赋值给EIP''' if 'Tags' not in addr['Addresses'][i] and 'InstanceId' in addr['Addresses'][i] : instance_id = addr['Addresses'][i]['InstanceId'] AllocactionID = addr['Addresses'][i]['AllocationId'] keyname=client.describe_instances(InstanceIds=[instance_id],) keyName=keyname['Reservations'][0]['Instances'][0]['Tags'][0]['Value'] response=client.create_tags( Resources=[AllocactionID,], Tags=[{ "Key":"Name", "Value":keyName }, ], ) '''如果即没有tag也没有挂载到实例上并且没有附加到Natgateway上,则打上自定义tag''' elif 'Tags' not in addr['Addresses'][i] and 'InstanceId' not in addr['Addresses'][i] and 'PrivateIpAddress' not in addr['Addresses'][i]: AllocactionID = addr['Addresses'][i]['AllocationId'] keyName="没有挂载也没有Tag" print("没有挂载也没有Tag:"+addr['Addresses'][i]['AllocationId']) response=client.create_tags( Resources=[AllocactionID,], Tags=[{ "Key":"Name", "Value":keyName }, ], ) '''如果没有tag也没有挂载到实例上,但是有绑定到其他资源,则自定义Tag''' elif 'Tags' not in addr['Addresses'][i] and 'InstanceId' not in addr['Addresses'][i] and 'PrivateIpAddress' in addr['Addresses'][i]: AllocactionID = addr['Addresses'][i]['AllocationId'] keyName="可能是NatGateway" response=client.create_tags( Resources=[AllocactionID,], Tags=[{ "Key":"Name", "Value":keyName }, ], ) def lambda_handler(event, context): client=boto3.client('ec2') regions=client.describe_regions() for i in range(0,len(regions['Regions'])): create_ebs_tag(region_name=regions['Regions'][i]['RegionName']) print("现在处理的Region:",regions['Regions'][i]['RegionName']) get_eip_Create(region_name=regions['Regions'][i]['RegionName'])
|