Amazon start and stop
This is a sample "script" to schedule start and stop Amazon EC2 instances based on a date time range, i.e. start at 6AM and stop at 0AM.
Have a look at AWS Calculator to see how much you can save by stopping unsed instances from 0AM to 6AM (save 25%) or other date time ranges.
Usage
Checkout amazon_start_stop.
$ git clone git@github.com:phstc/amazon_start_stop.git
$ cd amazon_start_stopEdit bin/amazon_start_stop and set the constants START_HOUR and STOP_HOUR as you need.
# bin/amazon_start_stop
START_HOUR = 6 # Start 6AM
STOP_HOUR = 0 # Stop 0AM (midnight)Heroku
- Create a Heroku account if don't have one and install the Heroku Toolbelt.
- Create a new app.
$ heroku apps:createDeploy
$ git push heroku masterAWS Credentials
Configure your AWS Credentials using Heroku config vars.
$ heroku config:set AWS_ACCESS_KEY_ID=
$ heroku config:set AWS_SECRET_ACCESS_KEY=
# The instance id to start and stop i.e. i-1a1aa111
$ heroku config:set AWS_INSTANCE_ID=
# Leave blank if you don't need to associate an Elastic IP
# (everytime you stop an instance Amazon dissociates Elastic IPs)
$ heroku config:set AWS_ELASTIC_IP=In development create an .env.
# .env
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_INSTANCE_ID=
AWS_ELASTIC_IP=Heroku Scheduler
Add and configure Heroku Scheduler add-on standard (free add-on).
$ heroku addons:add scheduler:standard
$ heroku addons:open schedulerIn the Scheduler Dashboard add a new Scheduler.
| TASK | DYNO SIZE | FREQUENCY |
|---|---|---|
| bin/amazon_start_stop | 1x | Every 10 minutes |
Manual execution
In production.
$ heroku run bundle exec ruby bin/amazon_start_stop
# to check the script output
$ heroku logsIn development.
$ foreman start