Geek Igor

Set up AWS Lambda with terraform

In the previous post, we’ve presented an AWS Lambda function to automatically invalidate resources in CloudFront distribution when underlying objects in an S3 bucket change.

Instead of clicking and copy-pasting in the AWS Console, we can use terraform to set this function up. In fact, I’ve already made it a part of my terraform static aws website — terraform module which sets up an S3 bucket to host a static website and CloudFront as a cache; it also handles a redirect www.domain.com --> domain.com and, provided with an AWS generated https cert, the https:// bit.

The file invalidate_cache.tf sets the lambda up. Please consult it for all the details. We’ll focus on the big picture here.

Let’s go through the resource we need to define.

Lambda resource diagram

In the short post, we’ve described AWS resources which we need to create for a simple lambda function to work. I hope this is useful to understand what happens behind the hood or if you want to terraform your own lambda.