AWS Cost Optimization - Automate EC2, RDS & Redis Start - Stop
AWS Cost Optimization - Automate EC2, RDS & Redis Start - Stop
Follow
Upasana Singh
1. Introduction
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
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
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
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')
return {
'statusCode': 200,
'body': json.dumps('Resources stopped successfully')
}
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
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.
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!
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
MORE ARTICLES
Upasana Singh
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
https://upasanasingh.hashnode.dev/aws-cost-optimization-automating-ec2-rds-and-redis-startstop-with-lambda 20/20