Skip to content

How to deploy Expressjs application using AWS SAM?

Published: at 12:00 AM

Serverless applications are increasingly popular due to their scalability and cost-effectiveness, and Amazon Web Services (AWS) offers an ideal tool to streamline the process: the AWS Serverless Application Model (SAM).

In this guide, we’ll walk through deploying an Express.js application using AWS SAM, making it easy to build, deploy, and manage your application on AWS.

Whether you’re new to serverless deployments or experienced with AWS, this guide will help you understand how to take advantage of AWS SAM’s benefits for Express.js. Let’s dive in!

Table of contents

Open Table of contents

Prerequisites

Before you start, make sure you have:

  1. AWS CLI: Download and install the AWS CLI. This tool will allow you to access various aws services.
  2. AWS SAM CLI: Download and install the AWS SAM CLI if you haven’t already. This tool will allow you to build, test, and deploy your application.
  3. AWS Account: You’ll need access to an AWS account with permissions to create and manage AWS resources.
  4. Node.js: Ensure Node.js and npm are installed to run and test your Express.js application.
  5. Code Files: You can find the codefiles in the following GitHub repo

With these in place, let’s get started!

What is AWS SAM?

AWS SAM is an open-source framework that simplifies the building and deployment of serverless applications on AWS. It extends AWS CloudFormation and offers several additional features:

With SAM, you define your resources in a template.yaml file, specifying how AWS should provision and configure them.

Why Deploy Express.js on AWS Lambda?

Traditionally, Express.js applications are hosted on dedicated servers or containers. However, deploying Express.js on AWS Lambda offers several advantages:

By using AWS SAM, we make it easy to deploy Express.js as a Lambda-based API.

Key Components in SAM Deployment

The SAM template (template.yaml) is the heart of your serverless application. This templates decides what and how the resources are goint to get deployed in your aws account.

Components deployed using the YAML file

  1. API Gateway configuration
  2. Lambda function specifications
  3. Other AWS resources needed by your application

Key Considerations

  1. Cold Starts: Be aware of potential cold start times for Lambda functions.

  2. Statelessness: Design your application to be stateless as Lambda functions are ephemeral.

  3. Monitoring: Utilize AWS CloudWatch for monitoring and logging.

Project Structure

Template Breakdown

The SAM template (template.yaml) includes:

AWSTemplateFormatVersion: "2010-09-09" # Specifies the version of the AWS CloudFormation template format being used.

Transform: AWS::Serverless-2016-10-31 # This applies the AWS SAM (Serverless Application Model) transform for serverless functions and APIs.

Description: >
  sam-app
  Sample SAM Template for sam-app # Brief description of the template, used for identification and documentation.

# Globals block defines common properties that are applied to serverless resources (e.g., functions).
Globals:
  Function:
    Timeout: 3 # Sets a timeout of 3 seconds for all Lambda functions in this template.

Resources:
  MyCustomApi:
    Type: AWS::Serverless::Api # Defines an API Gateway for the serverless application.
    Properties:
      Name: sam-app # Name of the API Gateway.
      StageName: Prod # Defines the deployment stage name as 'Prod'.
      # Domain:
      #   DomainName: "" # Optional. Specify a custom domain for the API if desired.
      #   CertificateArn: "" # Optional. Specify the ARN of the SSL certificate if using a custom domain.
      #   EndpointConfiguration: REGIONAL # Sets the endpoint configuration as regional.

  GetUserFunction:
    Type: AWS::Serverless::Function # Defines a Lambda function in a serverless application.
    Properties:
      CodeUri: express/ # Directory path where the function code is stored.
      Handler: src/server.handler # Entry point for the Lambda function.
      Runtime: nodejs18.x # Specifies the runtime for the function, Node.js 18.x in this case.
      Architectures:
        - x86_64 # Architecture for the Lambda function, here set to x86_64.
      Events:
        HelloWorld:
          Type: Api # Sets up API Gateway as the trigger for the Lambda function.
          Properties:
            RestApiId: !Ref MyCustomApi # Links the function to the previously defined API Gateway.
            Path: /{proxy+} # Specifies a catch-all proxy path, allowing the API to handle any subpath.
            Method: ANY # Allows any HTTP method (e.g., GET, POST) for the API requests.

Setup AWS Access

Use the AWS CLI to configure your AWS Access Keys.

aws configure

Note: If you don’t watn to use access keys, you can use an EC2 instance, attach a role to it and then deploy your application using the ec2 instance.

Deploy Application

Step 1: Build the Application

Use the SAM CLI to build the application:

sam build

Step 2: Validate the SAM Template

Use the SAM CLI to build the application:

sam validate

Step 3: Deploy the Application

Use the SAM CLI to build the application:

sam deploy --guided

Conclusion

Deploying Express.js applications using AWS SAM offers a powerful, scalable, and cost-effective solution for modern web applications.

By embracing serverless architecture, developers can focus more on writing code and less on managing infrastructure.

Remember, while serverless deployment simplifies many aspects of application management, it also introduces new concepts and best practices. Continuously learning and adapting to serverless paradigms will help you make the most of this innovative approach to web application deployment.


Next Post
How to install prometheus node-exporter on aws amazon linux ec2 instance?