This is a very common questions asked by many companies.
How can we reduce our cloud costs?
Companies who want to reduce EC2 cost on AWS have several options available to them.
Use right AWS configurations
The reassignment of instances to either Amazon’s Spot Instances or Reserved Instances pricing plan rather than On Demand can reduce EC2 cost. This should be done if businesses have development projects that are not time-sensitive, or if businesses have the organisational foresight to plan up to three years ahead.
Switch it off when not required
Alternate ways to reduce AWS costs is to develop scheduling scripts – or asking developers to log into the AWS console each day – to manually switch off AWS instances when not required. The latter is not an ideal solution, as it’s counter-productive to automation, and manual on/off processes are subject to human error.
Remember when your parents told you to always switch off the lights before leaving the room – and how effective it was 😉
Schedule Scripts
The final alternative is the implementation of scheduling scripts to reduce EC2 cost. At LeanApps, we have written these scripts as Lambda Function. So far it has been the perfect solution and does not involve any disadvantage.
Usually, the working hours at any organization are from 10AM to 6PM, when all the development and testing happens. Same is at our organization and we have therefore scheduled our non-production instances to start at 9:30AM and stop at 9:00PM.
The following Lambda Script in Python 2.7, is to start the instances, and it is configured with a CloudWatch event scheduled according to the regular expression
00 4 ? * MON-FRI *
This regex schedules the script to run at 4:00AM GMT on weekdays of all weeks, all months of all year.
import boto3 region = ‘ap-south-1’ instances = [‘comma separated list of instance ids’]
def lambda_handler(event, context):
ec2 = boto3.client(‘ec2’, region_name=region)
ec2.start_instances(InstanceIds=instances) client = boto3.client(‘sns’) response = client.publish(
TopicArn=’arn:aws:sns:ap-south-1:mySlackNotifications’, Message=’All the EC2 instances that are scheduled by AWS Lambda have been started’, Subject=’LeanApps AWS servers have been started’ )
The following Lambda Script in Python 2.7, is to stop the instances, and it is configured with a CloudWatch event scheduled according to the regular expression
30 15 ? * MON-FRI *
This regex schedules the script to run at 3:30PM GMT on weekdays of all weeks, all months of all year.
import boto3 region = ‘ap-south-1’ instances = [‘comma separated list of instance ids’]
def lambda_handler(event, context):
ec2 = boto3.client(‘ec2’, region_name=region)
ec2.stop_instances(InstanceIds=instances) client = boto3.client(‘sns’) response = client.publish(
TopicArn=’arn:aws:sns:ap-south-1:mySlackNotifications’, Message=’All the EC2 instances that are scheduled by AWS Lambda have been stopped’, Subject=’LeanApps AWS servers have been stopped’ )
Slack Integration
Further, these functions call the SNS to send notifications on Slack.
Another thing that we have implemented to ease the life of our developers is to allow them to start/stop their instances during the STOP hours via a Slack Chat Bot.
Scrum masters or devs can start/stop instances whenever they need, simply with a Slack chat bot instead of signing into the console and starting and stopping.
Reduction in costs
By incorporating these things, we have reduced our AWS costs by approximately 30%. This data has been calculated by applying simple mathematics and not by using data from AWS Billing console, because since we have implemented this, we have provisioned a lot of on Demand Instances for our production and development use.