Boilerplate code for AWS Lambda Function - using NodeJS runtime.
- API Gateway → Exposes endpoints for CRUD operations.
- AWS Lambda - Hello World
- AWS Lambda → Handles business logic for order management.
- Amazon DocumentDB (MongoDB-compatible) → Stores order data.
- Amazon SQS → Handles message queue processing.
- Amazon ElastiCache (Redis) → Caches order details for fast retrieval.
- AWS SAM (Serverless Application Model) → Defines infrastructure as code.
- Deployment Pipeline using Github actions (manual trigger)
- Authentication
- Authorization
- GraphQL - for order details
read
- Step 1: Fork this repository
- Step 2: Configure valid AWS credentials in your cloned repository's "Settings" tab -> "Secrets and Variables" -> "Repository Secrets" Section
- Step 3: Go to Actions
- Step 4: Select "SAM Deploy"
- Step 5: Click "Run Workflow" dropdown on the right hand side
read
1️⃣ template.yaml (AWS SAM Template)
Defines the Lambda functions, API Gateway, SQS, and ElastiCache.
- Create
mongodbtest instance (preferably local), Configure your mongodb url atDB_URIin template.yaml - Create
redistest instance at REDIS_HOST
docker run --name local-redis -p 6379:6379 -d redis
This command will start a Redis server on localhost at port 6379.
- Create pipeline:
cd order-manager && sam pipeline init --bootstrap - Validate SAM template:
cd order-manager && sam validate - Test Function in the Cloud:
cd order-manager && sam sync --stack-name {stack-name} --watch
sam build
sam deploy --guided
The AWS SAM CLI deploys your application by doing the following:
- The AWS SAM CLI creates ̑an Amazon S3 bucket and uploads your .aws-sam directory.
- The AWS SAM CLI transforms your AWS SAM template into AWS CloudFormation and uploads it to the AWS CloudFormation service.
- AWS CloudFormation provisions your resources.
Once the stack is created/updated successfully, then you will find the api gateway endpoint url.
To get the endpoint url:
Use the sam list endpoints --output json command to get this information
To Invoke the function
- Using curl
curl https://ets1gv8lxi.execute-api.us-west-2.amazonaws.com/Prod/hello/
{"message": "hello world"}
- Using sam command
sam remote invoke HelloWorldFunction --stack-name sam-app
Use the AWS SAM CLI sam sync --watch command to sync local changes to the AWS Cloud.
In your command line, from the sam-app project directory, run the following:
sam sync --watch
The AWS SAM CLI prompts you to confirm that you are syncing a development stack. Since the
sam sync --watchcommand automatically deploys local changes to the AWS Cloud in real time, we recommend it for development environments only.
Use the AWS SAM CLI sam local command to test your application locally. To accomplish this, the AWS SAM CLI creates a local environment using Docker. This local environment emulates the cloud-based execution environment of your Lambda function.
- Create a local environment for your Lambda function and invoke it.
- Host your HTTP API endpoint locally and use it to invoke your Lambda function.
In your command line, from the sam-app project directory, run the following:
sam local invoke
// To call a specific function
sam local invoke CreateOrderFunction --event events/createOrder.json
// To pass local env variables - only for local testing
sam local invoke CreateOrderFunction --event events/createOrder.json --env-vars env.json
In your command line, from the sam-app project directory, run the following:
sam local start-api
In your command line, from the sam-app project directory, run the following:
sam delete
If you've accidentally deleted the S3 bucket used by AWS SAM, you’ll need to create a new bucket and remap it to AWS SAM.
You can manually create an S3 bucket or use the AWS CLI:
aws s3 mb s3://my-new-sam-bucket --region us-east-1🔹 Replace my-new-sam-bucket with a unique bucket name.
🔹 Replace us-east-1 with your AWS region.
Modify your SAM configuration file (samconfig.toml) to reference the new bucket.
🔹 Locate samconfig.toml in your project.
🔹 Update the s3_bucket value:
[default.deploy.parameters]
s3_bucket = "my-new-sam-bucket"
s3_prefix = "my-app"
region = "us-east-1"
capabilities = "CAPABILITY_IAM"
stack_name = "my-app-stack"Alternatively, specify the new bucket during deployment:
sam deploy --s3-bucket my-new-sam-bucketRun the following to rebuild and redeploy:
sam build
sam deploy --guidedThis ensures that SAM now uses the new S3 bucket.
If AWS SAM was using a deleted bucket, you might encounter an error like:
🚨 "The specified bucket does not exist"
To fix this:
- Check
.aws-samdirectory:rm -rf .aws-sam
- Clear SAM cache (if needed):
sam cache clear
- Then rebuild & deploy again.
✔️ Created a new S3 bucket
✔️ Updated AWS SAM to use the new bucket
✔️ Rebuilt & deployed the application








