EBS Operations: Snaphshot creation and restoring EBS volume in different AZ

Photo by Andrew Neel on Unsplash

EBS Operations: Snaphshot creation and restoring EBS volume in different AZ

Amazon Elastic Block Store (EBS) provides block-level storage volumes for use with Amazon EC2 instances. An EBS snapshot is a point-in-time copy of your data, and it is a way to back up the data on your Amazon EBS volumes. Once you create a snapshot for an EBS volume, you can use it to restore data or create a new EBS volume. Snapshots are incremental in nature, meaning that only the blocks that have changed since your last snapshot are saved. This minimizes the storage costs.

How EBS Snapshots works:

Amazon EBS (Elastic Block Store) snapshots are a key feature for backup, restore, and disaster recovery. Understanding how they work can help users utilize them more effectively. Here's an overview of how EBS snapshots work:

1. Block-Level Storage:

Amazon EBS provides block-level storage, meaning data is saved in blocks of a specified size. When you take a snapshot, Amazon captures the data in these blocks.

2. Incremental Backups:

  • The very first snapshot taken of an EBS volume is a full copy of the volume's data. However, subsequent snapshots are incremental.

  • This means that only the blocks that have changed after the last snapshot will be saved in the subsequent snapshot.

  • For instance, if you have a 100 GB volume and only 5 GB of data has changed since your last snapshot, only that 5 GB will be stored in the new snapshot.

  • This approach helps in minimizing storage costs and snapshot creation time.

3. Storage in Amazon S3:

  • Even though you don't directly interact with Amazon S3 when working with EBS snapshots, the snapshots are stored on Amazon S3, which provides durability and availability.

  • Snapshots are stored across multiple servers and data centers within a region, making them highly durable.

4. Snapshot Creation Process:

  • When you initiate a snapshot of an EBS volume, the snapshot captures a point-in-time copy of the data.

  • If data is being written to the volume while you're taking the snapshot, Amazon will also store metadata with the snapshot to ensure data consistency for any restores.

  • It's often recommended to either halt IO operations or detach the volume temporarily if consistency is a high concern. However, in many cases, like for basic file systems, AWS can manage the consistency without intervention.

5. Restoring from a Snapshot:

  • You can create a new EBS volume from a snapshot. This volume can be of the same size as the original or larger.

  • When a new volume is created from a snapshot, all the data is pulled from Amazon S3 to the EBS volume.

  • The restored volume is a full copy of the original, and changes to the restored volume won't affect the snapshot or vice versa.

6. Sharing Snapshots:

  • By default, snapshots are private, meaning they can only be accessed by the AWS account that created them.

  • However, you can share snapshots with specific AWS accounts or make them public, allowing any AWS account to access them.

7. Encryption:

  • If an EBS volume is encrypted, its snapshots are encrypted automatically.

  • You can also encrypt a snapshot later or when copying it.

  • When you restore the encrypted snapshot, the EBS volume created will also be encrypted.

8. Performance:

  • The first time you read from an EBS volume restored from a snapshot, you might observe a latency. This is because the data is being lazily loaded from Amazon S3.

  • For applications where this latency is unacceptable, it's recommended to "pre-warm" the volume by reading all of its blocks before the actual use.

Prerequisites:

  1. Install AWS CLI: Ensure you have the AWS Command Line Interface (CLI) installed. If not, you can download and install it from here.

  2. Configure AWS CLI: Before you can start using the AWS CLI with your AWS account, you'll need to configure it using the aws configure command. You'll be prompted to provide your AWS access key, secret key, region, and output format.

💡
To run an EC2 instance use the following guide: How to launch a single EC2 instance via AWS CLI

