Learn when to choose Terraform or Serverless Framework for deploying AWS Lambda functions and related infrastructure based on your project's needs and complexity.
Choosing the right tool for deploying AWS Lambda functions can be crucial for your project's success. Both Terraform and Serverless Framework offer powerful capabilities, but their strengths lie in different areas.
When deciding between Terraform and Serverless Framework for AWS Lambda deployments, consider their strengths:
functions:
myFunction:
handler: index.handler
events:
- http:
path: /users
method: getresource "aws_lambda_function" "example" {
function_name = "my_function"
# ... other configurations
}For projects primarily focused on serverless components like Lambda functions, DynamoDB tables, and SNS topics, Serverless Framework simplifies deployment and configuration.
However, if your project involves extensive infrastructure management beyond serverless components, Terraform provides the flexibility and control needed.
You can even combine both tools:
This approach leverages the strengths of both tools, providing a robust and scalable solution for deploying and managing serverless applications on AWS.
This code demonstrates deploying a "Hello World" AWS Lambda function with an API Gateway endpoint using both Terraform and Serverless Framework. Terraform provisions the VPC and subnet, outputting the subnet ID. Serverless Framework defines the Lambda function, API Gateway endpoint, and references the Terraform output for the subnet. The Lambda function code returns a "Hello World" message. Deployment involves running terraform init and terraform apply in the Terraform directory, followed by npm install and sls deploy in the project root. This approach combines infrastructure management with serverless function deployment for a scalable solution.
This example demonstrates a basic setup using both Terraform and Serverless Framework to deploy a Lambda function with an API Gateway endpoint.
1. Project Structure:
serverless-terraform-demo/
├── serverless.yml
└── terraform/
└── main.tf
2. Terraform (terraform/main.tf):
# Configure the AWS Provider
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
provider "aws" {
region = "us-east-1" # Replace with your desired region
}
# Create a VPC
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
tags = {
Name = "serverless-terraform-demo-vpc"
}
}
# Create a subnet
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a" # Replace with your desired availability zone
tags = {
Name = "serverless-terraform-demo-subnet"
}
}
# Output the subnet ID for Serverless Framework to use
output "subnet_id" {
value = aws_subnet.main.id
}3. Serverless Framework (serverless.yml):
service: serverless-terraform-demo
provider:
name: aws
runtime: nodejs14.x
region: us-east-1 # Replace with your desired region
vpc:
subnetIds:
- ${terraform.subnet_id} # Reference the subnet ID from Terraform output
functions:
hello:
handler: handler.hello
events:
- http:
path: /hello
method: get
plugins:
- serverless-terraform-outputs
custom:
terraformOutputs:
# Specify the path to your Terraform output file
path: ./terraform/terraform.tfstate 4. Lambda Function Code (handler.js):
exports.hello = async (event) => {
return {
statusCode: 200,
body: JSON.stringify({
message: 'Hello from Serverless Framework and Terraform!',
}),
};
};Deployment Steps:
terraform/ directory.terraform init to initialize Terraform.terraform apply to create the VPC and subnet.npm install to install the necessary dependencies.sls deploy to deploy your Lambda function and API Gateway endpoint.This setup demonstrates a basic integration. You can expand this by:
This approach allows you to leverage the strengths of both tools, providing a robust and scalable solution for deploying and managing serverless applications on AWS.
Choosing the right tool depends on your project's specific needs and your team's expertise.
For simpler serverless projects, Serverless Framework offers a faster and more streamlined experience.
For complex projects requiring extensive infrastructure management, Terraform provides the necessary control and flexibility.
Combining both tools can offer the best of both worlds, allowing you to leverage their respective strengths for a robust and scalable solution.
| Feature | Serverless Framework | Terraform |
|---|---|---|
| Focus | Serverless-centric resources (Lambda, API Gateway, DynamoDB, etc.) | Broader infrastructure (VPC, subnets, security groups, etc.) |
| Strengths | - Simplified deployment and configuration for serverless applications - Easy integration with serverless services |
- Fine-grained control over infrastructure - Ideal for larger, non-serverless specific deployments |
| Code Example | YAML-based configuration for functions, events, etc. | Declarative code for defining resources like Lambda functions |
| Best Use Case | Projects primarily focused on serverless components | Projects requiring extensive infrastructure management beyond serverless |
Combination:
This approach leverages the strengths of both tools for a robust and scalable solution.
Ultimately, the choice between Terraform and Serverless Framework for AWS Lambda deployments depends on your project's specific needs and your team's familiarity with each tool. Serverless Framework excels in deploying serverless-centric applications, simplifying configurations and integrations with AWS services like Lambda, API Gateway, and DynamoDB. On the other hand, Terraform shines in managing broader infrastructure, offering fine-grained control over resources like VPCs, subnets, and security groups, making it suitable for larger, more complex deployments. A hybrid approach, using Terraform for base infrastructure and Serverless Framework for serverless components, can leverage the strengths of both tools. This allows teams to benefit from Terraform's robust infrastructure management capabilities and Serverless Framework's streamlined serverless deployments. Carefully evaluate your project requirements, team expertise, and the pros and cons of each tool to make an informed decision that sets your project up for success.
Can I access outputs from custom resources as variables in ... | I’m creating an AWS elasticsearch domain as a custom resource and I want to be able to pass the domain endpoint into my lambda functions. Is there a way to access a property of a generated resource as a variable in serverless.yml? I’m guessing not as the endpoint wouldn’t be generated until the CF deployment is done by which time I’d expect that the value couldn’t be made available to serverless-plugin-write-env-vars plugin that I’m using to setup the env vars for the lambda functions. I’m ho...
Creating AWS Resources via Severless Framework - Serverless ... | Hi everyone. Being new to the serverless and AWS, I was wondering how you’d recommend creating the resources required by the application. Is it possible to have the resources portion of the severless.yml trigger the creation of resources if they don’t exist? Example - Creating DynamoDB tables that are required in serverless.yml Thanks!
Leveraging Infrastructure as Code (IaC) for AWS Lambda: A ... | In recent years, Infrastructure as Code (IaC) has emerged as a fundamental practise in cloud...
Splitting big serverless.yml? - Serverless Framework - Serverless ... | Inside the serverless.yml file, there are lots of things : backing services (which for me consist of rds, elasticsearch, 3rd parties API keys, …) networking config (the VPCs) functions and events (the interesting part) The backing services and networking does not change a lot. The functions change every time we add a new service to our platform (we have one lambda per service) Is there a way to store one yml file per service ? That is, we’d like the following structure project |-serverles...
EFS Creation Fails - Serverless Framework - Serverless Forums | Hi all - I’m having an issue where I’m defining a new EFS instance (for FS and AP) in the resources section of my serverless.yml file. I then reference the FileSystem in one of my functions. However, the problem I’m having is that when the function attempts to deploy the access point is not yet ready and the function fails, which then rolls back the stack. I can sort of work around this by commenting out all of the functions and just deploying the resources, but this is not a viable solution. ...
Serverless at (Team) Scale | AJ Stuyvenberg | Reduce complexity, cognitive load, and deployment times by splitting cloud infrastructure from lambda code, using CloudFormation Outputs to share resources, and use SSM to play nice with Terraform and other IaC tools. - 8 mins