How to copy a snapshot in aws from one region to another


Copying snapshots is crucial and a resourceful way to efficiently and maintain an effective cloud infrastructure. In this article, we are going to discuss some of the approaches to achieve the goal of copying your snapshots from your AWS account to your desired region in your AWS account. 

The first approach is to use your AWS Console: 

To copy an AWS EC2 snapshot from one region to another using the AWS Management Console, follow these steps:

  1. Log in to AWS Console: Log in to your AWS account at https://aws.amazon.com/console/.

  2. Select Source Region: From the AWS Management Console, ensure that you are in the source region where the original snapshot exists.

  3. Navigate to EC2 Service: Navigate to the "EC2 Dashboard" by clicking on "Services" in the top left corner and selecting "EC2" under the "Compute" section.

  4. Select Snapshots: In the EC2 Dashboard, select "Snapshots" from the left-hand menu under "Elastic Block Store."

  5. Identify Source Snapshot: On the "Snapshots" page, identify the snapshot that you want to copy. Note its Snapshot ID and other details.

  6. Initiate Snapshot Copy: Right-click on the snapshot you want to copy, or select the snapshot and click the "Actions" button. Choose "Copy" or "Copy Snapshot" from the menu.

  7. Specify Destination Region: In the "Copy Snapshot" wizard, select the destination region where you want to copy the snapshot. Enter any desired description for the copied snapshot.

  8. Configure Encryption and Permissions (Optional): You can configure additional settings such as encryption, permissions, and tags as needed.

  9. Review and Confirm: Review the settings and click "Copy" or "Start copying" to initiate the copy operation.

  10. Monitor the Copy Status: You can monitor the copy operation's status from the "Snapshots" page. The status will change from "copying" to "completed" once the copy operation finishes.

  11. Navigate to Destination Region: To view the copied snapshot in the destination region, switch to the destination region in the AWS Management Console. You can use the region selector in the top right corner of the console.

  12. Check Copied Snapshot: In the destination region, go to the "Snapshots" page, and you should see the copied snapshot with the same Snapshot ID as the original snapshot.

Please note that copying a snapshot may incur additional charges in the destination region. Make sure to verify your permissions and billing settings to perform this operation.

The second approach to achieve the same would be by using the AWS CLI :

In your linux terminal you can supply these commands :

Example 1: To copy a snapshot to another Region

The following copy-snapshot example command copies the specified snapshot from the us-west-2 Region to the us-east-1 Region and adds a short description.

aws ec2 copy-snapshot \
    --region us-east-1 \
    --source-region us-west-2 \
    --source-snapshot-id snap-066877671789bd71b \
    --description "This is my copied snapshot."

Output:

{
    "SnapshotId": "snap-066877671789bd71b"
}

Example 2: To copy an unencrypted snapshot and encrypt the new snapshot

The following copy-snapshot command copies the specified unencrypted snapshot from the us-west-2 Region to the current Region and encrypts the new snapshot using the specified KMS key.

aws ec2 copy-snapshot \
    --source-region us-west-2 \
    --source-snapshot-id snap-066877671789bd71b \
    --encrypted \
    --kms-key-id alias/my-kms-key

Output:

{
    "SnapshotId": "snap-066877671789bd71b"
}

Example 2: To copy an unencrypted snapshot and encrypt the new snapshot

The following copy-snapshot command copies the specified unencrypted snapshot from the us-west-2 Region to the current Region and encrypts the new snapshot using the specified KMS key.

aws ec2 copy-snapshot \
    --source-region us-west-2 \
    --source-snapshot-id snap-066877671789bd71b \
    --encrypted \
    --kms-key-id alias/my-kms-key

Output:

{
    "SnapshotId": "snap-066877671789bd71b"
}                                                                                                                                                        
You can also use a bash script to achieve the same: 
#!/bin/bash

# Snapshot ID to copy
source_snapshot_id="snap-0896cc8c3a903e01f"

# Source and destination regions
source_region="us-east-1"
destination_region="ap-south-1"

# Copy the snapshot
aws ec2 copy-snapshot \
    --region "$source_region" \
    --source-region "$source_region" \
    --source-snapshot-id "$source_snapshot_id" \
    --destination-region "$destination_region" \
    --description "Copy of snapshot $source_snapshot_id from $source_region to $destination_region"
Do not forget to make this script executable by giving it permission by command: chmod + x script.sh (give your script name in place of the script.sh file name)

and then execute the same by using the command: sh script.sh (use your filename in place of the script.sh file name)
You can also use a python script to achieve the same. 
import boto3
import time

def copy_snapshot(source_snapshot_id, source_region, destination_region, description):
    # Create a Boto3 EC2 client for the source and destination regions
    source_client = boto3.client('ec2', region_name=source_region)
    destination_client = boto3.client('ec2', region_name=destination_region)

    # Copy the snapshot
    response = source_client.copy_snapshot(
        SourceSnapshotId=source_snapshot_id,
        SourceRegion=source_region,
        DestinationRegion=destination_region,
        Description=description
    )

    return response['SnapshotId']

def check_snapshot_copy_status(snapshot_id, region):
    client = boto3.client('ec2', region_name=region)

    # Check the snapshot status
    while True:
        response = client.describe_snapshots(SnapshotIds=[snapshot_id])
        state = response['Snapshots'][0]['State']

        print(f"Snapshot {snapshot_id} status in region {region}: {state}")

        if state == 'completed':
            break

        time.sleep(10)  # Wait for 10 seconds before checking again

if __name__ == '__main__':
    source_snapshot_id = 'snap-0896cc8c3a903e01f'
    source_region = 'us-east-1'
    destination_region = 'ap-south-1'
    description = 'Copy of snapshot from us-east-1 to ap-south-1'

    # Copy the snapshot and get the new snapshot ID
    new_snapshot_id = copy_snapshot(source_snapshot_id, source_region, destination_region, description)
    print(f"Snapshot copied. New snapshot ID: {new_snapshot_id}")

    # Check the status of the snapshot copy
    check_snapshot_copy_status(new_snapshot_id, destination_region)

No comments:

Post a Comment