Skip to content

Instantly share code, notes, and snippets.

@danzelziggy
Forked from mlapida/EC2-Tag-Assets-Lambda.py
Created August 13, 2018 08:20
Show Gist options
  • Select an option

  • Save danzelziggy/2a4b8de08f765c0563d95d3b25c70c89 to your computer and use it in GitHub Desktop.

Select an option

Save danzelziggy/2a4b8de08f765c0563d95d3b25c70c89 to your computer and use it in GitHub Desktop.

Revisions

  1. Michael Lapidakis renamed this gist Jan 29, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. Michael Lapidakis revised this gist Jan 29, 2016. No changes.
  3. Michael Lapidakis created this gist Jan 15, 2016.
    77 changes: 77 additions & 0 deletions EC2-Tag-Assets.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    from __future__ import print_function

    import json
    import boto3
    import logging

    #setup simple logging for INFO
    logger = logging.getLogger()
    logger.setLevel(logging.ERROR)

    #define the connection region
    ec2 = boto3.resource('ec2', region_name="us-west-2")

    #Set this to True if you don't want the function to perform any actions
    debugMode = False

    def lambda_handler(event, context):
    #List all EC2 instances
    base = ec2.instances.all()

    #loop through by running instances
    for instance in base:

    #Tag the Volumes
    for vol in instance.volumes.all():
    #print(vol.attachments[0]['Device'])
    if debugMode == True:
    print("[DEBUG] " + str(vol))
    tag_cleanup(instance, vol.attachments[0]['Device'])
    else:
    tag = vol.create_tags(Tags=tag_cleanup(instance, vol.attachments[0]['Device']))
    print("[INFO]: " + str(tag))

    #Tag the Network Interfaces
    for eni in instance.network_interfaces:
    #print(eni.attachment['DeviceIndex'])
    if debugMode == True:
    print("[DEBUG] " + str(eni))
    tag_cleanup(instance, "eth"+str(eni.attachment['DeviceIndex']))
    else:
    tag = eni.create_tags(Tags=tag_cleanup(instance, "eth"+str(eni.attachment['DeviceIndex'])))
    print("[INFO]: " + str(tag))

    #------------- Functions ------------------
    #returns the type of configuration that was performed

    def tag_cleanup(instance, detail):
    tempTags=[]
    v={}

    for t in instance.tags:
    #pull the name tag
    if t['Key'] == 'Name':
    v['Value'] = t['Value'] + " - " + str(detail)
    v['Key'] = 'Name'
    tempTags.append(v)
    #Set the important tags that should be written here
    elif t['Key'] == 'Application Owner':
    print("[INFO]: Application Owner Tag " + str(t))
    tempTags.append(t)
    elif t['Key'] == 'Cost Center':
    print("[INFO]: Cost Center Tag " + str(t))
    tempTags.append(t)
    elif t['Key'] == 'Date Created':
    print("[INFO]: Date Created Tag " + str(t))
    tempTags.append(t)
    elif t['Key'] == 'Requestor':
    print("[INFO]: Requestor Tag " + str(t))
    tempTags.append(t)
    elif t['Key'] == 'System Owner':
    print("[INFO]: System Owner Tag " + str(t))
    tempTags.append(t)
    else:
    print("[INFO]: Skip Tag - " + str(t))

    print("[INFO] " + str(tempTags))
    return(tempTags)