Bobcares

Restore an EC2 instance from an AWS Backup with AWS CLI

by | Aug 23, 2021

One of the common queries we get from our AWS customers is something along the lines of ” how to restore an EC2 instance from an AWS Backup with AWS CLI”? We can help you with this!

As a part of our AWS Support Services, we often receive similar requests from our AWS customers.

Today, let’s see the steps followed by our Support Techs to help our customers to restore an AWS EC2 instance from an AWS Backup with AWS Command-Line Interface.

 

Restore an EC2 instance from an AWS Backup with AWS CLI

 
Now, let’s see the steps for restoring EC2 instance from an AWS Backup with AWS CLI:

  1. Firstly, list all the recovery points in the vault using the following list-recovery-points-by-backup-vault command. Note the RecoveryPointArn for the recovery point that we want to restore.
aws backup list-recovery-points-by-backup-vault --backup-vault-name Default --output json --query 'RecoveryPoints[].[BackupVaultName,RecoveryPointArn]'

2. To get the metadata of the Amazon EC2 instance, we need to run the get-recovery-point-restore-metadata command.

aws backup get-recovery-point-restore-metadata --backup-vault-name Default --recovery-point-arn arn:aws:ec2:eu-west-1::image/ami-012ab3456789c0123

3. Also copy the output of the above command. For example:

{
"BackupVaultArn": "arn:aws:backup:eu-east-1:987654321013:backup-vault:Default",
"RecoveryPointArn": "arn:aws:ec2:eu-west-1::image/ami-111ab9876c0123",
"RestoreMetadata": {
"CapacityReservationSpecification": "{\"CapacityReservationPreference\":\"open\"}",
"CpuOptions": "{\"CoreCount\":1,\"ThreadsPerCore\":1}",
"CreditSpecification": "{\"CpuCredits\":\"standard\"}",
"DisableApiTermination": "false",
"EbsOptimized": "false",
"HibernationOptions": "{\"Configured\":false}",
"IamInstanceProfileName": "ServerAccess",
"InstanceInitiatedShutdownBehavior": "stop",
"InstanceType": "t2.micro",
"KeyName": "BackupTesting",
"Monitoring": "{\"State\":\"disabled\"}",
"NetworkInterfaces": "[{\"AssociatePublicIpAddress\":true,\"DeleteOnTermination\":true,\"Description\":\"\",\"DeviceIndex\":0,\"Groups\":[\"sg-0babcd1234567890f\"],\"Ipv6AddressCount\":0,\"Ipv6Addresses\":[],\"NetworkInterfaceId\":\"eni-12345678a9b0cd1e2\",\"PrivateIpAddress\":\"192.0.2.206\",\"PrivateIpAddresses\":[{\"Primary\":true,\"PrivateIpAddress\":\"192.0.2.206\"}],\"SecondaryPrivateIpAddressCount\":0,\"SubnetId\":\"subnet-1a2b3c4d\",\"InterfaceType\":\"interface\"}]",
"Placement": "{\"AvailabilityZone\":\"eu-west-1b\",\"GroupName\":\"\",\"Tenancy\":\"default\"}",
"SecurityGroupIds": "[\"sg-0babcd1234567890f\"]",
"SubnetId": "subnet-1a2b3c4d",
"VpcId": "vpc-1a2b3c4d",
"aws:backup:request-id": "c1234567-ee7c-4896-beeb-ee123b456789"
}
}

4. Then edit the command output to keep only the following metadata fields and values:

