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.
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
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+
npm installsam buildOutput:
Built Artifacts : .aws-sam\build
Built Template : .aws-sam\build\template.yaml
sam deploy --guidedExample 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).
| 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}'| Attribute | Type | Key |
|---|---|---|
id |
String | HASH |
createdAt |
String | — |
| other fields | — | — |
Table names are environment-based:
ItemsTable-dev
ItemsTable-uat
ItemsTable-prod
You can test the function locally before deploying to AWS.
sam local start-apiThen access:
http://127.0.0.1:3000/items
To invoke a single function manually:
sam local invoke CrudFunction --event events/event.jsonLogs 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 --tailThe template supports multiple environments via a parameter called Environment.
Parameters:
Environment:
Type: String
Default: dev
AllowedValues:
- dev
- uat
- prod- Lambda:
SamCrudFunction-${Environment} - Table:
ItemsTable-${Environment} - Stack:
triplek-lambda-node-${Environment} - S3 Bucket:
triplek-sam-artifacts-${Environment}
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\""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\""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\""sam deploy --config-file samconfig-dev.toml
sam deploy --config-file samconfig-uat.toml
sam deploy --config-file samconfig-prod.tomlTo delete a deployed stack and its resources:
sam delete --stack-name triplek-lambda-node-dev- 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.