How to deploy Lambda using Terraform

Erik A. Ekberg
ClickFlow
Published in
5 min readMay 25, 2021

Deploying our code at ClickFlow using AWS Lambda functions via Terraform has transformed our team’s cycle time from weeks to days. This has let our small startup engineering team focus more on business opportunities and value without the technical overhead of traditional server architectures. In this article, we will walk though

  1. What is Terraform?
  2. How can we use Terraform to deploy AWS Lambda functions?
  3. Why should we use Terraform over other out-of-the-box serverless frameworks?
  4. An example AWS Lambda deployment using Terraform

What is Terraform?

Terraform is a robust Infrastructure as Code (IaC) tool that lets engineering teams easily and safely deploy code and manage cloud infrastructure. Terraform can plug into all of the major providers like AWS or Google while also letting companies publish their own community plugins. This open-sourced framework pioneered by HashiCorp codifies cloud infrastructure so that it is fast, consistent, and accurate through configuration files. Configuration files which can be committed to version control systems to setup, teardown, or scale cloud resources as part of a CICD pipeline. Ultimately, Terraform strives to be a standard between engineering teams and cloud providers without being locked to a specific technology or platform. Instead, Terraform lets engineers plug, play, and connect the right cloud resources for their given use case with IaC.

How can we use Terraform to deploy AWS Lambda functions?

AWS Lambda is one of the many AWS resources Terraform can plug into. Terraform can bundle our code into a zip file and then upload that zip file to AWS on demand. This simplicity makes Terraform just as easy as other all-in-one serverless frameworks to deploy lambda functions.

Why should we use Terraform over other out-of-the-box serverless frameworks?

All-in-one serverless frameworks tightly couple IaC, local development, and your deployment process to the framework. This coupling can make it difficult to inject credentials or change how you develop your lambda functions local. Terraform does not do that. Terraform only handles the release process of your lambda function without any side effects in local development or your deployment process.

Additionally, all frameworks require some degree of vendor lock-in. Terraform in my opinion is the most robust and adaptable framework at the moment to manage IaC in general.

Example AWS Lambda deployment using Terraform

How to create a lambda function

AWS Lambda is broken into 2 main parts: our handler and perform functions. Our perform function is our business logic we want AWS to run whenever our lambda is called while our handler function is the function exposed to AWS to actually run our lambda. To put this into more concrete terms, lets start by building a simple “Hello world” lambda with a single index.js file as describe in the image below.

Reviewing our index.js file above we can see our handler and perform functions in action. Looking at our perform action, we see that whenever our lambda is run we wait 5 seconds and then output “Hello world from: …” into our console (our important business logic). While we export our handler function so that AWS can hook into it and then in turn run our perform.

Some like to merge the handler and perform functions into a single function instead of splitting them up. This is perfectly acceptable: I just choose to keep them apart as I feel it improves readability.

Now with our small “Hello world” lambda built we can start working on our Terraform files.

Deploying our Lambda with Terraform

Before we dive into Terraform, we will need AWS credentials to let Terraform provision AWS resources. If you need AWS credentials, I recommend creating a new AWS IAM role following the steps below. This will give you a AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY which we will plug into Terraform.

Now with our AWS credentials we can setup Terraform to bundle and deploy our “Hello world” code to lambda. First, we create a main.tf file and plug in the code below.

This terraform and provider "aws" block will version lock our Terraform packages/modules and permit Terraform to provision AWS resources later on.

To get our “Hello world” code from our local computer uploaded to AWS, we will have Terraform create a zip file and then upload that zip file to AWS Lambda. To make the code easier to follow and see what options we can play with we will define some locals in our main.tf to define some of the parameters we will be plugging out our AWS Lambda configuration later on.

With our parameters like our lambda runtime clearly defined, we now move into bundling and uploading our code described step-by-step below.

With all the above setup, we can now run terraform apply to deploy our “Hello World” code. Once we are done, we can run terraform destroy to teardown all the AWS resources Terraform created.

As an optional step, instead of writing this out by hand every time at ClickFlow we created a one-stop-shop reusable Terraform module to easily deploy our AWS Lambda functions described below.

Source code: https://github.com/click-flow/examples/tree/main/articles/how-to-deploy-lambda-using-terraform

--

--

Erik A. Ekberg
ClickFlow

Software engineer with a background in human psychology and data analytics who affords both customer and engineer delight through Agile software architectures.