Mastering Serverless with AWS SAM

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. AWS SAM (Serverless Application Model) is an open-source framework that makes building and deploying serverless applications easier. In this gui…


This content originally appeared on DEV Community and was authored by Márcio Coelho

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. AWS SAM (Serverless Application Model) is an open-source framework that makes building and deploying serverless applications easier. In this guide, we will walk through setting up an AWS Lambda function using Node.js and AWS SAM.

Step 1: Create a New AWS SAM Application

To begin, open a terminal and run:

sam init

Select the following options when prompted:

Template source: AWS Quick Start Templates

Runtime: nodejs22.x (or latest available)

Package type: Zip

Project name: aws-sam-node-lambda

Once completed, navigate to the project directory:

cd aws-sam-node-lambda

Step 2: Understanding the Project Structure

Your SAM project contains several files and folders:

aws-sam-node-lambda/
├── functions
│   ├──hello-world/
│      ├── app.js         # Lambda function code
│      ├── package.json   # Node.js dependencies
│      ├── tests/         # Unit tests
├── template.yaml       # AWS SAM template defining resources

The template.yaml file is crucial as it defines your AWS resources.

Step 3: Writing the Lambda Function

Modify hello-world/app.js to:

exports.lambdaHandler = async (event, context) => {
    return {
        statusCode: 200,
        body: JSON.stringify({
            message: "Hello from AWS Lambda with Node.js and SAM!"
        })
    };
};

Step 4: Build and Test Locally

Before deploying, build the function:

sam build

To invoke it locally:

sam local invoke HelloWorldFunction

Expected output:

{
    "statusCode": 200,
    "body": "{\"message\":\"Hello from AWS Lambda with Node.js and SAM!\"}"
}


This content originally appeared on DEV Community and was authored by Márcio Coelho


Print Share Comment Cite Upload Translate Updates
APA

Márcio Coelho | Sciencx (2025-03-31T13:55:14+00:00) Mastering Serverless with AWS SAM. Retrieved from https://www.scien.cx/2025/03/31/mastering-serverless-with-aws-sam/

MLA
" » Mastering Serverless with AWS SAM." Márcio Coelho | Sciencx - Monday March 31, 2025, https://www.scien.cx/2025/03/31/mastering-serverless-with-aws-sam/
HARVARD
Márcio Coelho | Sciencx Monday March 31, 2025 » Mastering Serverless with AWS SAM., viewed ,<https://www.scien.cx/2025/03/31/mastering-serverless-with-aws-sam/>
VANCOUVER
Márcio Coelho | Sciencx - » Mastering Serverless with AWS SAM. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/31/mastering-serverless-with-aws-sam/
CHICAGO
" » Mastering Serverless with AWS SAM." Márcio Coelho | Sciencx - Accessed . https://www.scien.cx/2025/03/31/mastering-serverless-with-aws-sam/
IEEE
" » Mastering Serverless with AWS SAM." Márcio Coelho | Sciencx [Online]. Available: https://www.scien.cx/2025/03/31/mastering-serverless-with-aws-sam/. [Accessed: ]
rf:citation
» Mastering Serverless with AWS SAM | Márcio Coelho | Sciencx | https://www.scien.cx/2025/03/31/mastering-serverless-with-aws-sam/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.