aws lambda terraform is a method of defining and deploying AWS Lambda functions using Infrastructure as Code (IaC) via HashiCorp Terraform. It enables automated, repeatable creation of serverless functions with version-controlled configurations.
Terraform uses declarative configuration files written in HashiCorp Configuration Language (HCL) to define AWS resources. When applied, Terraform provisions or updates AWS Lambda functions based on the specified code. This eliminates manual setup and reduces configuration drift across environments.
For example, a single Terraform file can declare a Lambda function with 512 MB memory, a 10-second timeout, a Python runtime, and an IAM role granting access to S3. Running terraform apply creates the function in AWS exactly as described. Changes to the configuration trigger updates during subsequent runs.
Terraform operates in a state-driven model. You write configuration describing the desired infrastructure state. Terraform compares this to the current state stored in a backend (like S3 or Terraform Cloud) and calculates the minimal set of actions needed to reach the target.
When you run terraform plan, Terraform generates a detailed execution plan showing which resources will be created, modified, or destroyed. This allows review before changes are applied. Once approved, terraform apply executes the plan against the AWS API.
Each Lambda function deployed via Terraform is tied to a specific configuration file. If multiple teams manage different functions, each can maintain their own module directory, reducing conflicts and enabling independent deployment cycles.
Deploying AWS Lambda via Terraform requires several key elements:
The following table shows common configuration patterns:
| Use Case | Terraform Resource | Key Attribute Example |
|---|---|---|
| Basic function | aws_lambda_function |
handler = "index.handler" |
| Environment variables | environment block |
variables = { "LOG_LEVEL" = "info" } |
| External triggers | aws_lambda_event_source_mapping |
event_source_arn = aws_sqs_queue.queue.arn |
| Code packaging | archive_file |
output_path = "lambda.zip" |
Using modules improves reusability. A shared lambda_function module can enforce naming standards, logging, and monitoring across projects. Teams deploy consistent functions without rewriting configuration.
Many assume Terraform manages Lambda function code directly. That's incorrect. Terraform only declares the function's configuration and uploads the code package. The actual code remains in your local project or CI/CD pipeline.
Another misconception is that Terraform replaces AWS CloudFormation. They serve similar purposes but differ in design. Terraform supports multi-cloud environments and uses a different state model. CloudFormation is AWS-native and integrates tightly with AWS services like CloudWatch and Step Functions.
A critical distinction: Terraform does not execute Lambda functions. It only provisions the infrastructure. Function execution happens when triggered by events such as API calls, scheduled tasks, or S3 uploads. The deployment tool has no control over runtime behavior.
Suppose you need a Lambda function to process CSV files uploaded to an S3 bucket. The function reads the file, validates rows, and writes results to another bucket.
Here's a simplified Terraform configuration:
provider "aws" {
region = "us-east-1"
}
resource "aws_lambda_function" "process_csv" {
filename = "function.zip"
function_name = "process-csv"
role = aws_iam_role.lambda_exec.arn
handler = "index.handler"
runtime = "python3.9"
timeout = 300
memory_size = 512
source_code_hash = filebase64sha256("function.zip")
}
resource "aws_iam_role" "lambda_exec" {
name = "lambda-exec-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
Action = "sts:AssumeRole"
}]
})
}
resource "aws_iam_policy" "lambda_s3_access" {
name = "lambda-s3-access"
policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Action = [
"s3:GetObject",
"s3:PutObject"
]
Resource = ["arn:aws:s3:::input-bucket/*", "arn:aws:s3:::output-bucket/*"]
}]
})
}
resource "aws_iam_role_policy_attachment" "attach_policy" {
role = aws_iam_role.lambda_exec.name
policy_arn = aws_iam_policy.lambda_s3_access.arn
}
resource "aws_lambda_event_source_mapping" "trigger_from_s3" {
event_source_arn = "arn:aws:s3:::input-bucket"
function_name = aws_lambda_function.process_csv.function_name
enabled = true
}
This configuration defines a Lambda function with proper IAM roles, S3 access, and event triggers. Running terraform apply creates the function and sets up the S3 trigger. The function runs automatically whenever a new file is uploaded to the input bucket.
I've used this pattern in production for processing daily sales reports. The same configuration deploys identically across staging and production environments. No manual steps were involved.
Always store Terraform state remotely. Using backend "s3" prevents local state corruption and enables team collaboration. State files contain sensitive details like ARNs and credentials.
Never commit raw secrets to configuration files. Use AWS Systems Manager Parameter Store or Secrets Manager with changing retrieval via data "aws_ssm_parameter".
Use terraform fmt and terraform validate in CI pipelines. These catch syntax errors early and ensure consistency across contributors.
A common mistake is forgetting to update the source_code_hash when changing function code. If the hash doesn't match, Terraform won't redeploy even if the code changed. Always regenerate the hash after updating the ZIP file.
For complex deployments, split configurations into modules. Use module "lambda" blocks to encapsulate reusable components. This keeps root directories clean and enables reuse across teams.
aws lambda terraform is not a replacement for operational tools like AWS CloudWatch or third-party observability platforms. It handles infrastructure setup, not monitoring, logging, or error tracking. Combine it with dedicated tools for full visibility into function performance.
Using aws lambda terraform ensures consistent, auditable, and reproducible deployments. It shifts focus from manual configuration to defined system behavior, reducing human error and improving team velocity.