AWS environment, there is no automation process to update or create EC2 Server-Name Tags (ex: Tag{key}: Name  Tag{Value}:  ABCSRV001) into attached EBS volumes. The  “Name” tag has to be created manually in volumes.  This is an issue when try to identify  which ec2 instance is, or was, associated with the volume. You should use an Instance ID all the time to search the volume.

Also, this is an issue in CPM backups to identify snapshots due to missing “Name” Tag in EBS Volumes.

To fill the gap for above issue, developed Lambda Function to create tags in attached volumes with similar to “Name” tag of EC2 Instance.

About this Lambda Script

This function is designed to tag volumes of EC2 instance, which is managed by Kloud. First, function will filter the instances with tag of “Kloud_Managed = True”. Then it will record the “instance id” and “Name” tag details (As key and value in python dictionary).

Next the function will create a list of volumes filtered with instance id.

Finally, the function will match the instance id’s from ec2 dictionary(key) and list of volumes. If the ids match, it creates a new tag in volume with server name (dictionary value). The lambda function will be triggering on every Friday at 18:00 Hrs in client environment

Screenshot of Before executing the script..

This Instance has Name Tag and  this instance is managing by Kloud (Tag:kloud_managed Value: True)

The attached volumes doesn’t have  TAG value on  Kloud_Name Tag.

Screenshot of After executing the script…

After executing the Lambda function new Kloud_name tag creates in attached volumes, if Instance managed by Kloud ( Tag:Key”kloud_managed” Tag:Value “true)

 Flow Diagram of the script


IAM Policy which is attached to Lambda function

IAM Role created with full privileges to describe and create tags on ec2.

The Cloud-watch event for trigger the Lambda function

Lambda has setup for trigger via Cloud-watch event.

The Lambda Script

This Lambda script has written in Python.

#--- Script for update the volume tags in which is not matching with instance tags---
#!/usr/bin/env python
import boto3
ec2 = boto3.resource('ec2')
ec2client = boto3.client('ec2')

#-----Define Lambda function-----#
def lambda_handler(event, context):
#-----Check& filter Instances which  Kloud_managed equal true-----#
    instances = ec2client.describe_instances(Filters=[{'Name': 'tag:kloud_managed', 'Values': ['True']}])
#-----Define dictionary to store Tag Key & value------#
#-----Store Key & Value of Instance Tag:“Name” ------#
    for reservation in instances['Reservations']:
        for instance in reservation['Instances']:
           for tag in instance['Tags']: 
                if tag['Key'] == 'Name':
                    #print ( instance['InstanceId'],tag['Value'])
                    #ids.append(( instance['InstanceId'],tag['Value']))
                    dict[instance['InstanceId']]= tag['Value']
#-----Store Key & Value with attached instance ID of all volumes ------#     
    volumes = ec2.volumes.all() 
    for volume in volumes:
#-----compare dictionary value Key:InstanceID and volume attached Key:InstanceID ------#     
        for a in volume.attachments:
            for key, value in dict.items():
#-----compare dictionary value Key:InstanceID and volume attached Key:InstanceID ------# 
#-----If the InstanceID matched create new Tag:’Kloud_Name’ with key of value: servername ------#     
            if a['InstanceId'] == key:
                     volume.create_tags(Tags=[{'Key': 'Kloud_Name', 'Value': value}])
Application Development and Integration, Uncategorized
, , ,