Step by Step Implementation of AWS Samurai - Python Project

Step by Step Implementation of AWS Samurai - Python Project

ยท

4 min read

๐Ÿ“ Introduction:

In this tutorial, we will walk through the process of implementing and executing an AWS Lambda function using a CloudWatch Event. We will convert an EBS volume from gp2 to gp3 using Python and the Boto3 library.

Prerequisites: Before starting this tutorial, make sure you have the following prerequisites:

  1. An AWS account with appropriate permissions to create and manage Lambda functions and CloudWatch events.

  2. Python and Boto3 library installed on your local machine for development.

  3. An understanding of AWS Lambda and basic Python programming.

๐Ÿ“ High-Level Design ๐Ÿ“๐Ÿ–Œ๏ธ:

Behold, a glimpse into the magnificent high-level design of AWS Samurai! The interface, adorned with the essence of our noble warrior, allows you to configure and customize the behavior of Lambda functions. Harness their power to shape the destiny of your AWS governance, ensuring optimal performance, unwavering security, and unparalleled efficiency. The screenshot presented showcases the awe-inspiring AWS Samurai interface, a portal to victory in the realm of AWS governance! ๐ŸŽจ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ–ฅ๏ธ

๐Ÿ”น Step 1: Create a new IAM role

  1. Go to the IAM service in the AWS Management Console.

  2. Click on "Roles" in the left navigation pane.

  3. Click "Create role".

  4. Select "AWS service" as the trusted entity and choose "Lambda" as the service.

  5. Attach the necessary policies for Lambda execution and EC2 volume modification.

  6. Provide a name for the role and create it.

  7. Attaching to Policies to the Role:

  1. Policies for AWS Lambda:

  1. Policies rule for AWS EBS Volume:

๐Ÿ”น Step 2: Develop the Lambda function code

  1. Set up your development environment with Python and Boto3.

  2. Create a new Python file, e.g., lambda_function.py.

  3. Copy and paste the following code into the file:

import json
import boto3


def convert_volume_to_gp3(volume_arn):
    ec2_client = boto3.client('ec2')

    arn_parts = volume_arn.split(':')
    volume_id = arn_parts[-1].split('/')[-1]
    return volume_id

def lambda_handler(event, context):

    volume_arn = event['resources'][0]
    volume_id = convert_volume_to_gp3(volume_arn)

    ec2_client = boto3.client('ec2')

    response = ec2_client.modify_volume(
        VolumeId=volume_id,
        VolumeType = 'gp3',
    )
  1. Save the file.

๐Ÿ”น Step 3: Create a Lambda function

  1. Go to the Lambda service in the AWS Management Console.

  2. Click "Create function".

  3. Select "Author from scratch" as the blueprint.

  4. Provide a name for the function and choose Python as the runtime.

  5. Select the IAM role you created in Step 1.

  6. Click "Create function".

๐Ÿ”น Step 4: Configure the Lambda function

  1. In the function configuration page, scroll down to the "Function code" section.

  2. Choose "Upload a .zip file" in the "Code entry type" dropdown.

  3. Click on "Upload" and select the lambda_function.py file you created.

  4. Set the "Handler" field to lambda_function.lambda_handler.

  5. Set the "Timeout" according to your requirements.

  6. Click "Save".

๐Ÿ”น Step 5: Set up a CloudWatch Event

  1. Go to the CloudWatch service in the AWS Management Console.

  2. Click on "Events" in the left navigation pane.

  3. Click "Create rule".

  4. Choose "Event pattern" and select the desired event source (e.g., EC2).

  5. Configure the event pattern based on your requirements.

  6. Select the target as "Lambda function" and choose the Lambda function you created in Step 3.

  7. Click "Configure details".

  8. Provide a name and description for the rule.

  9. Click "Create rule".

๐Ÿ”น Step 6: Test the Lambda function

  1. Go back to the Lambda service in the AWS Management Console.

  2. Open the function you created in Step 3.

  3. Click on the "Test" button in the top-right corner.

  4. Select "Create new test event" from the dropdown.

  5. Provide a name for the test event, e.g., "TestEvent".

  6. Copy and paste the following JSON payload into the event body:

{
  "resources": [
    "arn:aws:ec2:region:account-id:volume/volume-id"
  ]
}
  1. Replace region, account-id, and volume-id with the actual values corresponding to your AWS environment.

  2. Click "Create".

  3. Click on the "Test" button again to execute the Lambda function with the test event.

  4. Monitor the execution results and check the CloudWatch logs for any error messages.

๐Ÿ”น Step 7: Verify the EBS volume conversion

  1. Go to the EC2 service in the AWS Management Console.

  2. Navigate to the volume that you specified in the test event.

  3. Check the volume details to verify that it has been converted to gp3.

  4. Ensure that the volume attributes (such as IOPS and throughput) meet your desired configuration.

Created EBS Volume of type GP2:

Modified EBS Volume to type GP3 as per Organization Governance Compliance:

๐Ÿ“ Conclusion:

In this tutorial, we covered the step-by-step process of implementing and executing an AWS Lambda function using a CloudWatch Event. We converted an EBS volume from gp2 to gp3 using Python and the Boto3 library. You can now apply this knowledge to automate various tasks and workflows using Lambda and CloudWatch in your AWS environment.

Remember to properly configure event patterns and IAM permissions to ensure the security and reliability of your serverless applications.

๐Ÿ“ Resources to follow along By:

๐Ÿ”— youtu.be/DgavixR_w5Y

๐Ÿ”น Checkout GitHub Repository for projects:

๐Ÿ”— github.com/sumanprasad007

Did you find this article valuable?

Support Prasad Suman Mohan by becoming a sponsor. Any amount is appreciated!

ย