Python Forum

Full Version: Script for untagged resources in aws
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,
Im wondering if someone can please please help me. I am trying to achieve the following. I want to create a python script which will eventually become a lambda. but for now I need to get it working as a script. I want it to go through my work aws account and list ALL untagged ebs volumes. From this, it should send me an email with a list of all the volumes it has found along with what ec2 instances they are attached to and the tags of those ec2 instances in a nice table. Below is the code which I have so far

import logging
import boto3
import smtplib
from itertools import zip_longest

logger = logging.getLogger()
logger.setLevel(logging.INFO)

client = boto3.client('ec2')

my_session = boto3.session.Session()
my_region = my_session.region_name

untagged_volumes = []
detached_volumes = []
ec2_instances = []
ec2_properties = []

response = client.describe_volumes()

for volume in response['Volumes']:
    if 'Tags' in str(volume):
        continue
    else:
        if 'available' in str(volume):
            detached_volumes.append(volume['VolumeId'])
        else:
            untagged_volumes.append(volume['VolumeId'])
            untagged_volumes.append(volume['Attachments'][0]['InstanceId'])
            ec2_instances.append(volume['Attachments'][0]['InstanceId'])

for instance in ec2_instances:
    get_instance = client.describe_instances(
    InstanceIds=[
        '%s'%(instance)
        ],
    )
    ec2_properties.append(get_instance['Reservations'][0]['Instances'][0]['InstanceId'])
    ec2_properties.append(get_instance['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['VolumeId'])
    ec2_properties.append(get_instance['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['Status'])
    ec2_properties.append(get_instance['Reservations'][0]['Instances'][0]['Tags'])
Whilst this code works in general and does give me a json formatted list, it will eventually error out. This is because we have some instances in our aws account which have tags which are set but have no values. So i want to implement something like a dict.get() so if it comes across a tag with no value, it sets the value as "not _set" and continues. ec2_properties.append(get_instance['Reservations'][0]['Instances'][0]['Tags']) This is the part of the code which gets the Tags and this is the error:
Quote:File "c:/python/volumes.py", line 41, in ec2_properties.append(get_instance['Reservations'][0]['Instances'][0]['Tags']) KeyError: 'Tags'

In really helping someone can help me. Appreciate any help I can get. Smile
Take a look: EAFP

You can just handle the exception which is similar to providing a default value in dict.get() method.(probably how it works behind the scene)

PS:
Since you can use dict['Key'] to access the element it looks like a nested dictionary so .get should work too.

Edit: client.describe_instances does indeed return a dict. As stated in documents.