15)How to automate EC2 Instance type using Lambda & Event bridge…

Venkatramanan C S
5 min readJul 1, 2024

--

Today we will see how to automate EC2 instance type from t3.micro to t3.large using Lamba which is a Serverless service and Event Bridge service where we can add cron expression for scheduling purposes….

AWS Services Involved: EC2,Lambda,Eventbridge..

1-Login to AWS Console using Root username and Password

2-Search for EC2 instance in search box and enter the EC2 console…

3-We first need an EC2 instance only then we can convert its Instance type… So in my case I already have a EC2 instance running in my console, but incase if you don’t have one you have create a new ec2 instance..

(If you are new to this topic you can go through this link on how to deploy a EC2 instance …https://medium.com/aws-tip/2-aws-amazon-web-services-basic-services-ec2-elastic-cloud-compute-create-deploy-part-2-c2716fcbe510

So as I mentioned I already have my EC2 instance running….

4-Next thing we have to do lets search Lambda and get inside Lambda console

5-Here we are gonna create a new function..

6-Click on “Create function”.

  • Choose “Author from scratch”.
  • Provide a name for the function (e.g., ConvertInstancetype ).
  • Select a runtime (e.g., Python 3.12).

and then click on create function..

7-So once you have deployed a function in lambda you have to add code based on your requirement based on what platfomr you have selected…here we will go with python..

Edit lambda_function and add the code given below…

Python Code template-to change instance type automatically…

import boto3

def lambda_handler(event, context):
ec2 = boto3.client(‘ec2’)

# Specify the instance ID and desired instance type
instance_id = ‘i-1234567890abcdef0’
desired_instance_type = ‘t2.large’

# Stop the instance
ec2.stop_instances(InstanceIds=[instance_id])

# Wait until the instance is stopped
waiter = ec2.get_waiter(‘instance_stopped’)
waiter.wait(InstanceIds=[instance_id])

# Change the instance type
ec2.modify_instance_attribute(InstanceId=instance_id, Attribute=’instanceType’, Value=desired_instance_type)

# Start the instance
ec2.start_instances(InstanceIds=[instance_id])

return {
‘statusCode’: 200,
‘body’: f’Instance {instance_id} has been modified to {desired_instance_type} and restarted.’
}

8-So here we have added the above mentioned python code and all we have to do configure and change Instance ID and desired Instance type which we want the instance to be changed..

now deploy the code…once the deployment is successfull we can test the code….

9-So now we have to make sure that our lambda function has all the required permissions to execute certain actions like Starting ,Stopping, modifying & changing instance type… for this we need to attach policy to a role and attach the role to lambda function

IAM-Policy-Role-Lambda function-EC2

So now search for configuration option and select permission you can see a role attached to the lambda function by default…click on the role and you will directed to IAM Console…

10-So now are inside Role-Console all we have to do is attach policy based on our requirement…

click Add permission and select create inline polices….

11-Here we have to attach the below policy…

{
“Version”: “2012–10–17”,
“Statement”: [
{
“Effect”: “Allow”,
“Action”: [
“ec2:StopInstances”,
“ec2:StartInstances”,
“ec2:ModifyInstanceAttribute”,
“ec2:DescribeInstances”
],
“Resource”: “*”
}
]
}

and click create policy….

12-So now review and create a new policy

click create policy

13-Now we have attached our new policy to the Role which is attached to the lambda function…

14-Now we are gonna go back to our lambda console and test the code.

15-When you test the code you have to create a new test event ,name and save it..

16-Now the instance type has changed to t3.large …

17-Now we have to configure Event Bridge with lambda function

18-We have to add a schedule details as shown below..

select Cron based schedule and add Cron based on the clients schedule…

So in this Cron its mentioned everyday from July 02 to July 11 2024 this lambda function will be triggered once…and the instance type will be changed to t3.large…

19-Select target as lambda function and select the existing lambda function which we have created…

20-Now review and create schedule.

21-Once the Cron Schedule is created and this is configured with lambda function which will change the EC2 instance type automatically from t3.micro to t3.large…

--

--

Venkatramanan C S

Myself Venkat I have been working as a Cloud & DevOps Engineer for the past 3 years focusing on Cloud & DevOps Tech. AWS SAA-03 Certified Solution Architect.