Step By Step guide to auto start ec2 instance using lambda function on amazon web service

Hello,I hope you’re doing well.

In this blog we are discussing how to auto start ec2 instance using lambda function on aws console.Lambda is is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services.Using lambda function, we can easily schedule EC2 instances’ start and stop time to save AWS cloud operational cost.We need to setup a trigger on lambda function to schedule auto start ec2 instances.

There are some steps to auto start ec2 instances using lambda function:

Launch a EC2 instance

  • Login to AWS console management.
  • Go to AWS Services & select Compute.
  • Click on EC2.
  • Click on the following below URL for launch a EC2 instance on aws console.

https://www.hackerxone.com/2021/05/21/how-create-ec2-instance-amazon-web-serviceaws/

  • Once Instance is launched successfully.

Manually Stop /Auto stop the running instances

  • After doing some work on the current instance, you want to stop running instances via manually or using lambda function.

Manually Stop the instance

  • Go to EC2 instance.
  • Click on Action –>Instance state –>select stop.
  • Now Instance is stopping.

Stop the instance using Lambda function

  • Go to the following below link for how to auto stop running ec2 instance:

https://www.hackerxone.com/2022/09/30/step-by-step-guide-to-auto-stop-ec2-instance-using-lambda-function-on-amazon-web-service/

Create a Lambda function

  • Go to Services & select Compute.
  • Click on Lambda.
  • Click on the following below URL for create a Lambda function.

https://www.hackerxone.com/2021/05/30/how-create-lambda-function-amazon-web-serviceaws/

  • Lambda function is created.

Attach a IAM policy on Lambda function Role

“AmazonEC2FullAccess”

  • To attach the following IAM policy on Lambda function role.
  • Here is the output.

Test Python code on lambda function for Auto Start EC2 instance

  • Go to Created lambda function.

  • Under the test folder you will find a lambda_function.py file.Click in that file.

  • Remove the existing code.

  • Add the New code.

import json
import boto3
ec2 = boto3.resource('ec2', region_name='eu-west-3')
def lambda_handler(event, context):
instances = ec2.instances.filter(Filters=[
{
'Name': 'instance-state-name',
'Values': ['stopped']
},
])
for instance in instances:
id=instance.id
ec2.instances.filter(InstanceIds=[id]).start()
print("Instance ID is started :- "+instance.id)
return "success"

  • Click on Deploy.

  • Code is deployed.

Create a Event

A Event is a JSON object that mocks the structure of request emitted by AWS service to invoke a lambda function.

  • Next ,Click on Test.

  • Select event action “create a new event”.
  • Provide a Event Name.

  • Click on Save.

  • The Event was successfully saved.

Configure the Lambda function

  • Go to Configuration.

  • In the General configuration, click Edit.

  • By default the timeout is 3 sec.

  • Increase the timeout and click on Save.

  • Lambda function has been updated.

Test the code

  • Click on code option & click on Test.

  • Now, code is executed successfully.

Check the Instance status

  • Go to EC2 instance.
  • Here, the instance status is initializing.

Add a trigger on lambda function

  • Click on Add Trigger.

  • Select a source “EventBridge (CloudWatch Events).

  • Click on “create a new rule”.

  • Provide a rule name & description.
  • Select Rule type “Schedule Expression”
  • In Schedule repression,mention — cron(0 10 ? * * *) – Everyday at 10:00 AM UTC.
  • In the mentioned Region on code,all EC2 instances will start automatically daily at 10:00 a.m. UTC.
  • Click on Add.

  • The Trigger was successfully added to lambda function.

Leave a Reply