luktom.net
  • blog
  • contact
  • polish





How to restore EBS snapshots with Ansible?

On 29 Dec, 2018
Ansible, AWS
No Comments
Views : 5150

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 :)



Tags :   restorerporto

Related Posts

  • What’s the real RPO for databases on AWS RDS and why you’re probably wrong?

  • Leave a Comment

    Click here to cancel reply

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>





    Łukasz Tomaszkiewicz

    Łukasz Tomaszkiewicz

    Łukasz Tomaszkiewicz is a highly skilled and passionate cloud expert who loves to automate repeatable things and secure them.

    His broad experience in the areas of software development, database design, containerization and cloud infrastructure management gives him a holistic view of a modern technology stack.

    In his spare time he enjoys photography, blogging and speaking on local IT-related communities.

    Vim-believer :)

    Categories

    • Ansible
    • AWS
    • C#
    • Go
    • Google Cloud
    • Kubernetes
    • Prometheus
    • Speeches
    • Virtualization
    • Windows

    Tags

    alert alerting alertmanager ansible ansible operator argocd aws aws cli aws ug bash c# centos cloudwatch databases esxi flux gcp gitops google cloud k8s kubernetes linux mysql open source operator operator-sdk policies powershell prelekcje prometheus recovery restore rhel rpo rto scp speeches terraform virtualization vmware vsan vsphere weaveworks wifi windows

    Copyright © 2006-2018 by Łukasz Tomaszkiewicz. Wszelkie prawa zastrzeżone