Bootstrap FreeKB - Amazon Web Services (AWS) - Create an Elastic Block Storage (EBS) Volume Snapshot using Python boto3
Amazon Web Services (AWS) - Create an Elastic Block Storage (EBS) Volume Snapshot using Python boto3


This assumes you are familar with the basic configurations needed to connect to Amazon Web Services (AWS) using Python boto3. If not, check out my article Python (Scripting) - Getting Started with Amazon Web Services (AWS) boto3.

There are different ways to go about backing up your Amazon Web Services (AWS) EC2 instances

An AMI will be a complete snapshot of the entire EC2 instance, whereas an EBS Volume Snapshot will be a snapshot of one of the EBS Volumes being used by the EC2 instance

By default, when you create a new EC2 instance, the file system being used by the EC2 instance will be an Elastic Block Storage (EBS) Volume. This can be seen on the Storage tab of your EC2 instance.

 

The aws ec2 describe-volumes command can be used to list your Elastic Block Storage (EBS) Volumes. Notice in this example that the Elastic Block Storage (EBS) Volume has snapshot snap-096dd96dde7d24189.

~]# aws ec2 describe-volumes
[
    {
        "AvailabilityZone": "us-east-1b",
        "Attachments": [
            {
                "AttachTime": "2023-07-28T19:58:37.000Z",
                "InstanceId": "i-0a3ea97aa2383de58",
                "VolumeId": "vol-0d989fe3bad4dd2f9",
                "State": "attached",
                "DeleteOnTermination": true,
                "Device": "/dev/xvda"
            }
        ],
        "Encrypted": false,
        "VolumeType": "gp3",
        "VolumeId": "vol-0d989fe3bad4dd2f9",
        "State": "in-use",
        "Iops": 3000,
        "SnapshotId": "snap-096dd96dde7d24189",
        "CreateTime": "2023-07-28T19:58:37.544Z",
        "MultiAttachEnabled": false,
        "Size": 8
    }
]

 

The aws ec2 describe-snapshots can be used to list your Elastic Block Storage (EBS) Volume Snapshots. A snapshot is created when the EBS volume is created, but subsequent snapshots are not automatically created, thus it is your responsibility to create snapshots.

~]# aws ec2 describe-snapshots
[
    {
        "Description": "",
        "Encrypted": false,
        "OwnerId": "123456789012",
        "Progress": "100%",
        "SnapshotId": "snap-096dd96dde7d24189",
        "StartTime": "2023-07-25T22:43:53.821000+00:00",
        "State": "completed",
        "VolumeId": "vol-0d989fe3bad4dd2f9",
        "VolumeSize": 8,
        "OwnerAlias": "amazon",
        "StorageTier": "standard"
    }
]

 

Here is the minimal boilterplate code without error handling to create an Elastic Block Storage (EBS) Volume Snapshots. The VolumeSize key/value pair is required to specify the size of the volume (8 GB in this example).

#!/usr/bin/python3
import boto3

client = boto3.client('ec2')
response = client.create_snapshot(
        VolumeId='vol-0d989fe3bad4dd2f9'
        )

print(response)

 

Here is a more practical example, with try/except/else error handling

#!/usr/bin/python3
import boto3
import sys

try:
  client = boto3.client('ec2')
except Exception as exception:
  print(exception)
  sys.exit(1)

try:
  response = client.create_snapshot(
               VolumeId='vol-0d989fe3bad4dd2f9'
             )
except Exception as exception:
  print(exception)
else:
  print(response)

 

Something like this should be returned.

{
  'Description': '', 
  'Encrypted': False, 
  'OwnerId': '123456789012', 
  'Progress': '', 
  'SnapshotId': 'snap-0c36f7db5b929a9f3', 
  'StartTime': datetime.datetime(2024, 4, 5, 0, 24, 37, 398000, tzinfo=tzutc()), 
  'State': 'pending', 
  'VolumeId': 'vol-0d989fe3bad4dd2f9', 
  'VolumeSize': 8, 
  'Tags': [], 
  'ResponseMetadata': {
    'RequestId': '36ce9c82-0394-4916-8486-12a4277f1dcf', 
    'HTTPStatusCode': 200, 
    'HTTPHeaders': {
      'x-amzn-requestid': '36ce9c82-0394-4916-8486-12a4277f1dcf', 
      'cache-control': 'no-cache, no-store', 
      'strict-transport-security': 'max-age=31536000; includeSubDomains', 
      'content-type': 'text/xml;charset=UTF-8', 
      'content-length': '555', 
      'date': 'Fri, 05 Apr 2024 00:24:37 GMT', 
      'server': 'AmazonEC2'}, 
    'RetryAttempts': 0
  }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 0159a2 in the box below so that we can be sure you are a human.