Pause/Stop/Start Elastic-Beanstalk
This post shall help you start and stop elastic beanstalk gracefully without terminating it and help you minimize the cost of its usage.
A little backdrop
While maintaining staging environment for www.theveu.com, I realized that the staging environment was used only for few hours in a day, to be specific only when we create a new build or hot fix patches that we need to test quickly.
But the Staging Instance which had a T2-large instance with load balancing using Elastic Beanstalk was running 24*7, which needless to say was something useless that I was doing. Let’s say, I was too busy with other stuff that I forgot to look at this way and it skipped my observation.
Figuring out a solution !
After closely monitoring the usage, I realized that we need to use the elastic beanstalk environment only when we need it and for the rest of the time we need to pause it but not terminate it.
But, elastic beanstalk, doesn’t have such option inbuilt, it only allows you to terminate the instance. Looking for the ideal solution, I found out that it has an option for scaling the instances where you can set minimum and maximum instances as per your need.
Let’s automate, because I am lazy !
Now, it would be very tedious task to ask the DevOps person to rebuild the staging environment every time you need to stop or start the environment.
Thus, I wrote a small script that automates it.
How to run the script?
$ ./start-stop-ebs.sh <min-number of instance> <max-number of instance>
where min and max are ≥0 (integer)
Note: To run the above script, you need to configure aws-cli and enter your credentials to have proper access to aws commands.
Hey, I forgot to put staging down!
Now, when I was done automating the start/pause/stop behavior of our staging environment, I began to observe another behavior of my team.
Developers after they are done testing forgot to shut down the environment which led it to keep running throughout the night, uselessly, which completely defeats the purpose of this activity.
Let’s cron-job the shit out it!
Cron is one thing which I feel is a life-saver for all the lazy developers like me to automate certain actions at a certain time when a certain condition meets.
Thankfully, elastic beanstalk configuration provides this easy setup in-built. Follow the steps below to set-it up.
- Go to your elastic beanstalk environment.
- Followed by configuration tab in the left sidebar.
- Click on capacity and then go at the bottom of it to find Time-based Scaling option.
- Then, click on Add Scheduled Action and follow the screenshots given below to add the cron-job. Time mentioned in the cron is UTC based, so adjust accordingly.
5. Finally, save the configuration and update the environment.
Dude, what the hell did we all do? TLDR; required !
- We automated the process of starting and stopping the instance.
- We setup a cron job which stops the environment and pause it at 7:30 pm when the office ends, so that if anyone forgets to stop it, the cron does it for us.
Shameless Plug 🤪:
Hey everyone!
I’m excited to announce the launch of my first book, “Building Serverless Applications on AWS: A Step-by-Step Guide !”
You can get my book in multiple formats, including Kindle, paperback (both in India and internationally), and Gumroad.
So, whether you prefer a physical book or a digital copy, you can choose the format that works best for you.
If you wish to buy it, please use the link below:
About the book : https://bit.ly/about-my-book
Kindle : https://bit.ly/buy-kindle-version
Paperback India : https://bit.ly/buy-paperback-version-india
Gumroad : https://bit.ly/buy-on-gumroad
Paperback International : https://bit.ly/buy-paperback-version-international
I’m excited to hear what you think!