0% found this document useful (0 votes)
3 views20 pages

AWS Cost Optimization - Automate EC2, RDS & Redis Start - Stop

The document provides a step-by-step guide on automating the start and stop of AWS EC2, RDS, and Redis resources using AWS Lambda to optimize costs during off-hours. By implementing this automation, users can potentially save up to 50% on their monthly cloud expenses by minimizing idle resource consumption. The guide includes detailed instructions, code snippets, and best practices for setting up the automation process effectively.

Uploaded by

upasanasingh2055
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)
3 views20 pages

AWS Cost Optimization - Automate EC2, RDS & Redis Start - Stop

The document provides a step-by-step guide on automating the start and stop of AWS EC2, RDS, and Redis resources using AWS Lambda to optimize costs during off-hours. By implementing this automation, users can potentially save up to 50% on their monthly cloud expenses by minimizing idle resource consumption. The guide includes detailed instructions, code snippets, and best practices for setting up the automation process effectively.

Uploaded by

upasanasingh2055
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/ 20

4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

Upasana Singh's Blog

Follow

AWS Cost Optimization: Automating


EC2, RDS, and Redis Start/Stop with
Lambda
Step-by-Step Guide to Automating EC2, RDS, and
Redis Start/Stop During Off-Hours with AWS Lambda

Upasana Singh

Dec 30, 2024 · 4 min read

1. Introduction

In cloud environments, managing costs is crucial to maintaining efficiency and


profitability. With AWS, services like EC2, RDS, and Redis can run 24/7, incurring
charges even when idle. By leveraging AWS Lambda for automation, you can
optimize these resources to start and stop during off-hours, reducing
unnecessary costs.

2. Why Automate Resource Shutdowns?


1

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 1/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

EC2, RDS, and Redis instances continue to consume resources even when not
in use, leading to higher costs. Automating their shutdown during off-hours
helps minimize idle resource consumption, potentially saving up to 50% on
monthly cloud expenses. For example, stopping EC2 instances during non-peak
times or pausing RDS databases overnight can significantly reduce your AWS
bill.

3. Step-by-Step Implementation

Below is a detailed step-by-step guide, along with screenshots, to help you


implement the automation process for EC2, RDS, and Redis start/stop during
off-hours using AWS Lambda. Follow each stage carefully to ensure a seamless
setup.

Step1: In the AWS Management Console, navigate to Lambda Service and create
lambda function and edit configuration section to increase Timeout value and
create new policy, attach to created existing lambda function role as shown in
below attached screenshots.

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 2/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 3/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

COPY

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 4/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop
"Effect": "Allow",
"Action": [
"ec2:StartInstances",
"ec2:StopInstances",
"ec2:DescribeInstances",
"rds:DescribeDBInstances",
"rds:StartDBInstance",
"rds:StopDBInstance",
"elasticache:CreateCacheCluster",
"elasticache:DeleteCacheCluster",
"elasticache:DescribeCacheClusters"
],
"Resource": "*"
}
]
}

Note: If you’re using tagged resources, you can narrow the "Resource": "*"
field to specific ARNs for better security. For example, replace "*" with the
ARNs of your EC2, RDS, or ElastiCache resources.

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 5/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

In the search bar, look for the policy you just created. For example, in my case, it
is named lambda-starter-stopper-policy.

Step2: After completing Step 1, click on the newly created Lambda function and
enter the script for the EC2, RDS, and Redis instances that you want to automate

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 6/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

for starting and stopping. Use the exact/correct name, instance-id and other
required details.

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 7/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

After above configuration is done, add script to lambda function starter you
created earlier like shown below.

COPY

import boto3
region = 'ap-south-1' # replace with your actual region
instances = ['i-001g234456ef'] # replace with your actual instance-id
db_instances = ['frm-test'] # replace with your actual db identifier na

ec2 = boto3.client('ec2', region_name=region)


