0% found this document useful (0 votes)
41 views17 pages

Automate Start-Stop EC2 Instances Using Lambda

The document outlines a project to automate the start and stop of Amazon EC2 instances using AWS Lambda and EventBridge to optimize resource usage and reduce costs. It provides step-by-step instructions for creating IAM roles, Lambda functions, and scheduling rules to manage EC2 instances based on specific time triggers. The end result is a serverless solution that activates instances at 9 AM UTC and deactivates them at 6 PM UTC, ensuring efficient resource management.

Uploaded by

ralagarasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views17 pages

Automate Start-Stop EC2 Instances Using Lambda

The document outlines a project to automate the start and stop of Amazon EC2 instances using AWS Lambda and EventBridge to optimize resource usage and reduce costs. It provides step-by-step instructions for creating IAM roles, Lambda functions, and scheduling rules to manage EC2 instances based on specific time triggers. The end result is a serverless solution that activates instances at 9 AM UTC and deactivates them at 6 PM UTC, ensuring efficient resource management.

Uploaded by

ralagarasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Name: Ashish Kumbhojkar

Automate Start-Stop EC2 Instances Using


Lambda
Instructions: In cloud computing, efficient resource management is essential to reduce
unnecessary costs and optimize infrastructure usage. One of the most common issues
organizations face is running Amazon EC2 (Elastic Compute Cloud) instances continuously—
even when they are not in use. This can lead to significant charges, especially in development or
testing environments that only need to be available during working hours.
This project, "Automate Start-Stop of EC2 Instances Using AWS Lambda", aims to solve
this problem by leveraging serverless automation. By using AWS Lambda, Amazon
EventBridge (formerly CloudWatch Events), and EC2 instance tagging, we create a system
that automatically starts EC2 instances at the beginning of the day and stops them at the end.
This helps to ensure that compute resources are only active when required, leading to substantial
cost savings.

The project demonstrates:


• How to schedule Lambda functions to run at specific times,
• How to use instance tags to control which EC2 instances should be managed,
• And how to apply IAM roles for secure access control.
Name: Ashish Kumbhojkar

We need to launch an Instance

Details of the EC2 instance.


Name: Ashish Kumbhojkar

Create an IAM Role for Lambda

Your Lambda functions will need permission to manage EC2 instances.

1. Go to IAM > Roles > Create Role

2. Choose Trusted Entity: Lambda

3. Attach the following inline policy:


Name: Ashish Kumbhojkar

Step 3: Create the Start Lambda Function

1. Go to Lambda > Create Function

2. Name: StartEC2Instances

3. Runtime: Python 3.x (e.g., Python 3.12)

4. Role: Use existing role → LambdaEC2StartStopRole


Name: Ashish Kumbhojkar
Name: Ashish Kumbhojkar
Name: Ashish Kumbhojkar

Create the Stop Lambda Function

Repeat the same process as above, but:


Name: Ashish Kumbhojkar

Name: StopEC2Instances
Name: Ashish Kumbhojkar
Name: Ashish Kumbhojkar
Name: Ashish Kumbhojkar

Create EventBridge (CloudWatch) Rules for Scheduling

You’ll now set a cron schedule to trigger each function at a specific time.

For Start:

1. Go to Amazon EventBridge > Rules > Create Rule

2. Name: StartEC2Schedule

3. Schedule: cron(0 9 * * ? *) (9 AM UTC daily)

4. Target: Lambda function → StartEC2Instances

For Stop:

1. Create another rule: StopEC2Schedule

2. Schedule: cron(0 18 * * ? *) (6 PM UTC daily)

3. Target: Lambda function → StopEC2Instances

You can adjust the time to your timezone. Note that AWS cron uses UTC.

Test and Monitor

• Go to the Lambda console → Select the function → Click "Test".

• Check CloudWatch Logs to confirm that the correct instances were started/stopped.

• You can also verify by looking at EC2 state changes in the EC2 Dashboard.

Optional: Enhancements

• Environment Variables: Store instance IDs or tag keys as env vars in Lambda.
Name: Ashish Kumbhojkar

• Slack or Email Alerts: Add SNS notifications to get alerts when instances are
started/stopped.

• Terraform/CDK: Automate the entire infrastructure as code.


Name: Ashish Kumbhojkar
Name: Ashish Kumbhojkar
Name: Ashish Kumbhojkar
Name: Ashish Kumbhojkar
Name: Ashish Kumbhojkar

By Following steps we will create a scheduled programming for EC2 to start and stop.

End Results

EC2 instances with the tag AutoStartStop = true will start at 9 AM UTC.

And will stop at 6 PM UTC.

Everything runs serverless and hands-free via AWS Lambda and EventBridge.

You might also like