Tutorial: How to Create EBS Snapshot via AWS CLI

  1. Identify the EBS Volume ID: Before creating a snapshot, you need to know the ID of the EBS volume you want to snapshot. You can list all the EBS volumes using:

     aws ec2 describe-volumes --output table
    

    Look for the VolumeId in the output.

    The SnapshotId you see in the describe-volumes output indicates which snapshot (if any) the volume was created from. If it's the root volume of an EC2 instance, it's likely created from the snapshot associated with the chosen AMI. If you see a null or empty value for SnapshotId, it means that the volume was created without a base snapshot.

  2. Create a Snapshot: Once you have identified the volume ID (let's say it's vol-0123456789abcdef0), use the following command to create a snapshot:

     aws ec2 create-snapshot --volume-id vol-0123456789abcdef0 --description "My first EBS snapshot"
    

    Replace vol-0123456789abcdef0 with your volume ID and "My first EBS snapshot" with a description of your choice.

  3. Monitor the Snapshot Creation: The snapshot creation might take some time depending on the size of the volume and the amount of data changed since the last snapshot. To check the status, use:

     aws ec2 describe-snapshots --snapshot-ids <Your-Snapshot-ID>
    

    Replace <Your-Snapshot-ID> with the snapshot ID you received from the output of the previous command. Look for the State field in the output. Once it changes from pending to completed, your snapshot is ready.

  4. (Optional) Tag the Snapshot: If you want to add tags to your snapshot, you can do so with the following command:

     aws ec2 create-tags --resources <Your-Snapshot-ID> --tags Key=Name,Value="My Snapshot Name"
    

    Replace <Your-Snapshot-ID> with your snapshot ID and adjust the tag key-value pairs as desired.

  5. (Optional) Delete the Snapshot: If you ever want to delete a snapshot, you can do so with the following command:

     aws ec2 delete-snapshot --snapshot-id <Your-Snapshot-ID>
    

    Replace <Your-Snapshot-ID> with the ID of the snapshot you wish to delete.

You've successfully created, tagged, and understood how to delete an EBS snapshot using the AWS CLI. Remember that while snapshots are a great way to back up your data, you should also consider other backup and disaster recovery strategies for comprehensive data protection.

Tutorial: Restoring EBS Volume in Another AZ from a Snapshot

Restoring an EBS volume from a snapshot in another Availability Zone (AZ) is a common practice for various purposes like disaster recovery, data migration, or setting up duplicate environments. Here's a step-by-step tutorial on how to accomplish this:

  1. List Available Snapshots (Optional): If you're unsure about the snapshot ID you want to restore, you can list all snapshots available to your account:

     aws ec2 describe-snapshots --owner-ids self --query 'Snapshots[].[SnapshotId,Description]' --output table
    

  2. Create a New EBS Volume from the Snapshot in Another AZ:

    Use the following command, replacing the placeholders with the appropriate values:

     aws ec2 create-volume --snapshot-id snap-xxxxxxxx --availability-zone us-east-1b --volume-type gp2
    
    • Replace snap-xxxxxxxx with your snapshot ID.

    • Replace us-east-1b with the desired AZ where you want to restore the volume.

    • Adjust gp2 if you wish to use a different volume type.

The command will return details about the new EBS volume, including its volume ID.

  1. Wait for the Volume to be Available:

    Creating a volume from a snapshot, especially a large one, can take some time. Use the following command to monitor its status:

     aws ec2 describe-volumes --volume-ids vol-xxxxxxxx --query 'Volumes[].[VolumeId,State]' --output table
    

    Replace vol-xxxxxxxx with the volume ID returned in the previous step. Wait until the state is available.

  2. Attach the Restored Volume to an EC2 Instance:

    Once the volume is available, you can attach it to an EC2 instance in the same AZ:

     aws ec2 attach-volume --volume-id vol-xxxxxxxx --instance-id i-xxxxxxxx --device /dev/sdf
    
    • Replace vol-xxxxxxxx with your new volume ID.

    • Replace i-xxxxxxxx with the ID of the EC2 instance where you want to attach the volume.

    • Adjust /dev/sdf as necessary. This is the device name the volume will appear as inside the instance. Ensure it doesn't conflict with other device names.

  3. Mount the Volume Inside the EC2 Instance (if required):

    After attaching the volume, you'll typically need to mount it within the operating system of the EC2 instance to access its data. This process can vary based on the OS and whether the volume has a file system on it. For a common Linux system:

     sudo mkdir /mnt/myvolume
     sudo mount /dev/sdf /mnt/myvolume
    

    Replace /mnt/myvolume with your desired mount point and /dev/sdf with the device name you used when attaching the volume.

    If there's no file system, you'll need to create one. For an ext4 file system, you can use:
     sudo mkfs -t ext4 /dev/sdf
    
  4. Clean Up:

    If you no longer need the original snapshot or the old volume, consider deleting them to avoid incurring unnecessary costs. But make sure you've backed up any critical data before doing so!

You've successfully restored an EBS volume from a snapshot in another Availability Zone. This method can be particularly handy for migrating data or setting up failover systems across AZs.

References:

  1. Amazon EBS snapshots

  2. Create Amazon EBS snapshots