How To Setup AWS Lambda Scheduled Events with Terraform
Tags: aws, cloud, serverless, devops
Published: 2020-01-21 08:01:00

---
title: How To Setup AWS Lambda Scheduled Events with Terraform
published: true
date: 2020-01-21 07:55:40 UTC
tags: aws, cloud, serverless, devops
canonical_url: https://www.thedevcoach.co.uk/terraform-lambda-scheduled-event/
---

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.

![AWS Lambda](https://i1.wp.com/www.thedevcoach.co.uk/wp-content/uploads/2020/01/aws-lambda.png?fit=760%2C489&ssl=1)

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.

1. **CloudWatch Event Rule** — The way that we’ll setup our scheduling.
2. **Cloudwatch Target** — How we’ll tell our scheduler to call our Lambda.
3. **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.

 If you’ve not got a Terraform setup, I’d recommend checking out: [Create An AWS S3 Website Using Terraform And Github Actions](https://www.thedevcoach.co.uk/terraform-github-actions/) 

## 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.

{% gist https://gist.github.com/loujaybee/bd98e9bff7430026ba639d4539d819c2.js %}

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.

If you’re not familiar with Terraform check out the free email course: [Terraform Kick Start](https://terraform.thedevcoach.co.uk) 

## 1. Setup your CloudWatch Event Rule

{% gist https://gist.github.com/loujaybee/691fc2ff5df367402af4842cd52f76bd.js %}

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.

 ![CloudWatch Event Setup](https://i0.wp.com/www.thedevcoach.co.uk/wp-content/uploads/2020/01/Screenshot-2020-01-21-at-07.08.03.png?resize=760%2C346&ssl=1)

CloudWatch Event Setup

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…

 ![CloudWatch Event](https://i2.wp.com/www.thedevcoach.co.uk/wp-content/uploads/2020/01/Screenshot-2020-01-20-at-07.54.22.png?resize=760%2C291&ssl=1)

CloudWatch Event

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

{% gist https://gist.github.com/loujaybee/b6432f15599724da7ea0446e6b6ac00a.js %}

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…

 ![CloudWatch Event Trigger](https://i0.wp.com/www.thedevcoach.co.uk/wp-content/uploads/2020/01/Screenshot-2020-01-21-at-06.55.50.png?resize=760%2C409&ssl=1)

CloudWatch Event Trigger

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

{% gist https://gist.github.com/loujaybee/63495d692e5f9c60e0e4a1a19e0c4735.js %}

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…

 ![AWS Lambda CloudWatch Event Source](https://i1.wp.com/www.thedevcoach.co.uk/wp-content/uploads/2020/01/Screenshot-2020-01-21-at-06.48.52.png?resize=760%2C170&ssl=1)

AWS Lambda CloudWatch Event Source

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…

 ![CloudWatch Scheduled Event Log](https://i1.wp.com/www.thedevcoach.co.uk/wp-content/uploads/2020/01/Screenshot-2020-01-20-at-07.53.05.png?resize=750%2C338&ssl=1)

CloudWatch Scheduled Event Log

## 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!

---

The post [How To Setup AWS Lambda Scheduled Events with Terraform](https://www.thedevcoach.co.uk/terraform-lambda-scheduled-event/) appeared first on [The Dev Coach](https://www.thedevcoach.co.uk).

Lou is the editor of [The Cloud Native Software Engineering Newsletter](https://newsletter.thedevcoach.co.uk) a Newsletter dedicated to making Cloud Software Engineering more accessible and easy to understand, every 2 weeks you’ll get a digest of the best content for Cloud Native Software Engineers right in your inbox.