/ec2_instance_control

Ansible: AWS EC2 instance control scripts

MIT LicenseMIT

Ansible: AWS EC2 instance control scripts

Scripts for starting and stopping AWS EC2 instances. You can place these scripts in your cron and manage instance usage during weekends for example.

Requirements

  • Make scripts executables
chmod +x ec2_start_instances.yml
chmod +x ec2_stop_instances.yml
virtualenv pyenv
pip install -r requirements.txt
  • Rename and complete variable scripts
mv vars/aws_config-sample.yml vars/aws_config.yml
mv vars/ec2_instances-sample.yml vars/ec2_instances.yml

Variables

AWS credentials

Variable file

region: eu-west-1
  • Instance IDs to control:
instance_ids:
  - i-XXXXXX
  - i-XXXXXX
  • Project name for control over vars/ec2_instances<project>.yml
project: '_myProject'

Running

  • Start instances
./ec2_start_instances.yml
# or
./ec2_start_instances.yml -e project=<project>
  • Stop instances
./ec2_stop_instances.yml
# or
./ec2_stop_instances.yml -e project=<project>

Examples

Edit /etc/crontab and place the following lines for auto stop-start on the weekends. With vars/ec2_instances_myProject.yml:

0 1 15 ? * FRI *  ./ec2_stop_instances.yml -e project=_myProject
0 1 9 ? * MON *   ./ec2_start_instances.yml -e project=_myProject

Author

  • Carles San Agustin - carlessanagustin.com
  • MIT License