Akamas Docs
3.3.0
Ask or search…
K
Links
Comment on page

Leveraging Ansible to automate AWS instance management

Ansible is an open-source software automation tool suited for instance configuration and provisioning, enabling an Infrastructure as Code approach to the Cloud. In this page we provide a set of ansible-playbooks templates to perform the most common task to tune EC2 instance types with Akamas, such as:
  • EC2 instance creation
  • EC2 instance termination
  • EC2 instance resizing
Refer to the Ansible documentation and to the Ansible ec2 module for more details, and make sure to check the concepts behind inventory management to build a robust automation.
The orchestrator requires access to an account or role linked to the correct policies; this requires managing AWS Policies and having access to the required security groups.

Instance Creation

The following example playbook provisions an EC2 instance using the latest Ubuntu 18-04 LTS image and then waits for it to be available. The playbook requires the following set of arguments:
  • key: the name of the SSH key pair to use
  • Name: the instance name
  • security_group: the name of the AWS security group
  • region: the selected AWS region
You can update the ec2_ami_info task to query for a different AMI family or specify directly the id under ec2.image.
When executing the script we must assign the following arguments as extra-vars:
  • intance_type: type of instance to provision
  • volume_size: size of the attached volume
1
# Launch an ubuntu instance and wait for ssh
2
3
- name: Create an instance request
4
hosts: localhost
5
gather_facts: False
6
7
tasks:
8
- name: query api
9
ec2_ami_info:
10
filters:
11
name: "ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"
12
owner-id: "099720109477" # Canonical Group Limited
13
register: amis
14
- name: sort by creation date
15
set_fact:
16
sorted_amis: "{{ amis.images | sort(attribute='creation_date') }}"
17
- name: get latest
18
set_fact:
19
latest_ami: "{{ sorted_amis | last }}"
20
21
- name: Launch instance
22
ec2:
23
key_name: "{{ key }}"
24
instance_type: "{{ instance_type | default('m5.xlarge') }}"
25
group:
26
- <your-security-groups>
27
image: "{{ latest_ami.image_id }}"
28
count: "{{ count | default('1') }}"
29
wait: yes
30
wait_timeout: 500
31
region: "{{ region }}"
32
spot_wait_timeout: 600
33
instance_initiated_shutdown_behavior: terminate
34
ebs_optimized: yes
35
volumes:
36
- device_name: /dev/sda1
37
volume_type: gp2
38
volume_size: "{{ volume_size | default('20') }}"
39
delete_on_termination: yes
40
instance_tags:
41
Name: "{{ Name }}"
42
CNAME: "{{ Name }}.<your-domain>"
43
register: ec2
44
45
- name: Wait for SSH to come up
46
wait_for:
47
host: "{{ item.public_dns_name }}"
48
port: 22
49
delay: 60
50
timeout: 320
51
state: started
52
with_items: "{{ ec2.instances }}"
To apply the EC2 parameters from the AWS Optimization Pack selected by the Akamas engine you can generate the playbook arguments through a template like the following one, where ec2 is the name of the component:
ansible-playbook -i inventory --extra-vars "instance_type=${ec2.aws_ec2_instance_type}.${ec2.aws_ec2_instance_size}" provision.yaml

Instance Termination

The following playbook terminates all instances with the specified name (or any other tag). It requires the following arguments:
  • instance_name: the name instances name
  • region: the selected AWS region
1
# Terminate an aws instance
2
3
- name: Terminate instance
4
hosts: localhost
5
gather_facts: False
6
tasks:
7
- name: retrieve instance info
8
ec2_instance_info:
9
filters:
10
"tag:Name": "{{ instance_name }}"
11
register: ec2
12
13
- name: terminate the instance
14
ec2:
15
state: absent
16
instance_ids:
17
- "{{ item.instance_id }}"
18
region: "{{ region }}"
19
with_items: "{{ ec2.instances }}"

Instance Resizing

Instance resizing is a little trickier to deploy as it requires you to install AWS CLI and setup the required credentials. The following playbook provides a simple way to stop, update and restart your instance: it is intended as a building block for more elaborate workflows.
It makes use of a list of arguments:
  • instance_name: your instance name
  • region: the selected AWS region
For a successful workflow it requires:
  • The instance to exist
  • The instance to be unique
1
# Change instance type, requires AWS CLI
2
3
- name: Resize the instance
4
hosts: localhost
5
gather_facts: no
6
connection: local
7
tasks:
8
- name: save instance info
9
ec2_instance_info:
10
filters:
11
"tag:Name": "{{ instance_name }}"
12
register: ec2
13
- name: stop the instance
14
ec2:
15
region: "{{ region | default('us-east-2') }}"
16
state: stopped
17
instance_ids:
18
- "{{ ec2.instances[0].instance_id }}"
19
instance_type: "{{ ec2.instances[0].instance_type }}"
20
wait: True
21
- name: Change the instances ec2 type
22
shell: >
23
aws ec2 modify-instance-attribute --instance-id "{{ ec2.instances[0].instance_id }}"
24
--instance-type "{{ new_instance_type }}"
25
delegate_to: localhost
26
- name: restart the instance
27
ec2:
28
region: "{{ region }}"
29
state: running
30
instance_ids:
31
- "{{ ec2.instances[0].instance_id }}"
32
wait: True
33
register: ec2
34
- name: wait for SSH to come up
35
wait_for:
36
host: "{{ item.public_dns_name }}"
37
port: 22
38
delay: 60
39
timeout: 500
40
state: started
41
with_items: "{{ ec2.instances }}"
To apply the EC2 parameters from the AWS Optimization Pack selected by the Akamas engine you can generate the playbook arguments through a template like the following, where ec2 is the name of the component:
ansible-playbook -i inventory --extra-vars "new_instance_type=${ec2.aws_ec2_instance_type}.${ec2.aws_ec2_instance_size}" resize.yaml