Python Real-time Project For Devops On Aws | Lambda Functions

Python Real-time Project For Devops On Aws | Lambda Functions


3 min read


As a Cloud Engineering team we take care of the AWS environment and make sure it is in compliance with the organizational policies. We use AWS cloud watch in combination with AWS Lambda to govern the resources according to the policies. For example, we Trigger a Lambda function when an Amazon Elastic Block Store (EBS) volume is created. We use Amazon CloudWatch Events. CloudWatch Events that allows us to monitor and respond to EBS volumes that are of type GP2 and convert them to type GP3.

GitHub Repo:

Creation of Lambda function:

Go to search bar and type lambda and click on lambda under service as show below.

Then we will select create function and put the details as shown on image.

Under Function name you can give any name as per your requirement.

and under Runtime you have to select Python.

For Permission we are going with default option, you can also create an IAM role with set of permission for lambda creation.

Once we give the permission just hit create function and it will create the function.

Testing the Lambda test function

You have to click on Test and and under that you will find Configure test event.

After clicking on configure test event you have to given the basic details as show on below image and just click on save.

Once you configure the test you check the function by executing test key.

Configure Cloud Watch rule

In this section we will configure cloud watch rule which will trigger the Lambda function.

Go to search bar and type cloud watch and select cloud watch under services.

Go to Event on the left side panel and click on Rule.

Once you click on Rules you have to click on Create Rules and follow the same which is mention below in the diagram.

Select target(s)

For selecting the target follow the below step and click on next.

Creating a volume for testing.

  1. Go to Ec2 Dashboard and see below the volume section click on it create one

    volume for testing purpose.

  2. And select gp2 partition as we are going to convert the same into gp3

    Created a Volume.

You can see that Lambda function got triggered.

Python Code for converting volume gp2 to gp3

Now we will write Python code to convert volume gp2 to gp3

Now we will grant the permission for Roles to get the access of EBS volume.

You fill find the roles like below, click on it and attach the policy to it.

Kindly select Create inline policy.

I am going with all the permission as it is demo one. you can restrict the access.

Provide the Policy Name and click on Create

Now we will delete the volume which we have created as policy will triggered wen we create a Volume, so for that we required to create one.

Created new volume let see if policy has been triggered or not.

Policy has been triggered successfully and you can see that it has been converted to "gp3"

Thank You. please share if you find helpful.