Deploying an AWS Lambda using AWS SAM

When we want to create a new serverless function using the Amazon Web Services ecosystem, specifically the AWS Lambda service, we need to be concerned with code management: packaging by compressing files, using a Docker image, etc., in addition to dealing with event mapping, IAM execution function, etc.

With this in mind, the AWS Serverless Application Model (AWS SAM) was created, an open-source framework that aims to create some abstractions under AWS CloudFormation, which is AWS's infrastructure as code tool.

According to the documentation, AWS SAM is defined as:

AWS Serverless Application Model (AWS SAM) is an open-source framework for building serverless applications using infrastructure as code (IaC). With AWS SAM’s shorthand syntax, developers declare AWS CloudFormationresources and specialized serverless resources that are transformed to infrastructure during deployment.

In this way, AWS SAM provides us with a new resource, called: AWS Serverless Function. This resource aims to help us manage these micro-actions related to AWS Lambda, so it manages our dependencies and source code, IAM execution functions, and maps possible event triggers that we can add to the Lambda function.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: My Example Function

Parameters:
    Environment:
        Type: String

Globals:
    Function:
        Timeout: 30

Resources:
    ExampleFunction:
        Type: AWS::Serverless::Function
        Properties:
            FunctionName: 'example-function'
            CodeUri: ./
            Architectures: ['arm64']
            Handler: ./src/index.handler
            Runtime: nodejs18.x
            MemorySize: 128
            Description: 'Example Function'
            Environment:
                Variables:
                    NODE_ENV: !Ref Environment
                    REGION: !Ref AWS::Region
            Tags:
                Environment: !Ref Environment

Done! In this way, you already have a ready-to-deploy Lambda function model using AWS SAM.

1
Subscribe to my newsletter

Read articles from Thalles Lossurdo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Thalles Lossurdo
Thalles Lossurdo