Automating EC2 with AWS Lambda Using Boto3- Lab 5
Problem Statement:
In a dynamic and distributed AWS environment, effective management of Amazon Elastic Block Store (EBS) snapshots is essential to optimize storage costs and ensure efficient resource utilization. The challenge is to implement an automated solution that regularly identifies and deletes older EBS snapshots based on specified criteria. This solution should be designed to run as an AWS Lambda function, providing a scalable and centralized approach to snapshot management across multiple AWS regions.
Requirements:
- Automated Snapshot Deletion: Develop an AWS Lambda function that automatically identifies and deletes EBS snapshots older than the latest three snapshots.
- Dynamic Region Handling: The solution should dynamically iterate through all AWS regions to comprehensively manage snapshots across the entire infrastructure.
- AWS Account Identification: Utilize the AWS Security Token Service (STS) to identify the AWS account associated with the Lambda function for correct snapshot ownership.
- Snapshot Sorting: Implement sorting of snapshots based on their start time to identify and retain the latest three snapshots.
- Deletion Handling: Handle exceptions, specifically, to avoid attempting to delete snapshots still in use and provide relevant logging.
- Logging: Implement detailed logging to record the deletion process, including information about the snapshots being deleted and any exceptions encountered.
#Import the boto3 library, which is the AWS SDK for Python.
import boto3
#Define the main Lambda function.
def lambda_handler(event, context):
#Use the AWS Security Token Service (STS) to get the AWS account ID associated with the Lambda function.
account_id = boto3.client('sts').get_caller_identity().get('Account')
#Create an EC2 client using boto3
ec2 = boto3.client ('ec2')
#Retrieve a list of AWS regions by querying the EC2 service.
regions = [region[RegionName]
for region in ec2.describe_regions()['Regions']]
#Iterate through each AWS region, print the region name, and create a new EC2 client for the specific region.
for region in regions:
print(region)
ec2 = boto3.client('ec2', region_name=region)
#Query and describe EBS snapshots owned by the AWS account in the current region. Sort the snapshots by their start time and then remove the latest three snapshots.
response = ec2.describe_snapshots(OwnerIds=[account_id])
snapshots = ec2.describe_snapshots(OwnerIds=[account_id])['Snapshots']
snapshots.sort(key=lambda x: x["StartTime"])
snapshots = snapshots[:-3]
#Iterate through the remaining snapshots and attempt to delete each one. If a InvalidSnapshot.InUse exception is raised, it means the snapshot is still in use, so print a message and skip to the next snapsho
for snapshot in snapshots:
id = snapshot['SnapshotId']
try:
print("Deleting Snapshots:",id)
ec2.delete_snapshot(SnapshotId=id)
except Exception as e:
if 'InvalidSnapshot.InUse' in str(e):
print("Snapshot {} in use, skipping".format(id))
The overall functionality of this code is to iterate through all AWS regions, identify EBS snapshots owned by the AWS account, sort them by start time, remove the latest three, and attempt to delete the remaining snapshots. It handles the case where a snapshot is still in use by catching the InvalidSnapshot.InUse
exception and skipping the deletion in such cases.
Hope this was helpful
See you in the next lab
Happy Learning!
Shivani S