How to Back Up Amazon EC2 Instances

How to Back Up Amazon EC2 Instances

What Is Amazon EC2?

Amazon Elastic Compute Cloud (Amazon EC2) is a virtual server that offers businesses the ability to run applications on the AWS cloud infrastructure. Amazon EC2 lets you create virtual machine instances with different resource configurations of CPU, memory, storage, and networking. EC2 setup involves creating an Amazon Machine Image (AMI). Machine images act as a template that configures an operating system and determines the operating environment of the user.

When it comes to developing an EC2 backup strategy, your backup method will depend on your specific needs. AWS offers a wide range of backup and recovery tools. However, the service does not back up EC2 Instances by default. This article will explore two common cloud-based solutions—EBS snapshots and AMIs.

2 Different Ways to Backup EC2 Instances

There is more than one way to backup EC2 instances. To choose the right backup method for your needs, you should ask yourself the following questions

  • Does the backup solution has to automated 
  • How quickly you want to restore an instance during an emergency
  • How much data you can store and transfer

EC2 Backup Method 1: EBS Snapshots

If your EC2 instances are stored via Amazon Elastic Block Store (EBS), you’ll need to create EBS snapshotsEBS snapshots allow you to save the state of an EBS volume at a given point in time, then restore it later. This approach to EC2 backup is relatively straightforward. You first stop the instance or unmount the volume you want to back up since you can’t reliably back up a volume that is still in use.

Use the following steps to create a snapshot from the specified volume

Create a snapshot using the AWS Management Console:

  • Choose Snapshots under Elastic Block Store in the navigation pane.
  • Choose Create Snapshot.
  • Choose Volume for ‘Select resource type’
  • For Volume, select the volume.
  • Choose Create Snapshot.

Create a snapshot using the AWS CLI:

Use one of the following commands to create a snapshot using the command line 

create-snapshot
[--description <value>]
--volume-id <value>
[--tag-specifications <value>]
[--dry-run | --no-dry-run]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]

Shorthand Syntax:

ResourceType=string,Tags=[{Key=string,Value=string},{Key=string,Value=string}]

For example, to create a snapshot of the volume with a volume ID of vol-1234567890abcdem2 and a short description to identify the snapshot, use the following command:

aws ec2 create-snapshot --volume-id vol-1234567890abcdem2 --description "This is my EBS volume snapshot"

EC2 Backup Method 2: Creating a New AMI

Another approach to back up EC2 instances is to create a new Amazon Machine Image (AMI). An AMI contains all of the operating system code, configuration settings, and data that you need in order to create a virtual server on EC2.

This method of EC2 backup involves more steps than taking EBS snapshots. AMI image backups provide the convenience of having your operating system and configuration data packaged into a single backup image, which you can then use to restore an instance quickly.

For example, to create an AMI from an Amazon Linux instance, use the following process.

1. Install GRUB

sudo yum install -y grub

2. Install the partition management packages

sudo yum install -y gdisk kpartx parted

3. Create a temporary directory on your instance for your credentials

mkdir /tmp/cert

For more details on creating an AMI for backup see the official AWS documentation.

AWS EC2 backup Automation

Working manually is not your only option for backing up EC2 instances. There are several ways to automate EC2 backup.

  • Running scripts or using API calls—a very challenging and resource-intensive process that requires a high proficiency in scripting. There is always a chance to get failed AWS EC2 backups in this method.
  • Using AWS Lambda service—allows running code for managing AWS services and performing various tasks in AWS environments. The downside of this approach the amount of time and effort it takes to set up a workable code to perform the AWS Lambda function the way you want.
  • Using AWS Backup service—allows to create automated data backups across AWS services and manage them using the AWS console.
  • Using Amazon Data Lifecycle Manager (Amazon DLM)—allows to automate the creation, retention, and deletion of Amazon EBS volume snapshots. Create a policy, indicating which volumes are to be snapshotted, set a retention model, fill in a few other details, and let Data Lifecycle Manager do the rest.

Let’s look at an example of how to automate EBS snapshots using Amazon DLM and the AWS console.

To create a lifecycle policy

  • Log in to the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  • In the navigation pane, choose Elastic Block Store, Lifecycle Manager and Create Snapshot Lifecycle Policy.
  • Provide essential information about your policy such as description, schedule name, retention rule and snapshot creation time.
  • Choose Create Policy.

To display a lifecycle policy

  • In the navigation pane, choose Elastic Block Store and Lifecycle Manager.
  • Select a lifecycle policy from the list.

To modify a lifecycle policy

  • In the navigation pane, choose Elastic Block Store and Lifecycle Manager.
  • Select a lifecycle policy from the list.
  • Choose Actions and Modify Policy.

For more information about how to automate EBS snapshots with Amazon DLM using the AWS CLI and using the AWs API, see the official documentation.

Conclusion

There are two main approaches to backing up EC2 instances—first, to use EBS snapshots, and second, to use AMI images. There are also different methodologies for implementing both of these approaches. The default method is to work manually through the EC2 Console. While this strategy is easy, it suffers from the major drawback of being impossible to automate. Lack of automation prevents this backup strategy from being feasible to execute on a large scale.

Any organization seeking to create an EC2 backup solution that works at scale should adopt one of the automation approaches we covered here.

This could interest you

Become a more social person