Update EC2 Tags using Python (Lambda function)
Simple example demonstrating Python's ability to lookup and update Tags.
You can also find me here.
import json import boto3 from datetime import date # I don't like the dictionary returned by AWS, so I convert it to # Key:Value pairs def parse_tags(tag_dict): my_dict = {} for tag in tag_dict: for item in tag: if item == 'Key': key = tag[item] else: value = tag[item] my_dict[key]= value return my_dict def lambda_handler(event, context): today = date.today() year = today.strftime("%Y") month = today.strftime("%m") day = today.strftime("%d") #Declare object for all of our ec2 objects in this region ec2 = boto3.resource('ec2', region_name='us-east-2') #give me all the instances instances = ec2.instances.all() print('Instances') for ins in instances: print("Instance Id: ", ins.id) ins_tag_dict = {} if(ins.tags != None): ins_tag_dict = parse_tags(ins.tags) #get function of dictionary return None if not found name_tag = ins_tag_dict.get('Name') if(name_tag == None): #Create a name tag for this object name_tag = 'Sam' ins.create_tags(Tags=[{'Key':'Name','Value': name_tag}]) #Give me all the volumes for this instance volumes = ins.volumes.all() for vol in volumes: vol_tag_dict = {} if(vol.tags != None): vol_tag_dict = parse_tags(vol.tags) print("Volume Id: ",vol.id) # attachment (LIST) has following values: ## [{'AttachTime': datetime.datetime(2018, 12, 3, 5, 11, 5, tzinfo=tzlocal()), 'Device': '/dev/xvda', 'InstanceId': 'i-XXXXXXXX', 'State': 'attached', 'VolumeId': 'vol-XXXXXXX', 'DeleteOnTermination': True}] # Convert this LIST to DICT vol_att_dict = vol.attachments[0] vol_device = vol_att_dict.get('Device') vol_name_tag = vol_tag_dict.get('Name') if(vol_name_tag == None): #Create a name tag for this object vol_name_tag = name_tag + '_' + vol_device vol.create_tags(Tags=[{'Key':'Name','Value': vol_name_tag}]) #Give ENI names of the EC2 Instance if they are missing name tag net_interfaces = ins.network_interfaces for eni in net_interfaces: print("ENI Id: ",eni.id) eni_tag_dict = {} if(eni.tag_set != None): eni_tag_dict = parse_tags(eni.tag_set) eni_name_tag = eni_tag_dict.get('Name') if(eni_name_tag == None): eni.create_tags(Tags=[{'Key':'Name','Value':name_tag}]) print('Volumes not in use') #give me all the volumes that are not in use volumes = ec2.volumes.filter(Filters=[{'Name': 'status', 'Values': ['available']}]) for vol in volumes: print("Volume Id: ",vol.id) vol_tag_dict = {} if(vol.tags != None): vol_tag_dict = parse_tags(vol.tags) vol_mode_tag = vol_tag_dict.get('Mode') if(vol_mode_tag == None): #If Mode tag does not exist, make it Auto mode vol.create_tags(Tags=[{'Key':'Mode','Value':'Auto'}]) vol_expire_tag = vol_tag_dict.get('Expire') if(vol_expire_tag == None): #If Expire tag does not exist then set it to 7 day from now ## we'd have a different function to do the actual cleanup new_day = str(int(day) + 7).zfill(2) expireDate = year + month + new_day vol.create_tags(Tags=[{'Key':'Expire','Value': expireDate}]) return { 'statusCode': 200, 'body': json.dumps('Finished!') }
No comments:
Post a Comment