🐶
Terraform

Terraform vs Serverless: Choosing for AWS Lambda Deployment

By Ondřej Dolanský on 12/22/2024

Learn when to choose Terraform or Serverless Framework for deploying AWS Lambda functions and related infrastructure based on your project's needs and complexity.

Terraform vs Serverless: Choosing for AWS Lambda Deployment

Table of Contents

Introduction

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.

Step-by-Step Guide

When deciding between Terraform and Serverless Framework for AWS Lambda deployments, consider their strengths:

  • Serverless Framework excels at deploying and managing serverless-centric resources. It simplifies Lambda deployments, API Gateway configurations, and integrations with DynamoDB, SNS, etc.
functions:
  myFunction:
    handler: index.handler
    events:
      - http:
          path: /users
          method: get
  • Terraform shines in managing broader infrastructure, including VPCs, subnets, security groups, and more complex resources. It offers fine-grained control and is ideal for larger, non-serverless specific deployments.
resource "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:

  • Use Terraform to define the base infrastructure (VPC, subnets, etc.).
  • Utilize Serverless Framework to deploy and manage the serverless application components within the infrastructure created by Terraform.

This approach leverages the strengths of both tools, providing a robust and scalable solution for deploying and managing serverless applications on AWS.

Code Example

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:

  1. Terraform:
    • Navigate to the terraform/ directory.
    • Run terraform init to initialize Terraform.
    • Run terraform apply to create the VPC and subnet.
  2. Serverless Framework:
    • Navigate to the root directory of your project.
    • Run npm install to install the necessary dependencies.
    • Run sls deploy to deploy your Lambda function and API Gateway endpoint.

This setup demonstrates a basic integration. You can expand this by:

  • Defining more complex infrastructure with Terraform (security groups, databases, etc.).
  • Leveraging Terraform outputs for dynamic configuration in Serverless Framework.
  • Implementing CI/CD pipelines for automated deployments.

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.

Additional Notes

  • Ease of Use: Serverless Framework is generally considered more beginner-friendly for serverless deployments due to its simpler syntax and abstractions. Terraform requires a deeper understanding of infrastructure concepts.
  • Learning Curve: While both tools have learning curves, Terraform's is steeper due to its broader scope and more complex configuration language (HCL).
  • Community & Ecosystem: Both tools have active communities and extensive documentation. Serverless Framework has a strong focus on serverless, while Terraform has a broader reach across different cloud providers.
  • State Management: Both tools offer state management, crucial for tracking infrastructure changes. Terraform typically stores state remotely, while Serverless Framework defaults to local storage (can be configured for remote storage).
  • Debugging & Troubleshooting: Debugging can be more challenging with infrastructure-as-code tools. Both offer debugging capabilities, but understanding the underlying cloud resources is essential.
  • Cost: Both tools are open-source and free to use. However, improper infrastructure management with either tool can lead to unexpected cloud costs.
  • Alternative Tools: Consider other tools like AWS SAM (Serverless Application Model) for serverless deployments or Pulumi for a code-first approach to infrastructure management.

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.

Summary

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:

  • Use Terraform for base infrastructure setup.
  • Use Serverless Framework to deploy and manage serverless components within the Terraform-defined infrastructure.

This approach leverages the strengths of both tools for a robust and scalable solution.

Conclusion

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.

References

  • Serverless or Terraform? : r/aws Serverless or Terraform? : r/aws | Posted by u/UnFukWit4ble - 12 votes and 28 comments
  • Can I access outputs from custom resources as variables in ... 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...
  • How to deploy, and maintain AWS Lambda functions? Terraform, or ... How to deploy, and maintain AWS Lambda functions? Terraform, or ... | Posted by u/matlau_286 - 17 votes and 21 comments
  • Creating AWS Resources via Severless Framework - Serverless ... 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 ... 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 ... 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...
  • The definitive guide to using Terraform with the Serverless Framework The definitive guide to using Terraform with the Serverless Framework | Wondering when to use Terraform and/or the Serverless Framework for your infrastructure as code? We've got answers.
  • EFS Creation Fails - Serverless Framework - Serverless Forums 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 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

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait