🎓 A1Learner
🔍
0 of 50 lessons completed

Serverless: A Lambda Function

A Lambda function is serverless code that runs when an event arrives. There is no server to start or keep running.

A tiny Python handler

The handler is one small function. It returns a short message.

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": "Hello from Lambda"
    }

Zip the handler

Terraform needs a zip file, not a loose script. The archive_file data source builds it.

data "archive_file" "lambda_zip" {
  type        = "zip"
  source_file = "${path.module}/handler.py"
  output_path = "${path.module}/handler.zip"
}

Create the function

The function points at the zip file. The hash tells Terraform when the code has changed.

resource "aws_lambda_function" "hello" {
  function_name    = "hello-function"
  role             = aws_iam_role.lambda.arn
  handler          = "handler.lambda_handler"
  runtime          = "python3.12"
  filename         = data.archive_file.lambda_zip.output_path
  source_code_hash = data.archive_file.lambda_zip.output_base64sha256
}

Apply against the emulator

Start the local AWS emulator first. Then apply in the usual way.

export AWS_ACCESS_KEY_ID=test
export AWS_SECRET_ACCESS_KEY=test
terraform init
terraform apply -auto-approve

Example

# Start the emulator first: moto_server -p 5000
# handler.py
def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": "Hello from Lambda"
    }

# main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }
}

provider "aws" {
  region                      = "us-east-1"
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true
  endpoints {
    lambda = "http://localhost:5000"
    iam    = "http://localhost:5000"
    sts    = "http://localhost:5000"
  }
}

resource "aws_iam_role" "lambda" {
  name = "lambda-role"
  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Action    = "sts:AssumeRole"
      Effect    = "Allow"
      Principal = { Service = "lambda.amazonaws.com" }
    }]
  })
}

data "archive_file" "lambda_zip" {
  type        = "zip"
  source_file = "${path.module}/handler.py"
  output_path = "${path.module}/handler.zip"
}

resource "aws_lambda_function" "hello" {
  function_name    = "hello-function"
  role             = aws_iam_role.lambda.arn
  handler          = "handler.lambda_handler"
  runtime          = "python3.12"
  filename         = data.archive_file.lambda_zip.output_path
  source_code_hash = data.archive_file.lambda_zip.output_base64sha256
}

output "function_name" {
  value = aws_lambda_function.hello.function_name
}

# terraform init
# terraform apply -auto-approve
# terraform destroy -auto-approve

Key points

  • Lambda runs code in response to an event.
  • An event can be an upload, a timer, or a web request.
  • Terraform creates the function and its permissions together.
  • The handler code is zipped before Terraform uploads it.
  • The archive_file data source builds the zip file for you.
  • The function runs in the local emulator. No live AWS account is used.

The handler, the role, and the function are code and infrastructure in one apply. The zip is built by Terraform, so the code and the function stay in step. Destroy at the finish removes the function, the role and the zip.

Note

A Lambda function needs an IAM role before it can run.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What does the archive_file data source do in this lesson?