DEV Community

Cover image for Leveraging Infrastructure as Code (IaC) for AWS Lambda: A Comparative Analysis of AWS SAM, Terraform, and Serverless Framework
Kasun de Silva for AWS Community Builders

Posted on

Leveraging Infrastructure as Code (IaC) for AWS Lambda: A Comparative Analysis of AWS SAM, Terraform, and Serverless Framework

In recent years, Infrastructure as Code (IaC) has emerged as a fundamental practise in cloud computing, allowing developers and operations teams to manage and provision infrastructure using code rather than manual processes. This shift has revolutionised the way applications are deployed and managed, especially in serverless environments like AWS Lambda. In this article, we’ll explore the pros and cons of using IaC tools such as AWS SAM, Terraform, and the Serverless Framework for AWS Lambda deployments.

AWS SAM (Serverless Application Model)


  1. Native AWS Integration: AWS SAM is designed specifically for AWS services, providing seamless integration with the AWS ecosystem. This allows for simplified configuration and deployment of serverless applications.
  2. Simplified YAML Syntax: AWS SAM uses a straightforward YAML-based syntax, making it easy to define serverless resources, such as functions, APIs, and event sources, in a concise and human-readable format.
  3. Built-in Local Testing: AWS SAM provides a local testing environment, enabling developers to test Lambda functions and APIs locally before deploying them to the AWS cloud. This speeds up development cycles and improves code quality.
  4. Automated Packaging and Deployment: AWS SAM CLI (Command Line Interface) simplifies the packaging and deployment process by handling the creation of deployment packages and the uploading of artifacts to AWS S3.
  5. Template Validation: AWS SAM CLI provides a built-in validation tool that checks the syntax and structure of SAM templates, helping to catch errors early in the development process.


  1. Limited Multi-Cloud Support: AWS SAM is tightly integrated with AWS services, which means it’s primarily optimised for AWS deployments. If you require multi-cloud support, you may need to supplement it with additional tools.
  2. Lack of Advanced Features: AWS SAM may not offer as many advanced features or customizations as other IaC tools like Terraform. It’s designed to streamline the deployment of serverless applications within AWS’s ecosystem.

Deploying a Hello World Lambda Function using AWS SAM

# template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

    Type: AWS::Serverless::Function
      Handler: index.handler
      Runtime: nodejs14.x
      CodeUri: ./
      FunctionName: HelloWorldFunction
      MemorySize: 128
      Timeout: 3
Enter fullscreen mode Exit fullscreen mode

In this example, we’re using AWS SAM to define a basic Lambda function. This template specifies the AWS resources needed for the function, including its name, runtime, and code location.

To deploy this using the AWS SAM CLI, you would run:

sam build
sam deploy --guided
Enter fullscreen mode Exit fullscreen mode



  1. Multi-Cloud Support: Terraform is a cloud-agnostic IaC tool, allowing you to provision and manage resources across multiple cloud providers, including AWS, Azure, Google Cloud, and more. This makes it a powerful choice for organisations with diverse cloud environments.
  2. Extensive Provider Ecosystem: Terraform boasts a large and active provider ecosystem, which means it supports a wide range of cloud services and other infrastructure providers. This provides flexibility in resource provisioning.
  3. Declarative Configuration: Terraform uses a declarative configuration language, HashiCorp Configuration Language (HCL), which allows you to specify the desired state of your infrastructure. It automatically manages the dependencies and execution order.
  4. State Management: Terraform keeps track of the state of your infrastructure, allowing for easier collaboration and change management across teams.


  5. Learning Curve: Terraform may have a steeper learning curve compared to AWS SAM, especially for those new to the tool or IaC in general. It requires a deeper understanding of infrastructure concepts.

  6. Complexity for Simple Tasks: While powerful, Terraform may be overkill for simple serverless deployments, where a more specialised tool like AWS SAM may provide a more streamlined experience.

    Deploying a Hello World Lambda Function using Terraform


provider "aws" {
  region = "us-east-1"

data "archive_file" "lambda_zip" {
  type        = "zip"
  source_dir  = "./lambda-function"
  output_path = "./"

resource "aws_lambda_function" "hello_world" {
  function_name = "hello-world-lambda"
  handler       = "index.handler"
  runtime       = "nodejs14.x"
  filename      = ""
  role          = aws_iam_role.lambda.arn

  source_code_hash = filebase64sha256("")

  environment {
    variables = {
      ENV_VAR = "Value"

resource "aws_iam_role" "lambda" {
  name = "lambda-role"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
        Action = "sts:AssumeRole"
        Effect = "Allow"
        Principal = {
          Service = ""

resource "aws_iam_role_policy_attachment" "lambda" {
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
  role       =
Enter fullscreen mode Exit fullscreen mode

In this Terraform example, we’re defining an AWS Lambda function along with the required IAM role and policy. We’re also setting an environment variable for the Lambda function.

To deploy this using Terraform, you would run:

terraform init
terraform apply
Enter fullscreen mode Exit fullscreen mode

Serverless Framework


  1. Platform Agnostic: Similar to Terraform, the Serverless Framework is not tied to any specific cloud provider. It supports AWS, Azure, Google Cloud, and other providers, making it suitable for organisations with diverse cloud environments.
  2. Rich Plugin Ecosystem: Serverless Framework has a robust plugin ecosystem that extends its capabilities, allowing for a wide range of integrations and customisations.
  3. Deployment Pipelines and Workflows: The Serverless Framework provides features for setting up deployment pipelines and workflows, which can be essential for complex applications with multiple environments.
  4. Community Support: Serverless Framework has a large and active community, which means there are plenty of resources, tutorials, and plugins available to support your deployments. ### Cons:
  5. Less Opinionated: While flexibility is an advantage, Serverless Framework may be less opinionated compared to AWS SAM, which can lead to more customization but may require additional effort in configuration.
  6. Steep Initial Setup: Setting up a project in the Serverless Framework may take more time compared to using a more specialised tool like AWS SAM.

Deploying a Hello World Lambda Function using Serverless Framework

# serverless.yml

service: hello-world-service

  name: aws
  runtime: nodejs14.x

    handler: handler.hello
Enter fullscreen mode Exit fullscreen mode

In this Serverless Framework example, we’re defining a service with one function (helloWorld) that uses the Node.js 14.x runtime.

To deploy this using the Serverless Framework, you would run:

serverless deploy
Enter fullscreen mode Exit fullscreen mode

AWS SAM is excellent for AWS-centric environments, offering seamless integration with AWS services. It’s especially advantageous if you’re primarily working within the AWS ecosystem.

Terraform shines in multi-cloud environments, allowing you to manage resources across various cloud providers. It provides extensive flexibility and control, making it ideal for complex infrastructures.

Serverless Framework offers a balance between flexibility and ease of use. It’s platform-agnostic, making it suitable for organisations with diverse cloud environments.

I personally prefer Terraform as my go-to IaC, but ultimately, the choice depends on your specific needs, team familiarity, and the complexity of your serverless applications. Each tool brings its strengths to the table, enabling you to optimise your serverless deployments for success.

Top comments (0)