Hard Lessons From Deploying Lambda Functions With Terraform

Because sometimes simple tasks require advanced techniques

Edoardo Nosotti
RockedScience

--

Photo by Hermes Rivera on Unsplash

I love Terraform for many reasons. One of them is the documentation. Oftentimes I only need to copy & paste snippets from the docs and change a few settings to deploy a resource.

So deploying a Lambda function on AWS should be as easy as using this snippet from the official documentation:

resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"

assume_role_policy = <<EOF
{
...
EOF
}

resource "aws_lambda_function" "test_lambda" {
filename = "lambda_function_payload.zip"
function_name = "lambda_function_name"
role = aws_iam_role.iam_for_lambda.arn
handler = "exports.test"

source_code_hash = filebase64sha256("lambda_function_payload.zip")

runtime = "nodejs12.x"

environment {
variables = {
foo = "bar"
}
}
}

…not really. At least, not in the real world. The source code repository of the official AWS provider does offer a better snippet, but still it does not cover some details that can save the day.

Learning to build effective Terraform configurations to deploy Lambda functions took me significantly more time than other resources usually do.
So I am sharing a few tips and…

--

--

Edoardo Nosotti
RockedScience

Certified Cybersecurity Analyst and Senior Cloud Solutions Architect. Passionate about IoT, AI, ML and automation.