Skip to content

Instantly share code, notes, and snippets.

@reiz
Created August 13, 2015 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save reiz/1df0d5284e9c3e796688 to your computer and use it in GitHub Desktop.
Save reiz/1df0d5284e9c3e796688 to your computer and use it in GitHub Desktop.
Spin up a EC2 instance with Ansible, run a job and terminate the instance after job is done.
---
- name: Create new instance
hosts: localhost
connection: local
remote_user: ubuntu
gather_facts: false
vars:
keypair: "id_rsa.pub"
instance_type: t2.medium
security_group: train
image: ami-47a23a30
region: eu-west-1
tasks:
- name: Launch instance
ec2: keypair={{keypair}} group={{security_group}}
instance_type={{instance_type}} image={{image}}
region={{region}} wait=true count=1
register: ec2
- name: Add new instances to host group
add_host: hostname={{item.public_ip}} groupname=prediction_train_server
with_items: ec2.instances
- name: Wait for the instances to boot by checking the ssh port
wait_for: host={{item.public_dns_name}} port=22 delay=60 timeout=320 state=started
with_items: ec2.instances
- hosts: prediction_train_server
user: ubuntu
sudo: true
roles:
- git
- docker
- docker_prediction_train_build
- docker_prediction_train_run
- name: Terminate instances
hosts: localhost
connection: local
tasks:
- name: Terminate instances that were previously launched
ec2:
region: 'eu-west-1'
state: 'absent'
instance_ids: '{{ ec2.instance_ids }}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment