This project implements a serverless text message marketing campaign micro-service on AWS. It uses AWS services like Lambda, Cloud Formation, API Gateway, SQS, and Step Functions for orchestration.
serverless-text-message-service/
│
├── env/
├── handlers/
├── helpers/
├── node_modules/
├── tests/
├── package.json
└── template.yaml
git clone https://github.com/ashisho0/serverless-text-message-service.git
cd serverless-text-message-service
npm i
-
AWS Account: Ensure you have an AWS account set up.
-
IAM Role: Create an IAM role with permissions for Lambda, SQS, Step Functions, and API Gateway.
- Create a zip file
zip -r serverless-text-message-service.zip path/to/your/folder
- Create a bucket in s3 named "serverless-text-message-service" and upload the file to s3 bucket.
- Navigate to AWS CloudFormation
- Create a New Stack
- Specify Template (template.yaml from the cloned repo)
- Stack Name and Parameters
- Configure Stack Options
- Review
- Create SQS Queue:
- Go to AWS Management Console.
- Navigate to SQS service.
- Create a new queue named
TextMessageQueue
.
- Create Lambda Functions:
- Open AWS Lambda Console.
- Create two Lambda functions:
queueMessageFunction
:- Handler:
handlers/queueMessage.handler
- Runtime: Node.js 14.x
- Environment Variable:
SQS_QUEUE_URL
pointing to SQS queue URL.
- Handler:
sendMessageFunction
:- Handler:
handlers/sendMessage.handler
- Runtime: Node.js 14.x
- Environment Variable:
SQS_QUEUE_URL
pointing to SQS queue URL.
- Handler:
- Create Step Function:
- Open AWS Step Functions Console.
- Create a new state machine named
TextMessageOrchestration
. - Use the provided state machine definition JSON or use AWS CloudFormation to define it.
- Create API Gateway:
- Open AWS API Gateway Console.
- Create a new API named
TextMessageAPI
. - Define a POST method
/queue-message
:- Integration Type: Lambda Function (
queueMessageFunction
). - Use AWS Proxy integration to pass the entire request to Lambda.
- Integration Type: Lambda Function (
- Test the API:
- Use tools like Postman or cURL to send POST requests to API Gateway endpoint
/queue-message
. - Provide JSON payload with
to
(array of phone numbers) andmessage
(text message content). - Verify messages are queued in SQS and processed by Lambda functions.
- Use tools like Postman or cURL to send POST requests to API Gateway endpoint
-
CloudWatch Metrics: Enable CloudWatch metrics for your Step Functions state machine. AWS Step Functions automatically publishes metrics to CloudWatch, including execution counts, duration, and errors.
-
CloudWatch Alarms: Set up CloudWatch alarms based on these metrics to monitor the health of your state machine executions. For example, you can create alarms to alert you when the state machine encounters a high rate of errors or when executions take longer than expected.
- The tests folder consists of all the test cases that would be applicable for both
sendMessage
andqueueMessage
functions