Convert EBS volume Type GP2 To GP3 Automatically By triggering Lambda function.
Tags: lambda, cloudwatchrule, ebsvolume, cloudwatch
Published: 2023-08-01 06:21:37




![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iob5ojl3f830j3o751xj.png)




Youtube Video reference link: https://youtu.be/DgavixR_w5Y
Here 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.

Here we will Learn about (Basic Requirements):
1.Cloudwatch Rule
2.Lambda Function
3.IAM Rule
4.EBS Volume
5.Cloudwatch Log Groups


**Creation of lambda:**



1. First Sign-in to AWS account and search for lambda. Then we have click on create a function and name and select the RUN Time as python(latest) and click on create Function.
2. Under the code section we will get by default python HEllO world script and we have to delete that script and we have to enter the script as shown in the below.

```
import boto3

def get_volume_id_from_arn(volume_arn):
    #split the ARN using the colon (':') seperator
    arn_parts = volume_arn.split(':')
    # the volume ID is the last part of the ARN ofter the 
    volume_id = arn_parts[-1].split('/')[-1]
    return volume_id

def lambda_handler(event, context):

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

    ec2_client = boto3.client('ec2')

    response = ec2_client.modify_volume(
        VolumeId=volume_id,
        VolumeType='gp3',
    )
```


3. Deploy this code. 
4. Ofter writing this we have to create one IAM role for accessing the EBS Volume events. By default if we create lambda Function lambda will create on IAM Role we can check that Role under lambda function configuration under permissions.


**Creation of IAM Role:**


1. We have to create one IAM role that role should have permission to access cloud watch logs and EBS Volume events.
2. While creating we will get one IAM Role Search for that Role and add some more permissions like EBS Volume events to that role.
3. Click on the perticular IAM role and click on add policies and click on add by inline policy and under services we have to select EC2 we can search or choose directly. Ofter selecting EC2 we have to Search for Volume under action allowed tab and we have to choose which actions we want to create. Here I selected modify Volume and Describe Volume.
4. When ever we create EBS volume y Default it will create GP2 but by Automatically we are converting that EBS volume into GP3. So for that we Should have Describe Volume Action permission.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/on8rby4uk77ckbn1f81m.png)



5. Finally we can click on create policy and enter name of the policy and create.
6. Now under the perticular lambda IAM role now we can see 2 permission out of those one is for cloudwatch and one more for EBS volume.


**Create cloudwatch RULE:**


1. In aws console search for cloudwatch. Under watch left side search for Events and under events click on Rules and create Rule.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kseisw7ml3nl5zkkc4i6.png)


2. Here under Service name enter ec2 and event type select ebs volume notification then select specific event what ever we want. Then select any ARN (newly created Volume ARN we don’t know). 
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tv7r10p280am8cjwnwwb.png)




3. Then click on add target on the right hand side then select lambda then choose lambda what ever we created earlier then create the target.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ka9pjrvpdao1cnutguxh.png)


 
4. Now finally click on create Rule. Enter the name of the Rule then create successfully.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41o7abcxr70u5segr8kq.png)




**Creating a new Volume:**


1. Under aws console search for ec2 and left side search for volume then click on create a volume by default the volume will create GP2.
2. So ofter creating this volume our lambda will trigger we can see that In cloudwatch logs groups and check the latest log group.
3. Cloudwatch will give data to lamda in json format.
4. Our lambda have code to convert GP2 volume into GP3 volume that’s why what ever we created GP2 volume that will convert into GP3 volume.
5. If it is converting we have check cloudwatch logs under cloudwatch log groups and check the latest and search for any ERROR message.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v8i7nzpx2waiogjzd5p2.png)



6. Based on the ERROR message we have to check and try to figure it out.
7. Even if we create create new Ec2 also by default t2.micro will create GP2 voulme so if we create ec2 instance automatically our GP2 Volume will convert it into GP3.