DEV Community

Cover image for AWS SAM vs. Serverless Framework: Choosing the Right Framework for Serverless Development
necologicLabs
necologicLabs

Posted on

AWS SAM vs. Serverless Framework: Choosing the Right Framework for Serverless Development

Introduction

The Challenge

As serverless development becomes more popular, many developers rely on frameworks like AWS SAM and Serverless Framework to build applications. However, deciding which tool to use can be challenging, especially when their functionalities overlap.

Purpose of This Article

This article compares AWS SAM and Serverless Framework, highlighting their features, use cases, and strengths. By the end, you’ll be able to choose the best tool for your project.


Background: The Growth of Serverless Architecture

Serverless architecture has become a compelling choice in recent years, offering numerous advantages.

1. The Evolution of Cloud Technology

Cloud technology has evolved through three significant phases:

  1. Virtual Machines (VMs): Easy to migrate but require infrastructure management.
  2. Containers: Lightweight virtualization enhancing portability and scalability.
  3. Serverless: Offloads all infrastructure management to cloud providers, allowing developers to focus solely on application logic.

2. Business Drivers Behind Serverless Adoption

Enterprises prioritize:

  • Development Speed: Focus on building applications without managing runtime environments.
  • Cost Efficiency: Pay-as-you-go pricing minimizes wasted resources.
  • Scalability: Built-in autoscaling adjusts automatically to traffic demands.

3. Technical Benefits

Serverless architecture offers:

  • Automated Scaling: Handles concurrent executions seamlessly.
  • Improved Productivity: Developers focus on logic, not infrastructure.
  • Compatibility: Works well with modern architectures like microservices and event-driven systems.

4. Cloud Ecosystems

AWS Lambda, API Gateway, and DynamoDB exemplify how serverless ecosystems enable rapid development. Similar offerings from Google Cloud and Azure further validate serverless as a mainstream choice.


Why AWS SAM and Serverless Framework?

Key Reasons

  1. Market Presence:

    • AWS SAM: AWS’s official tool for serverless applications.
    • Serverless Framework: A leading choice for multi-cloud environments.
  2. Developer Support:

    • AWS SAM integrates deeply with AWS resources.
    • Serverless Framework provides extensive plugins and community-driven support.
  3. Complementary Use Cases: These tools cater to distinct needs, making them excellent candidates for comparison.


Points of Comparison

  1. Implementation and Development Efficiency: How each tool structures applications and supports productivity.
  2. Deployment and Operational Flexibility: Differences in deployment workflows and constraints.
  3. Integration with AWS Services: SAM’s AWS-centric features versus Serverless Framework’s flexibility.

Sample Application: Task Management

Overview

This sample application demonstrates:

  • DynamoDB: Manages task data (e.g., id, task, status).
  • API Gateway and Lambda: Provides REST endpoints.
  • DynamoDB Streams and SNS: Sends email notifications for task status updates.

Image description

API Endpoints

Method URL Description
GET /task?id=1 Retrieves task by ID.
POST /task Updates or creates a task.

AWS SAM: AWS-Centric Power

Strengths

  • AWS Integration: Seamless support for AWS services like DynamoDB and SNS.
  • Robust Local Testing: High fidelity testing with sam local.
  • Best Use Case: Ideal for AWS-heavy environments.

Limitations

  • Limited support for non-AWS services.
  • Complex configurations may require familiarity with CloudFormation.

Template

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

Description: >
  sam-task-app - A simple task management application with DynamoDB, API Gateway, and SNS.

Resources:
  TaskTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: TaskTable
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      BillingMode: PAY_PER_REQUEST
      StreamSpecification:
        StreamViewType: NEW_AND_OLD_IMAGES

  TaskApiFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: task-api/app.lambdaHandler
      Runtime: nodejs18.x
      CodeUri: task-api/
      Environment:
        Variables:
          TABLE_NAME: TaskTable
      Policies:
        - DynamoDBCrudPolicy:
            TableName: TaskTable
      Events:
        TaskApi:
          Type: Api
          Properties:
            Path: /task
            Method: any

  StreamHandlerFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: stream-handler/app.lambdaHandler
      Runtime: nodejs18.x
      CodeUri: stream-handler/
      Environment:
        Variables:
          TOPIC_ARN: !Ref NotificationTopic
      Policies:
        - Statement:
            Effect: Allow
            Action: sns:Publish
            Resource: !Ref NotificationTopic
      Events:
        StreamEvent:
          Type: DynamoDB
          Properties:
            Stream: !GetAtt TaskTable.StreamArn
            StartingPosition: TRIM_HORIZON

  NotificationTopic:
    Type: AWS::SNS::Topic
    Properties:
      Subscription:
        - Protocol: email
          Endpoint: xxx@gmail.com
Enter fullscreen mode Exit fullscreen mode

Serverless Framework: Flexibility First

Strengths

  • Multi-Cloud Support: Works with AWS, Azure, GCP, and more.
  • Plugin Ecosystem: Highly customizable through plugins.
  • Best Use Case: Suitable for diverse, non-AWS-exclusive projects.

Limitations

  • Lacks built-in AWS-specific configurations like SAM’s Policies shortcuts.
  • May require extra setup for advanced AWS integrations.

Template

org: neclogoiclabs
app: sls-task-app
service: sls-test

package:
  individually: true
  exclude:
    - ./**

plugins:
  - serverless-plugin-common-excludes
  - serverless-plugin-include-dependencies
  - serverless-iam-roles-per-function

provider:
  name: aws
  runtime: nodejs18.x
  region: us-east-1

functions:
  apiHandler:
    handler: src/api/index.apiHandler
    package: 
      include:
        - src/api/**
    events:
      - http:
          path: task
          method: any
    iamRoleStatements:
      - Effect: Allow
        Action:
          - dynamodb:GetItem
          - dynamodb:PutItem
        Resource: 
          !GetAtt TaskTable.Arn

  streamHandler:
    handler: src/stream/index.streamHandler
    package: 
      include:
        - src/stream/**    
    environment:
      TOPIC_ARN: !Ref TaskNotificationTopic
    events:
      - stream:
          type: dynamodb
          arn:
            Fn::GetAtt: [TaskTable, StreamArn]
    iamRoleStatements:
      - Effect: Allow
        Action:
          - sns:Publish
        Resource:
          - !Ref TaskNotificationTopic
resources:
  Resources:
    TaskTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: TaskTable
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        BillingMode: PAY_PER_REQUEST
        StreamSpecification:
          StreamViewType: NEW_AND_OLD_IMAGES

    TaskNotificationTopic:
        Type: AWS::SNS::Topic
        Properties:
          TopicName: TaskNotificationTopic
          Subscription:
            - Protocol: email
              Endpoint: xxx@gmail.com
Enter fullscreen mode Exit fullscreen mode

Conclusion

Both frameworks have unique strengths. Consider the following when choosing:

  • Choose AWS SAM for AWS-specific projects requiring deep service integration.
  • Choose Serverless Framework for multi-cloud scenarios or projects needing high customization.

References

  1. AWS SAM Official Documentation
  2. Serverless Framework Documentation

Top comments (0)