rds = boto3.client('rds', region_name=region)
elasticache = boto3.client('elasticache', region_name=region)

def lambda_handler(event, context):

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 8/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop
ec2.start_instances(InstanceIds=instances)
for db_instance in db_instances:
rds.start_db_instance(
DBInstanceIdentifier = db_instance
)
print('start your instances: ' + str(instances))
response = elasticache.create_replication_group(
ReplicationGroupId='frm-test', # replace with your actual redis
ReplicationGroupDescription='frm-test', # you can keep the name
NumCacheClusters=1,
CacheNodeType='cache.t3.micro', # replace with your cache node
Engine='redis',
EngineVersion='7.1', # replace with your engine-version
CacheSubnetGroupName='subnet-group-redis-project', # replace wi
SecurityGroupIds=[
'sg-00946312345', # replace with your security-group id use
],
PreferredMaintenanceWindow='sun:01:00-sun:02:00', # replace wit
Port=6379, # replace with your actual redis port
AutoMinorVersionUpgrade=True
)

Step 3: After completing Step 2, follow the same approach to create a Lambda
function for stopping resources. Refer to the screenshots and script for
guidance.

COPY

import json
import json
import boto3

def lambda_handler(event, context):


ec2 = boto3.client('ec2')
rds = boto3.client('rds')

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 9/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop
elasticache = boto3.client('elasticache')

# Stop EC2 instances


ec2_instances = ['i-001g234456ef'] # replace with your lambda start
for instance_id in ec2_instances:
ec2.stop_instances(InstanceIds=[instance_id])

# Stop RDS instances


rds_instances = ['frm-test'] # replace with your lambda starter men
for instance_id in rds_instances:
rds.stop_db_instance(DBInstanceIdentifier=instance_id)

# Stop ElastiCache/Redis clusters


elasticache_clusters = ['frm-test'] # replace with your lambda sta
for cluster_id in elasticache_clusters:
elasticache.delete_replication_group(ReplicationGroupId=cluste

return {
'statusCode': 200,
'body': json.dumps('Resources stopped successfully')
}

Step4: Create AWS EventBridge scheduler rule for lambda starter/stopper


automation.

STOPPER REFERENCE:

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 10/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 11/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 12/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 13/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

STARTER REFERENCE:

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 14/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 15/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 16/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 17/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

NOTE: After creating the scheduler, attach it to the respective


starter/stopper Lambda functions for automation. Ensure the scheduler is
properly linked to the functions.

Conclusion
By following these steps and creating the AWS EventBridge scheduler rule,
you’ve successfully automated the starting and stopping of your resources
during off-hours. This approach not only streamlines operations but also
significantly optimizes your AWS costs.

Ensure your scheduler rule is correctly set up by referring to the attached


screenshots. If you encounter any issues, revisit the steps or consult the official
AWS documentation for further assistance.

If this guide was helpful or if you have suggestions for improvement, feel free to
share your feedback. Together, we can continue building smarter and more
cost-efficient AWS solutions!

Subscribe to our newsletter


Read articles from directly inside your inbox. Subscribe to the
newsletter, and don't miss out.

Enter your email address SUBSCRIBE

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 18/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

AWS Cost Optimization Automation AWS Cost Optimization

AWS EC2 RDS Redis Cost Optimization aws lambda

AWS EventBridge AWS rds ec2 eventbridge Redis

AWS cost optimization best practices

MORE ARTICLES

Upasana Singh

Prevent Storage Issues: Automated EC2 Cleanup


Using AWS Systems Manager
Deploying services on AWS EC2 instances using Docker over time can
lead to high storage, memory, and…

©2025 Upasana Singh's Blog

Archive · Privacy policy · Terms

Powered by Hashnode - Build your developer hub.

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 19/20
4/8/25, 4:16 PM AWS Cost Optimization: Automate EC2, RDS & Redis Start/Stop

Start your blog Create docs

https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 20/20

You might also like