Skip to main content

Scheduling EBS Snapshots Using CloudWatch Events and AWS Lambda

Hands-On Lab

 

Photo of

Training Architect

Length

00:30:00

Difficulty

Intermediate

Welcome to this hands-on lab for automating EBS snapshots using Lambda functions and CloudWatch events! This lab provides hands-on experience in utilizing CloudWatch events to create a Lambda function that takes snapshots of targeted EBS volumes. Our goal is to take snapshots of volumes that are currently in use, ignoring ones that are not attached. The primary focus will be on the following features of AWS Lambda: - Lambda Console - Function Code - CloudWatch Events - EBS Volumes/Snapshots The function you create in this lab will give you some practice creating Lambda functions and automating EBS snapshots. This scenario is something a cloud architect might encounter in the real world, and demonstrates best practice for getting it resolved. Happy learning!

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Scheduling EBS Snapshots Using CloudWatch Events and AWS Lambda

Introduction

Cloud architects are often tasked with taking targeted snapshots of volumes. In this hands-on lab, we will use a Lambda function to take snapshots of volumes that are currently in use, ignoring ones that are not attached. To do this, we will create a Lambda function and create and configure two CloudWatch events to take snapshots of targeted EBS volumes.

We'll be working with the following features of AWS:

  • Lambda Console
  • Function Code
  • CloudWatch Events
  • EBS Volumes/Snapshots

Log in to the AWS Management Console using the credentials provided on the lab instructions page. Make sure you are using the us-east-1 (N. Virginia) region.

Create an EBS Snapshot Lambda Function

  1. Navigate to the Lambda service in the AWS Management Console.
  2. Click Create a function.
  3. Choose Author from scratch.
  4. In the Author from scratch menu, configure the following settings:
    • Name: EBS_Snap
    • Runtime: Python 3.6
    • Role: Choose an existing role
    • Existing role: lambda_exec_role_LA
  5. Click Create function.
  6. Scroll down the page to the function code editor.
  7. Select and delete the default code.
  8. Paste in the code that was provided in the lab instructions.
  9. Click Save.

Create a CloudWatch Event that Triggers the Function (Rule A)

  1. Scroll up the page to the Designer section.
  2. Under Add triggers, click CloudWatch Events.
  3. Scroll down to the Configure triggers section, and configure the following events:
    • Rule: Create a new rule
    • Rule name: Rule_A
    • Rule description: A - 1 Min
    • Rule type: Schedule expression
    • Schedule expression: rate(1 minute)
  4. Select Enable trigger, then click Add.
  5. Click Save.
  6. In a new browser tab, open the AWS EBS service.
  7. Click Snapshots in the left sidebar.
  8. Go back to AWS Lambda.
  9. Under CloudWatch Events, toggle the trigger to Disabled to stop it from running.
  10. Click Save.
  11. Go back to the EC2 Management Console.
  12. Select all of the snapshots in the list.
  13. Click Actions > Delete > Yes, Delete.

Create a Second CloudWatch Event that Triggers the Function (Rule B)

  1. Open the AWS CloudWatch service in a new browser tab.
  2. Click Logs in the left sidebar.
  3. Click the name of the log group, then the name of the log stream to open it.
  4. Verify that everything is being written to the log stream correctly.
  5. Click Events in the left sidebar.
  6. Click Create rule.
  7. Under Event Source, select Schedule and configure the following settings:
    • Fixed rate of: 2 Minutes
  8. Under Targets, click + Add target.
  9. Select Lambda function from the dropdown, and configure the following settings:
    • Function: EBS_Snap
  10. Click Configure details.
  11. On the Configure rule details page, configure the following options:
    • Name: Rule_B
    • Description: B - 2 Min
    • State: Enabled
  12. Click Create rule.
  13. Switch to your ECS Management Console browser tab.
  14. Click Snapshots in the left sidebar.
  15. Click the refresh icon at the top right of the screen.
  16. Verify that four new snapshots have been created.

Conclusion

Congratulations, you've successfully completed this hands-on lab!