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:
$ 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.
$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″
}
$aws
ec2 describe-images \
–region us-east-2 \
–image-ids ami
-046698ac2e320e8c6
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.