{
"VpcId": "vpc-1a2b3c4d",
"Monitoring": "{\"State\":\"disabled\"}",
"CapacityReservationSpecification": "{\"CapacityReservationPreference\":\"open\"}",
"InstanceInitiatedShutdownBehavior": "stop",
"DisableApiTermination": "false",
"KeyName": "BackupTesting",
"CreditSpecification": "{\"CpuCredits\":\"standard\"}",
"HibernationOptions": "{\"Configured\":false}",
"EbsOptimized": "false",
"Placement": "{\"AvailabilityZone\":\"eu-east-1b\",\"GroupName\":\"\",\"Tenancy\":\"default\"}",
"aws:backup:request-id": "c1234567-ee7c-4896-beeb-ee123b456789",
"InstanceType": "t2.micro",
"NetworkInterfaces": "[{\"DeleteOnTermination\":true,\"Description\":\"Primary network interface\",\"DeviceIndex\":0,\"Groups\":[\"sg-0babcd1234567890f\"],\"Ipv6AddressCount\":0,\"Ipv6Addresses\":[],\"PrivateIpAddresses\":[{\"Primary\":true,\"PrivateIpAddress\":\"192.0.2.210\"}],\"SecondaryPrivateIpAddressCount\":1,\"SubnetId\":\"subnet-1a2b3c4d\",\"InterfaceType\":\"interface\"}]"
}

5. We must also change the primary private IP address in the metadata output if the original instance is still in use. For that, change the Primary PrivateIpAddress to a different IP address than what was originally returned by the get-recovery-point-restore-metadata command in the NetworkInterfaces metadata field.

6. Then we need to save the metadata into a JSON file. We can name the file ec2-restore-metadata.json.

7. Finally, run the start-restore-job command with –recovery-point-arn set to the RecoveryPointArn and –metadata set to the JSON file.

aws backup start-restore-job --region eu-west-1 --recovery-point-arn "arn:aws:ec2:eu-west-1::image/ami-111ab9876c0123" --iam-role-arn "arn:aws:iam::987654321012:role/service-role/AWSBackupDefaultServiceRole" --metadata file://ec2-restore-metadata.json

OR

Instead of using a JSON file, we can also define the instance metadata as parameters within the start-restore-job command. For example:

aws backup start-restore-job --region eu-west-1 --recovery-point-arn "arn:aws:ec2:eu-west-1::image/ami-111ab9876c0123" --iam-role-arn "arn:aws:iam::987654321013:role/service-role/AWSBackupDefaultServiceRole" --metadata '{"VpcId": "vpc-1a2b3c4d", "Monitoring": "{\"State\":\"disabled\"}", "CapacityReservationSpecification": "{\"CapacityReservationPreference\":\"open\"}", "InstanceInitiatedShutdownBehavior": "stop", "DisableApiTermination": "false", "KeyName": "BackupTesting", "CreditSpecification": "{\"CpuCredits\":\"standard\"}", "HibernationOptions": "{\"Configured\":false}", "EbsOptimized": "false", "Placement": "{\"AvailabilityZone\":\"eu-west-1b\",\"GroupName\":\"\",\"Tenancy\":\"default\"}", "aws:backup:request-id": "c1234567-ee7c-4896-beeb-ee123b456789", "InstanceType": "t2.micro", "NetworkInterfaces": "[{\"DeleteOnTermination\":true,\"Description\":\"Primary network interface\",\"DeviceIndex\":0,\"Groups\":[\"sg-0babcd1234567890f\"],\"Ipv6AddressCount\":0,\"Ipv6Addresses\":[],\"PrivateIpAddresses\":[{\"Primary\":true,\"PrivateIpAddress\":\"172.31.32.210\"}],\"SecondaryPrivateIpAddressCount\":1,\"SubnetId\":\"subnet-1a2b3c4d\",\"InterfaceType\":\"interface\"}]"}'

 

[Need help with more AWS queries? We’d be happy to assist]
 

Conclusion

 
To conclude, today we discussed the steps followed by our Support Engineers to help our customers to restore an EC2 instance from an AWS Backup with AWS CLI.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

2 Comments

  1. Fazal

    how to use userData field .. I am getting Invalid BASE64 encoding of user data

    Reply
    • Hiba Razak

      Hi,
      Please contact our support team via live chat(click on the icon at right-bottom)

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF