Python Forum
Script for untagged resources in aws
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script for untagged resources in aws
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Resources for printing tabledata-Linux and Windows hammer 4 1,390 Apr-25-2022, 12:09 PM
Last Post: hammer
  Wanted to tag azure resources with creator name raham3406 0 1,423 Apr-25-2021, 02:24 PM
Last Post: raham3406
  Need learning resources for Python distribution gretchenfrage 2 2,091 Nov-12-2020, 06:42 PM
Last Post: gretchenfrage
  Resources for working with images and video? SheCurvesMobius 0 1,383 Mar-28-2020, 08:43 PM
Last Post: SheCurvesMobius
  AWS lambda script help - Listing untagged volumes jutler 0 2,226 Feb-13-2019, 02:36 PM
Last Post: jutler
  Resources for converting Python 2.x code to 3.x Luke_Drillbrain 4 4,950 May-03-2017, 06:34 PM
Last Post: snippsat
  Audiovisual resources Hacsa 4 4,219 Apr-30-2017, 08:36 AM
Last Post: Hacsa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020