Lambda functions on their own are pretty useless. Lambda’s need someone — or something — to initiate them. An important (and fun) trigger for Lambda is the CloudWatch event. With CloudWatch events we can trigger Lambda’s on a recurring schedule that we define.
Scheduled Lambda’s are useful for executing tasks like backups, or running security scanning. Today we’re going to go through what you need to do in order to Terraform Lambda Scheduled Event’s. We’ll cover everything you need to know about CloudWatch Events, CloudWatch Event Targets and Lambda permissions.
By the end of this article you’ll know how to execute a Lambda on a scheduled CloudWatch event (and write it all in Terraform).
Before we jump in to the detail of our Terraform Lambda scheduled event setup let’s just take a look at the three high level steps we’ll need to take.
- CloudWatch Event Rule — The way that we’ll setup our scheduling.
- Cloudwatch Target — How we’ll tell our scheduler to call our Lambda.
- Lambda Permission — We’ll allow the Lambda to be invoked by the CloudWatch Event.
Note: The following steps assume that you already have a lambda setup with the correct permissions to be invoked.
The Quick Solution
If you’re in a rush (for whatever reason) I’ll spoil the ending. You’ll need these three following resource blocks in order to setup your Terraform Lambda Scheduled Event.
But if you’re not in a rush and would like to know more about the steps, don’t fear. We’ll break each step you need to take and explain why very soon.
So that covers the end result, let’s break it down. For each step we’ll re-cap our Terraform configuration and then we’ll break down the what and why.
1. Setup your CloudWatch Event Rule
The first thing we need to setup is a CloudWatch Event Rule. How CloudWatch Events fit into the picture of triggering Lambda’s can be a confusing at first. But, once you understand what CloudWatch events are I think the whole thing becomes a little clearer.
CloudWatch Events allow us to listen to different changes (“events”) in our AWS environment and respond to these changes in certain ways. For instance, you may want to run a verification process following an EC2 backup, or you may want to receive an email following an S3 bucket update.
You can see CloudWatch Events in action below in the following screenshot. Notice how we can choose the different service name inputs and the event types that we want to listen to.
But at this point you might be thinking: We’re not listening to an event though? Aren’t we listening to a schedule? And you’d be right! Lucky for us (as you can see in the above screenshot) AWS has the concept of a CloudWatch event triggered on a custom schedule.
A custom schedule event has the same functionality, but instead it’s triggered on a time basis, not on an action taken in the AWS environment. Once we’ve created our event in CloudWatch it should look something like this…
Awesome — that’s our first step down. But our schedule is pointless if it doesn’t do anything. Let’s go ahead and make it do something interesting.
2. Set Your Lambda CloudWatch Event Target
Now that you’ve got your CloudWatch event, you’ve essentially got a scheduler. But in order to do anything with it we need to give it a target. A target is an invocation or an action that is taken following a CloudWatch event.
In our case we’re going to trigger a Lambda. You can also put your event onto a queue (SQS) or perform direct actions such as taking EC2 snapshots. And once we’ve added our target it should look something like this…
Sweet! That’s all we need to setup in CloudWatch (not so scary, right?). But there is one last piece, which is easily forgotten and that’s updating your Lambda permissions. So let’s go ahead and see what that’s about.
3. Set Your AWS Lambda Permissions
When you’ve opened up a Lambda in the console you might be familiar with the following graphic: the Lambda “designer”. The designer simply shows you what inputs you’ve got to your Lambda i.e what can initiate the Lambda and also your outputs i.e. what happens if it fails or succeeds.
In order to get our CloudWatch scheduled event to work we need to also add a Lambda permission. The permission simply tells our Lambda that it is allowed to be invoked by a given event source. In our case, CloudWatch events. It should look something like this…
And voila! That should be everything you need! If you view the logs of your Lambda you should now see your event initiating your Lambda on the schedule you defined. In our case, that’s every minute. Here’s an example log entry…
Terraform Lambda Scheduled Event’s!
And on that note we concludes today’s article. Hopefully that should give you everything you need to know in order to setup a Terraform Lambda Scheduled Event.
CloudWatch events are a great way to schedule your Lambda functions on a recurring basis. When you think about it there are so many different ways you can leverage scheduled functionality for different purposes — so you’ve got a great tool to start to experiment with.
I hope that helped clear the fog about AWS Lambda scheduled events so that you’ve got the start you needed to get everything setup.
Speak soon Cloud Native friend!
- Serverless on AWS Lambda: A Comprehensive Comparison Of Approaches (Serverless Framework vs SAM vs Terraform vs CloudFormation) - April 1, 2020
- 3 Simple Serverless Beginner Projects Ideas - March 23, 2020
- Misconceptions of Serverless: 5 Things You Thought AWS Lambda Did… But It Doesn’t. - March 16, 2020