So you backup your EBS volumes, but do you actually test if you can recover them? How long does it take to restore EBS snapshot and bring your EC2 instance back to life? In this post I’ll show you how to automatate EBS restore process using Ansible and stop worrying about RTO :)
The restore procedure consists of the following steps:
- stop EC2 instance
- detach existing volume from the instance
- create volume from the snapshot and attach it to the instance
- start EC2 instance
It’s very easy to automate in Ansible, but before I show you the ready-to-go script, let’s look what parameters are required to do a restore operation:
- instance_id – target instance we want to restore snapshot to
- snapshot_id – ID of snapshot we want to restore from
- device_name – as one instance may have multiple volumes attached we need to specify which one we want to replace
The tasks in a playbook
- name: stop EC2 instance
ec2:
instance_ids: "{{ instance_id }}"
state: stopped
wait: yes
- name: get EC2 instance volumes
ec2_vol:
instance: "{{ instance_id }}"
state: list
register: volumes
- name: detach volume from instance
ec2_vol:
instance: None
id: "{{ volumes.volumes | selectattr("attachment_set.device", "equalto", device_name) | map(attribute="id") | first }}"
- name: create volume from snapshot
ec2_vol:
instance: "{{ instance_id }}"
snapshot: "{{ snapshot_id }}"
device_name: "{{ device_name }}"
delete_on_termination: yes
- name: start EC2 instance
ec2:
instance_ids: "{{ instance_id }}"
state: running
wait: yes
The tricky part here is in line 16 – we filter the volumes list by searching for a device name equal to the one from the argument, then we extract id property of the volume and then get the first match (first will fail on empty list and that’s good).
BTW: this script requires boto3 pip package installed, so if you want to ensure that it’s installed on the system that runs the playbook you can add the following task at the beginning:
- name: install required pip packages
become: yes
pip:
name:
- boto3
The playbook shown here does the restore for a single volume, if your instance uses multiple volumes and you want to restore all of them you need to run this script multiple times or – and it’s much better as it’s more consistent – only start instance after last volume restoration.
I hope it’ll save you some time :)
Leave a Comment