Code times out when trying to run as a lambda function in AWS

Refresh

February 2019

Views

87 time

1

Below is my code and I am hoping someone can help me with the cleaning up the code and making it more effiencient. Basically, the code should iterate through all the volumes in my AWS account and then list all untagged volumes and then send out an email. However, it times out when running it as a lambda function in AWS but if i run it locally, it will take over 30 mins to complete (however it does complete). Im sure its iterating through things it doesnt need.

Also if I print the ec2_instances list, I can see duplicate values, so I want to only have unique values so that its not repeating the script for each ec2 instance.

import logging
import boto3
from smtplib import SMTP, SMTPException
from email.mime.text import MIMEText

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

session = boto3.Session(profile_name="prod")
client = session.client('ec2')

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

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'])

unique_instances = list(set(ec2_instances))

# Create the msg body.
msg_body_list = []
for instance in unique_instances:
    desc_instance = client.describe_instances()

    # append to the msg_body_list the lines that we would like to show on the email
    msg_body_list.append("VolumeID: {}".format(desc_instance['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['VolumeId']))
    msg_body_list.append("Attached Instance: {}".format(desc_instance['Reservations'][0]['Instances'][0]['InstanceId']))

    # if there are tags, we will append it as singles lines as far we have tags
    if 'Tags' in desc_instance['Reservations'][0]['Instances'][0]:
        msg_body_list.append("Tags:")
        for tag in desc_instance['Reservations'][0]['Instances'][0]['Tags']:
            msg_body_list.append("    Key: {} | Value: {}".format(tag['Key'], tag['Value']))
    # in case we don't have tags, just append no tags.
    else:
        msg_body_list.append("Tags: no tags")
        msg_body_list.append("--------------------")

# send email
mail_from = "[email protected]"
mail_to = '[email protected]'

msg = MIMEText("\n".join(msg_body_list))
msg["Subject"] = "EBS Tagged Instance Report for"
msg["From"] = mail_from
msg["To"] = mail_to

try:
    server = SMTP('xxx.xxx.xxx.xxx', 'xx')
    server.sendmail(mail_from, mail_to.split(','), msg.as_string())
    server.quit()
    print('Email sent')
except SMTPException:
    print('ERROR! Unable to send mail')

1 answers

0

Lambda functions have a time limit of 15 minutes. That is the reason for the timeout - if you need to run scripts for longer, look up AWS Fargate.