Skip to content

TripleKay/node_serverless_lambda

Repository files navigation

🦾 AWS SAM Node.js CRUD Example

This project demonstrates a serverless CRUD API built with AWS SAM (Serverless Application Model) using:

  • AWS Lambda (Node.js 18)
  • Amazon API Gateway
  • Amazon DynamoDB

It supports multiple environments (dev, uat, prod) and can be deployed directly from your terminal using AWS SAM CLI.


📁 Project Structure


node_serverless_sample/
├── src/
│   └── app.js              # Lambda handler (CRUD logic)
├── template.yaml           # SAM template (defines AWS resources)
├── samconfig-dev.toml      # SAM deployment config (for dev)
├── package.json
└── README.md


⚙️ Prerequisites

Before running this project, ensure you have:

  • AWS Account (IAM user with appropriate permissions)
  • AWS CLI (configured with aws configure)
  • AWS SAM CLI (Install guide)
  • Node.js 18+

🚀 Setup and Deployment

1️⃣ Install dependencies

npm install

2️⃣ Build the project

sam build

Output:

Built Artifacts  : .aws-sam\build
Built Template   : .aws-sam\build\template.yaml

3️⃣ Deploy (guided for the first time)

sam deploy --guided

Example answers:

Stack Name: triplek-lambda-node-dev
AWS Region: ap-southeast-1
Confirm changes before deploy: y
Allow SAM CLI IAM role creation: y
Save arguments to configuration file: y

This creates samconfig.toml (you can duplicate it for other environments).


🌍 API Endpoints

Method Endpoint Description
POST /items Create a new item
GET /items Retrieve all items
GET /items/{id} Retrieve single item
PUT /items/{id} Update an existing item
DELETE /items/{id} Delete an item

Example request (POST /items):

curl -X POST https://<api-url>/Prod/items \
  -H "Content-Type: application/json" \
  -d '{"name":"Item A","price":100}'

💾 DynamoDB Table

Attribute Type Key
id String HASH
createdAt String
other fields

Table names are environment-based:

ItemsTable-dev
ItemsTable-uat
ItemsTable-prod

🧠 Local Testing

You can test the function locally before deploying to AWS.

sam local start-api

Then access:

http://127.0.0.1:3000/items

To invoke a single function manually:

sam local invoke CrudFunction --event events/event.json

🪵 Logs and Monitoring

Logs are automatically sent to Amazon CloudWatch Logs when deployed.

To view logs from your terminal:

sam logs -n CrudFunction --stack-name triplek-lambda-node-dev --tail

🧩 Environment Configuration

The template supports multiple environments via a parameter called Environment.

Example SAM Template Snippet:

Parameters:
  Environment:
    Type: String
    Default: dev
    AllowedValues:
      - dev
      - uat
      - prod

Environment-based naming:

  • Lambda: SamCrudFunction-${Environment}
  • Table: ItemsTable-${Environment}
  • Stack: triplek-lambda-node-${Environment}
  • S3 Bucket: triplek-sam-artifacts-${Environment}

🧰 Example SAM Config Files

samconfig-dev.toml

version = 0.1
[default.deploy.parameters]
stack_name = "triplek-lambda-node-dev"
region = "ap-southeast-1"
s3_bucket = "triplek-sam-artifacts-dev"
s3_prefix = "triplek-lambda-node-dev"
capabilities = "CAPABILITY_IAM"
disable_rollback = true
parameter_overrides = "Environment=\"dev\""

samconfig-uat.toml

version = 0.1
[default.deploy.parameters]
stack_name = "triplek-lambda-node-uat"
region = "ap-southeast-1"
s3_bucket = "triplek-sam-artifacts-uat"
s3_prefix = "triplek-lambda-node-uat"
capabilities = "CAPABILITY_IAM"
disable_rollback = true
parameter_overrides = "Environment=\"uat\""

samconfig-prod.toml

version = 0.1
[default.deploy.parameters]
stack_name = "triplek-lambda-node-prod"
region = "ap-southeast-1"
s3_bucket = "triplek-sam-artifacts-prod"
s3_prefix = "triplek-lambda-node-prod"
capabilities = "CAPABILITY_IAM"
disable_rollback = false
parameter_overrides = "Environment=\"prod\""

Deploy by environment:

sam deploy --config-file samconfig-dev.toml
sam deploy --config-file samconfig-uat.toml
sam deploy --config-file samconfig-prod.toml

🧹 Cleanup

To delete a deployed stack and its resources:

sam delete --stack-name triplek-lambda-node-dev

🧭 Notes

  • Every environment (dev/uat/prod) uses its own Lambda, API Gateway, DynamoDB table, and S3 bucket.
  • Avoid using the same Lambda function name across stacks.
  • CloudWatch Logs are enabled automatically when the Lambda runs.
  • You can safely redeploy anytime — SAM will update existing resources.

📚 References

About

serverless CRUD API built with AWS SAM

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published