Backing Up Amazon EC2 Instances – AWS Security Fundamentals – SCS-C02 Study Guide

Backing Up Amazon EC2 Instances

If you want to back up your instance for either point-in-time recovery purposes or to use in a launch configuration with autoscaling, you need to create an AMI. Follow these steps to create an AMI:

  1. Launch an EC2 instance. To do this, you must first ensure you have an instance to back up and create the AMI. You will need to find a public image from AWS to use as the base. An example AMI ID has been provided in the following example:

$ a

ws ec2 run-instances \

–image-id ami-0f3c9c466bb525749 \

–instance-type t3.micro \

–region us-east-2

Note

image-id, which is an AMI of Amazon Linux 2, is based out of the us-east-2 (Ohio) region.

Once it launches, it should return a JSON statement with the instance ID. You will need the instance ID for your next step to create the AMI. In this case, the instance ID that was returned was i-0563e7e31aca9c89a.

  • Create the AMI as the backup:

$aws

ec2 create-image \

–instance-id i-0563e7e31aca9c89a \

–name backup_ami \

–no-reboot

If the image is created successfully, then you should get a JSON return with the ImageId value:

{

“Im

ageId”: “ami

-046698ac2e320e8c6″

}

  • Verify the image:

$aws

ec2 describe-images \

–region us-east-2 \

–image-ids ami

-046698ac2e320e8c6

  • This should return a block of JSON with multiple values. However, the main value you are looking for is the line that says State. If the value says available, you have successfully backed up your EC2 instance and are ready to test your backup.

Here, you are going back to the command you initially executed in step 1; however, now you have your custom ImageId that you can substitute. Also, you will add the flag for an SSH key so that you can go in to verify any setting for the instance:

$aws

ec2 run-instances \

–image-id ami-046698ac2e320e8c6 \

–instance-type t3.micro \

–key-name my-ssh-key \

–region us-east-2

You can see that with just a few simple commands, you have taken your running EC2 instance and not only backed it up but also created a launchable AMI. You could take this backup a step further and copy the AMI to another region, ensuring you remove any hardcoded region-specific values or settings from the image. If you do this, it should launch and run without issues.