By Wolfgang Unger
Many customers spend more money on AWS as actually neccessary
There are many possibilites to reduce your AWS bill, please take a look in out Blog
Cost optimization strategies | How to save money on AWS
In this blog we will only focus on one aspect of the list:
Reduce usage hours
If you have read the blog about cost savings you know by know what this is all about.
Stop your EC2 and RDS instances if they are not in use.
This might not be possible for all of your instances, some might have to run 24/7, like a webserver,
but for sure you might have some servers which are not used and accessed some hours a day.
For example out of the business hours between 7pm and 7am.
The calculation is simple. If you stop your instances 12h each day, you will save 50% of your cost.
If you have identified these instances, as mentioned can be EC2 and also RDS instances, you
can define the time you want to run the machines and not to run.
This might differ from Stage to Stage also, for example you might want to stop your servers and DEV and QA at night, but of
course not on PROD.
Wouldn't it be great to have an automation for starting and stopping instances you can select with for example a Tag ?
Start and Stop the instances automatically by a Lambda Function
Please have a look in my Github Repo where you can find the Lambda to schedule your instances.
Cost savings lambda | Github Wolfgang Unger
The CloudFormation Template in the Repo will do most of the work for you.
All you have to do is deploy it in your account(s) and tag the instances which should be scheduled ( started and stopped).
Get started and deploy the CloudFormation Template Lambda-Instance-Start-Stop.yaml:
See more infos about the params in the Readme of the Repo.
The CloudFormation Template will create 2 Lambdas and 2 EventRules.
The Rules are scheduled by the cronjob expression in the Cloudformation Template, default is
Start: cron(0 6 ? * 2-6 *) # UTC time, which is mo-fr 6am
Stop: cron(0 18 ? * 2-6 *) # UTC time, which is mo-fr 6pm, on weekends instances will remain stopped until next start event on monday
Change the cronjob expression for the required behaviour
The Lambdas will search for instances & autoscaling groups by tag, the following Tag is required to identify
an instance to schedule:
Auto-Start : true
You can tag your instances manually or of course better you are already using a IaC Tool, like CloudFormation, CDK or Terraform
and add the tags for the selected instances in there.
If you don't yet use IaC please do, this blog will help to decide which one you can use:
IaC - infrastructure as Code
Supported Instances to schedule are:
EC2 Instances in a autoscaling group The autoscaling group must be tagged, not the instance ( stopping the instance would only cause an immediatelly restart by the autoscaling group)
autoscaling params MinSize, and DesiredCapacity will be set to either 1 or 0
Note: For autoscaling groups this solutions currently only supports desired count = 1
If your desired count is != 1 don't tag them.
To support different values the lambda logic must be changed, for example storing the current value in a parameter in parameter store
before stopping the instance and reading this value before starting/reseting the old value.
Now you can go ahead in install the Lambdas with the CFN Template and tag your instances
I hope you can reduce your costs quite a lot with this simple approach
Please give us some feedback on how much you could reduce your AWS bill with this little Lambda Function.