A command line tool to startup and shutdown AWS EC2 instances based on start and stop times defined in instance tags.
EC2 instances at hCentive are tagged with the following resource tags -
- Name (required) - name of the instance e.g wem-cvtdev.demo.hcentive.com
- cost-center (required) - cost center for the instance e.g. wem
- starttime - daily scheduled instance startup time in UTC e.g. 03:30:00
- stoptime - daily scheduled instance shutdown time in UTC e.g. 15:30:00
- owner - active directory username or email address of the instance owner e.g. jagdeep.singh
- stack - application stack for the server e.g. qa
- created - date of instance creation e.g. 22-07-2013
- expires - date of instance expiry/retirement e.g. 31-01-2014
The starttime and stoptime tags define instance start and stop times. If your instances use a different tag nomenclature, you can update tag keys in ec2.yml. Keep in mind the tool requires at least the Name and cost-center tags.
System requirements -
- Ruby 2.1.1
- The following packages must be installed on the system -
- build-essential
- zlib1g-dev
- openssl
- libssl-dev
- libopenssl-ruby
ec2-startup-shutdown requires the following environment variables to be present:
export AWS_ACCESS_KEY_ID='...'
export AWS_SECRET_ACCESS_KEY='...'
export AWS_REGION='us-east-1'
If this utility is run from an EC2 instance launched with an IAM role, the AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
are picked up from instance metadata.
ec2-startup-shutdown utility has the following commands. Type help to get the list of commands.
$ ec2 help
Commands:
ec2 help [COMMAND] # Describe available commands or one specific command
ec2 list COST-CENTER STACK -c, --cost-center=one two three # Describe instances, with state, for a cost center (e.g. techops, shared-services, phix) and stack (e.g. dev, qa, sit)
ec2 audit_tags COST-CENTER STACK -c, --cost-center=one two three # Audit instance tags; add missing tags. Expects at least the 'cost-center' tag
ec2 start_instances COST-CENTER STACK -c, --cost-center=one two three # Start stopped instances marked to be started at the hour for a stack e.g. dev, QA, SIT
ec2 stop_instances COST-CENTER STACK -c, --cost-center=one two three # Stop running instances marked to be stopped at the hour for a stack e.g. dev, QA, SIT
List instances, with state, for a cost center (e.g. techops, shared-services, phix) and stack (e.g. dev, qa, sit).
ec2 list COST-CENTER STACK -c, --cost-center=one two three
-c, --cost-center=one two three # cost-center - space separated list of cost center for the instances
-s, [--stack=one two three] # stack - space separated list of stack for the instances
Audit instance tags; add missing tags. Expects at least the 'cost-center' tag
ec2 audit_tags COST-CENTER STACK -c, --cost-center=one two three
-c, --cost-center=one two three # cost-center - space separated list of cost center for the instances
-s, [--stack=one two three] # stack - space separated list of stack for the instances
-d, [--dry-run] # dry run - does not fix tags; default: false
-y, [--no-prompt] # no-prompt - automatic yes to prompts; default: false
Stop running instances marked to be stopped at the hour for a stack e.g. dev, QA, SIT
ec2 stop_instances COST-CENTER STACK -c, --cost-center=one two three
-c, --cost-center=one two three # cost-center - space separated list of cost center for the instances
-s, [--stack=one two three] # stack - space separated list of stack for the instances
-d, [--dry-run] # dry run - does not stop instances; default: false
-n, [--notify-owner] # notify - notify owner before stopping instance; default: true
-y, [--no-prompt] # no-prompt - automatic yes to prompts; default: false
Start stopped instances marked to be started at the hour for a stack e.g. dev, QA, SIT.
ec2 start_instances COST-CENTER STACK -c, --cost-center=one two three
-c, --cost-center=one two three # cost-center - space separated list of cost center for the instances
-s, [--stack=one two three] # stack - space separated list of stack for the instances
-d, [--dry-run] # dry run - does not start instances; default: false
-n, [--notify-owner] # notify - notify owner before starting instance; default: true
-y, [--no-prompt] # no-prompt - automatic yes to prompts; default: false