Skip to content

Amazon DLM for snapshot automation

0

I automated the daily snapshot creation using Amazon DLM. I wanted to assign a custom name and description for the newly created snapshots via a Lambda function. I tried in many ways (CloudTrail, CloudWatch events etc) in the EventBridge, but the CreateSnapshot event doesn't get triggered. I checked CloudTrail event history, it doesn't capture CreateSnapshot event via DLM. I have noticed CloudWatch metrics EBS has six DLM metrics. But, it did not trigger EventBridge rule.

2 Answers
0
Accepted Answer

I finally figured it out. Create EventBridge rule with the following event pattern.

{
  "source": ["aws.ec2"],
  "detail-type": ["AWS API Call via CloudTrail"],
  "detail": {
    "eventSource": ["ec2.amazonaws.com"],
    "eventName": ["CreateSnapshots"]
  }
}
answered a year ago
0

While Amazon Data Lifecycle Manager (DLM) does not directly support dynamic naming of snapshots through console configuration, you can achieve custom naming and descriptions for your snapshots using a combination of AWS services. However, it's important to note that the CreateSnapshot event from DLM does not trigger EventBridge rules directly, which explains why your attempts with CloudTrail and CloudWatch events were unsuccessful.

Instead, you can implement a solution using AWS Lambda and CloudWatch Events to rename the snapshots after they are created by DLM. Here's how you can approach this:

  1. Set up a CloudWatch Event rule to trigger when a snapshot is created by DLM.

  2. Create an AWS Lambda function that will be triggered by this CloudWatch Event. This function will be responsible for renaming the snapshot and adding a custom description.

  3. In your Lambda function, use the AWS SDK to modify the snapshot tags and description based on your desired naming convention.

Here's a basic outline of what your Lambda function might look like:

import boto3

def lambda_handler(event, context):
    ec2 = boto3.client('ec2')
    snapshot_id = event['detail']['snapshot_id']
    instance_id = event['detail']['instance_id']
    timestamp = event['time']
    
    # Define your custom name and description
    custom_name = f"snapshot-{instance_id}-{timestamp}"
    custom_description = f"Custom snapshot for {instance_id} created at {timestamp}"
    
    # Add the custom name as a tag and update the description
    ec2.create_tags(
        Resources=[snapshot_id],
        Tags=[
            {'Key': 'Name', 'Value': custom_name}
        ]
    )
    
    ec2.modify_snapshot_attribute(
        SnapshotId=snapshot_id,
        Attribute='description',
        Value=custom_description
    )
    
    return {
        'statusCode': 200,
        'body': f"Snapshot {snapshot_id} renamed to {custom_name} and description updated"
    }

To verify if your snapshots are being created successfully by DLM, you can check for specific tags on the snapshots. DLM-created snapshots will have a tag with the key 'aws:dlm:lifecycle-policy-id'. Additionally, for application-consistent snapshots, you might see tags like 'AppConsistent:true' or system tags indicating the success or failure of pre and post scripts.

If you're experiencing issues with snapshot creation, you might want to implement an automatic recovery mechanism for failed EBS snapshots. This involves setting up an EventBridge rule to send EBS snapshot failure events to an SQS queue, which then triggers a Lambda function to attempt another snapshot creation.

Remember to monitor your DLM metrics in CloudWatch to stay informed about any EBS snapshot failures, as these can occur due to transient issues or service limits.

Sources
Amazon DLM for snapshot automation | AWS re:Post
Automating application-consistent Amazon EBS Snapshots for Windows applications | AWS Storage Blog
Automatic Recovery for Failed EBS Snapshot | AWS re:Post

answered a year ago
  • The above AI response doesn't work.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.