Learn how to create your EBS Snapshots Using AWS Lambda.

How to Create Snapshots Using AWS Lambda

AWS Lambda is a relatively new service offered by Amazon AWS. In this article, I'll explain what it is, its advantages and use cases, and how can it help you to backup your EBS volumes.

What Is AWS Lambda?

 AWS Lambda is a serverless compute service offered by Amazon Web Services (AWS). It is a computing platform that enables the running of code with no need to provision or manage a server or an operating system. The languages supported by AWS Lambda include Java, Python, Node.js, and C#.

To use Lambda, you load your code into the system and set a trigger that starts its execution. This is done by writing a programming function that is triggered to respond to events in your Amazon AWS environment. This function is called AWS Lambda function. AWS Lambda assigns for each Lambda function its own isolated computing environment. You pay for the service only when your application is running. There are no server-related costs. AWS Lambda can replace the traditional Amazon virtual machines (VMs) service, Amazon Elastic Compute Cloud (EC2).

When you use AWS Lambda you do not have to worry about a computing platform. Amazon manages the servers for you. A developer using this service can focus only on writing and testing the code. This brings the advantages of faster development and costs reduction. You are charged only for the number of requests served and compute time.

AWS Lambda Use Cases

 Here are some examples of AWS Lambda common use cases:

  • Personalized content generation—you can create a Lambda function that is triggered by various actions of your web site users. User operations such as sign-up, sign-in, search and confirmation will trigger the Lambda function to generate customized content to be presented to the user. It can make authentication more secure, and send personalized messages.
  • Media transformation—Lambda function can be used to adjust the media content on your web site or mobile application to the user's device and connection quality. Triggered by the event of a user accessing your media content, the Lambda function will change parameters like image size or media format to best match your user device, browser characteristics, and the available bandwidth.
  • Chatbot—creating and maintaining a chatbot on your web site is complicated and expensive. It consumes a lot of computation resources. You can create a Lambda function that is triggered each time a user interacts with the chatbot. When using the Lambda function, you don't need to maintain expensive resources. You will be charged only when your chatbot application is running.

How to Create EBS Snapshots Using AWS Lambda

You can use AWS Lambda to automate and customize your Amazon AWS backup procedures. With AWS Lambda you can have your own backup script that can back up your data sets automatically triggered by predefined events. The events can be based on time periods. For example, AWS Lambda backup function can be triggered to execute a specific task every 30 minutes, every 12 hours or at a specific time in the day. It can be also triggered by AWS event, such as when new data is written to Amazon S3.

A cheap way to back up your EBS volumes is taking snapshots, using Amazon EBS snapshots service. Snapshots are creating incremental backups that contain your full data at a specific point in time. They can be used to restore an EBS volume to a new one. With AWS Lambda you can automate the process of taking snapshots. For example, you can create Lambda function that snapshots your database every night. After your backup Lambda function is done, it can trigger another Lambda function that copies the snapshot to your backup account.

Amazon AWS provides EBS volume snapshot APIs that take a snapshot of a volume attached to EC2 instance. When a snapshot of a root volume is taken, AWS recommends stopping the instance it is attached to while the snapshot is taken. Any other volume's snapshot can be taken without this restriction Amazon CloudWatch control and monitors all activities in your EC2 environments. The events logged in CloudWatch, such as an instance being stopped, can be used as a trigger to a Lambda function. The Lambda function can be triggered by events reported by CloudWatch. Snapshots of multiple volumes can be done in parallel.

Here are the things you need to define in AWS in order to create the automated snapshots Lambda function:

  • IAM role—AWS Identity and Access Management (IAM) role is an AWS identity you need to create for your Lambda function. It sets the permissions of the Lambda function.
  • Lambda function—create a Lambda function. You can refer to Amazon AWS Lambda guide to learn how to create a Lambda function and add your code into it.
  • CloudWatch event rules—create events in Amazon CloudWatch that will be used to trigger your Lambda function.

Here are a few examples to event rules that you can create in Amazon CloudWatch and use in your Lambda function:

  • Stop EC2 instance once a day, at a specific time. This will enable a snapshot process of a root volume that is attached to this instance.
  • When the EC2 instance has stopped, trigger the Lambda function to create snapshot to the root volume attached to it.
  • When a snapshot of a volume is successfully created, trigger the Lambda function to restart the EC2 instance attached to this volume.
  • When a snapshot of a volume has failed, trigger the Lambda function to send an email notification, and restart the instance attached to this volume.

Wrap Up

AWS Lambda is a serverless event-driven computing platform. Besides its advantages for facilitating the execution of code, it can be used for creating customized scripts to control your EC2 environment. In this article, you've learned how to automate the creation of your snapshots with AWS Lambda.

Gilad David Maayan is a technology writer who has worked with over 150 technology companies including SAP, Samsung NEXT, NetApp and Imperva, producing technical and thought leadership content that elucidates technical solutions for developers and IT leadership.