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.
$ git clone git@github.com:phstc/amazon_start_stop.git
$ cd amazon_start_stop
- Create a Heroku account if don't have one and install the Heroku Toolbelt.
- Create a new app.
$ heroku apps:create
$ git push heroku master
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, e.g. i-1a1aa111
$ heroku config:set AWS_INSTANCE_ID=
# The region where your instance resides, e.g. eu-west-1
$ heroku config:set EC2_REGION=
# 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=
# The start and stop times (in 24h clock hours, UTC timezone) are configured per config vars as well:
$ heroku config:set START_HOUR=
$ heroku config:set STOP_HOUR=
In development create an .env
.
# .env
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_INSTANCE_ID=
EC2_REGION=
AWS_ELASTIC_IP=
START_HOUR=
STOP_HOUR=
See env.example for an example.
Add and configure Heroku Scheduler add-on standard (free add-on).
$ heroku addons:add scheduler:standard
$ heroku addons:open scheduler
In the Scheduler Dashboard add a new Scheduler.
TASK | DYNO SIZE | FREQUENCY |
---|---|---|
bin/amazon_start_stop | 1x | Every 10 minutes |
In production.
$ heroku run bundle exec ruby bin/amazon_start_stop
# to check the script output
$ heroku logs
In development.
$ foreman start