Stop automatically your AWS dev environment on non-working hours

And save money

If you are a developer or a cloud architect it maybe happened you to forget you dev/test environment running during non-working hours. It is not a big problem while you are working with micro instances, but what if you’re testing a solution that needs to run on bigger instances?

Recently in my company we’ve been testing a big oracle solution on d2.8xlarge instances. For some reason, maybe just a distraction, we forgot to stop instances and… BAM… 500$ spent in few days, what a shame!!!
We probably didn’t care about the most simple rule:

If something wrong can happen, it will surely happen.
Prevention is always the answer!

That’s why (and even because I’m too lazy to check manually) I decided to write a very simple lambda function that runs every day after we leave the office and stops all running instances. In our case we needed to stop all the instances of the account running on a single region, but it can obviously be improved by adding tags, or regions!

  • First step, the code

You can download the following code at https://github.com/gcis/stop-instances-lambda

  • Second step, the lambda policy (ensure lambda has the right role to interact with EC2)
  • Third step, add a cloudwatch event that triggers your lambda function (as shown below)

That’s it. 
Now you can sleep peacefully, without the fear of spending thousand dollars!

I leave with a little quote I love and that always inspires me to automate everything…

I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it. — Bill Gates
Like what you read? Give Gabriel a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.