AWS Resume ready project using AWS Lambda and EventBridge to Convert AWS EBS Volume type GP2 To GP3
Objective
Trigger a Lambda function when an Amazon Elastic Block Store (EBS) volume is created of type GP2 and convert them to type GP3.
Overview
1. Create a Lambda Function and Add code logic to convert EBS Volume type
2. Create an EventBridge event to trigger the Lambda function
Pre-requisites
Basic knowledge of AWS Lambda and Python Language
Working AWS account
1. Create a Lambda Function and Add code logic to convert EBS Volume type
Create the lambda function with the name of your choice and select the Python latest runtime, Leave the rest setting as it is.
Replace the following code in the Lambda function
import re import boto3 client = boto3.client('ec2') def get_volume_id_from_arn(volume_arn): return re.search(r'vol-[a-f0-9]+', volume_arn).group() def lambda_handler(event, context): vol_arn = event['resources'][0] vol_id = get_volume_id_from_arn(vol_arn) response = client.modify_volume( VolumeId=vol_id, VolumeType='gp3', ) return { 'statusCode': 200, 'body': json.dumps('Hello from Lambda!') }
get_volume_id_from_arn(volume_arn)
: This is a custom function defined to extract the volume ID from an Amazon Resource Name (ARN). It uses a regular expression to search for a pattern that matches the volume ID in the ARN and returns it.vol_arn = event['resources'][0]
: It retrieves the ARN of the resource that triggered the Lambda function from theevent
parameter. The assumption here is that the Lambda function is triggered by an event related to an Amazon EC2 volume.response = client.modify_volume(...)
: This line uses the Boto3 client to modify the volume identified by the volume ID extracted earlier. It changes the volume type to 'gp3', which is a type of General Purpose SSD (gp3) volume in Amazon EC2.
Create an EventBridge event to trigger the Lambda function
Open Rule in AWS Event Bridge and go to Rules and then Create Rules to Create Rule
A rule watches for specific types of events. When a matching event occurs, the event is routed to the targets associated with the rule. A rule can be associated with one or more targets.)
In Our case, When the EBS volumes get created the EventBridge will listen to that event and route that event to Lambda function.
Add the Name and description as per your choice and leave the default setting
On the next page leave the default setting and scroll down to the last section Event pattern
Select EC2 AWS Service, EBS Volume Notification in Event type, Event type specification should be Specific event(s), and select modifyVolume.
-
On the next page in the Target Section select AWS Lambda in AWS Service and select the Lambda function we created.
After Creating the Event Rule, try to create the EBS instance of Type GP2 and check whether that works.
It will throw the following error saying AWS Lambda doesn't have access to modify EBS Volume. We will need to assign permission to the AWS Lambda Role.
Open the role attached to AWS Lambda and Create an inline policy to modify the EBS Volume type.
- Try to create the volume of GP2 again and the EBS Volume type will change from GP2 to GP3.
Conclusion
In this guide, we've outlined a straightforward process to automate the conversion of Amazon Elastic Block Store (EBS) volumes from type GP2 to GP3 using AWS Lambda and EventBridge. By following the steps provided, you can set up a Lambda function to trigger when a GP2 volume is created, seamlessly converting it to GP3. This automation enhances efficiency and ensures your EBS volumes are optimized for performance. With basic knowledge of AWS Lambda and Python, you can implement this solution to streamline your AWS infrastructure management. Don't forget to add this project in your resume.
I hope you learned something new today! If this post was helpful, please like this article to show your support.