AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services.
It is designed to enable developers to run code without provisioning or managing servers.
It executes code in response to events and automatically manages the computing resources required by that code.
Key Features:
Event-Driven Execution: AWS Lambda allows developers to trigger functions in response to various events such as changes to data in an Amazon S3 bucket, updates to a DynamoDB table, or an HTTP request via Amazon API Gateway.
Scalability: Lambda automatically scales to handle the number of requests, ensuring optimal performance without the need for manual intervention. It can handle a few requests per day to thousands of requests per second.
Pay-Per-Use Pricing Model: With AWS Lambda, you only pay for the compute time that you consume. There are no upfront costs or charges when your code is not running.
Multilanguage Support: Lambda supports multiple programming languages, including Node.js, Python, Java, C#, and more, giving developers the flexibility to choose the language that best suits their application.
Integrated Security: AWS Lambda integrates with AWS Identity and Access Management (IAM) to control access to functions and resources. Developers can define IAM roles and permissions to manage who can invoke functions and what resources they can access.
Hands-On : AWS cloud cost optimization By AWS Lambda - Identifying Stale Resources
In this example, we'll create a Lambda function that identifies EBS snapshots that are no longer associated with any active EC2 instance and deletes them to save on storage costs.
The Lambda function fetches all EBS snapshots owned by the same account ('self') and also retrieves a list of active EC2 instances (running and stopped). For each snapshot, it checks if the associated volume (if exists) is not associated with any active instance. If it finds a stale snapshot, it deletes it, effectively optimizing storage costs.
We have the EC2 instance along with its default volume.
Create the snapshot of the instance volume.
Creation of LAMBDA function by using Python runtime:
Copy the Python code from GitHub:
https://github.com/iam-veeramalla/aws-devops-zero-to-hero/blob/main/day-18/ebs_stale_snapshosts.py
import boto3
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
# Get all EBS snapshots
response = ec2.describe_snapshots(OwnerIds=['self'])
# Get all active EC2 instance IDs
instances_response = ec2.describe_instances(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
active_instance_ids = set()
for reservation in instances_response['Reservations']:
for instance in reservation['Instances']:
active_instance_ids.add(instance['InstanceId'])
# Iterate through each snapshot and delete if it's not attached to any volume or the volume is not attached to a running instance
for snapshot in response['Snapshots']:
snapshot_id = snapshot['SnapshotId']
volume_id = snapshot.get('VolumeId')
if not volume_id:
# Delete the snapshot if it's not attached to any volume
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Deleted EBS snapshot {snapshot_id} as it was not attached to any volume.")
else:
# Check if the volume still exists
try:
volume_response = ec2.describe_volumes(VolumeIds=[volume_id])
if not volume_response['Volumes'][0]['Attachments']:
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Deleted EBS snapshot {snapshot_id} as it was taken from a volume not attached to any running instance.")
except ec2.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'InvalidVolume.NotFound':
# The volume associated with the snapshot is not found (it might have been deleted)
ec2.delete_snapshot(SnapshotId=snapshot_id)
print(f"Deleted EBS snapshot {snapshot_id} as its associated volume was not found.")
Paste the code in lambda_function tab of code:
Test the code manually.
Change the minimum execution time :
The minimum execution time for a LAMBDA function is "10 sec".
Define some permissions to the role :
DescribeSnapshot
DeleteSnapshot
DescribeInstance
DescribeVolume
in order to fetch all the desired data.
Again test the function:
Status : Succeeded
But nothing happen on running this function.
Delete the instance but leave the snapshot of its volume.
Run the code:
In this run the EBS snapshot is deleted due to non-associated with its volume(we've deleted earlier.)
Automatically invoke the LAMBDA function through CloudWatch
Create an EC2 instance & snapshot of instance volume.
Delete the instance but left the snapshot:
CloudWatch Operation:
Create an event for scheduling/ invoking LAMBDA function.
Define the date & time for invoking :
Set target to AWS Lambda for invoking the function.
Select the created LAMBDA function.
LAMBDA function automatically trigged at defined schedule time & it deleted the snapshot of volume.
You can check the log for the consequences of LAMBDA function invoke.
You can check the schedule(In CloudWatch) defined for LAMBDA invocation:
Thank you for reading! Happy Learning!!
Santosh Chauhan