This project demonstrates the integration of Amazon Simple Queue Service (SQS) with an Express.js application on AWS Lambda. It showcases how to effectively manage and process messages from an SQS queue within an serverless Express.js application environment.
In this Express.js application integrated with Amazon SQS, there is no explicit code required to poll the SQS queue. The AWS Lambda handles the polling of the SQS queue and Lambda Web Adapter forwards the event payload to the Express.js application vith a HTTP POST request. This simplifies the application code and allows developers to focus on processing the event payload rather than managing the queue polling logic.
The application can be deployed in an AWS account using the Serverless Application Model. The template.yaml
file in the root folder contains the application definition.
The top level folder is a typical AWS SAM project. The app
directory is an express.js application with a Dockerfile.
FROM public.ecr.aws/docker/library/node:20-slim
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.4 /lambda-adapter /opt/extensions/lambda-adapter
ENV PORT=8000 AWS_LWA_READINESS_CHECK_PROTOCOL=tcp
WORKDIR "/var/task"
ADD src/package.json /var/task/package.json
ADD src/package-lock.json /var/task/package-lock.json
RUN npm install --omit=dev
ADD src/ /var/task
CMD ["node", "index.js"]
Line 2 copies lambda adapter binary into /opt/extenions. This is the only change to run the express.js application on Lambda.
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.4 /lambda-adapter /opt/extensions/lambda-adapter
The following tools should be installed and configured.
Navigate to the sample's folder and use the SAM CLI to build a container image
sam build
This command compiles the application and prepares a deployment package in the .aws-sam
sub-directory.
To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the instructions on the screen
sam deploy --guided
Please take note of the container image name. Once the deployment is completed, the SAM CLI will print out the stack's outputs, including the new sqs queue URL.
...
---------------------------------------------------------------------------------------------------------------------------------------
Outputs
---------------------------------------------------------------------------------------------------------------------------------------
Key SqsQueueUrl
Description SQS URL the express Lambda Function will receive messages from
Value https://sqs.us-west-2.amazonaws.com/xxxxxxxx/xxxxxxxx
---------------------------------------------------------------------------------------------------------------------------------------
Use the following command to send a message to the sqs queue.
aws sqs send-message --queue-url <replace with your sqs queue url> --message-body "Hello from CLI"
Run the following command to see the Lambda function's CloudWatch logs.
sam logs --tail --stack-name <replace with your stack name>
First, start the express.js server locally.
cd app/src
npm install
node index.js
Use curl
to send a POST request to the express.js server.
curl -X POST -H "Content-Type: application/json" -d @events/sqs.json http://localhost:8080/events
You can also use your favorate IDE debugger to debug your application step by step.