|
| 1 | +# SQS Express.js example |
| 2 | + |
| 3 | +This project demonstrates the integration of Amazon Simple Queue Service (SQS) with an Express.js application. It showcases how to effectively manage and process messages from an SQS queue within an Express.js application environment. |
| 4 | + |
| 5 | +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. This simplifies the application code and allows developers to focus on processing the event payload rather than managing the queue polling logic. |
| 6 | + |
| 7 | +The application can be deployed in an AWS account using the [Serverless Application Model](https://github.com/awslabs/serverless-application-model). The `template.yaml` file in the root folder contains the application definition. |
| 8 | + |
| 9 | +The top level folder is a typical AWS SAM project. The `app` directory is an express.js application with a [Dockerfile](app/Dockerfile). |
| 10 | + |
| 11 | +```dockerfile |
| 12 | +FROM public.ecr.aws/docker/library/node:20-slim |
| 13 | +COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.0 /lambda-adapter /opt/extensions/lambda-adapter |
| 14 | +ENV PORT=8000 AWS_LWA_READINESS_CHECK_PROTOCOL=tcp |
| 15 | +WORKDIR "/var/task" |
| 16 | +ADD src/package.json /var/task/package.json |
| 17 | +ADD src/package-lock.json /var/task/package-lock.json |
| 18 | +RUN npm install --omit=dev |
| 19 | +ADD src/ /var/task |
| 20 | +CMD ["node", "index.js"] |
| 21 | +``` |
| 22 | + |
| 23 | +Line 2 copies lambda adapter binary into /opt/extenions. This is the only change to run the express.js application on Lambda. |
| 24 | + |
| 25 | +```dockerfile |
| 26 | +COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.0 /lambda-adapter /opt/extensions/lambda-adapter |
| 27 | +``` |
| 28 | + |
| 29 | +## Pre-requisites |
| 30 | + |
| 31 | +The following tools should be installed and configured. |
| 32 | + |
| 33 | +* [AWS CLI](https://aws.amazon.com/cli/) |
| 34 | +* [SAM CLI](https://github.com/awslabs/aws-sam-cli) |
| 35 | +* [Node](https://nodejs.org/en/) |
| 36 | +* [Docker](https://www.docker.com/products/docker-desktop) |
| 37 | + |
| 38 | +## Build |
| 39 | + |
| 40 | +Navigate to the sample's folder and use the SAM CLI to build a container image |
| 41 | + |
| 42 | +```shell |
| 43 | +sam build |
| 44 | +``` |
| 45 | +## Deploy |
| 46 | + |
| 47 | +This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory. |
| 48 | + |
| 49 | +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 |
| 50 | + |
| 51 | +```shell |
| 52 | +sam deploy --guided |
| 53 | +``` |
| 54 | + |
| 55 | +Please take note of the container image name. |
| 56 | +Once the deployment is completed, the SAM CLI will print out the stack's outputs, including the new sqs queue URL. |
| 57 | + |
| 58 | +```shell |
| 59 | +... |
| 60 | +--------------------------------------------------------------------------------------------------------------------------------------- |
| 61 | +Outputs |
| 62 | +--------------------------------------------------------------------------------------------------------------------------------------- |
| 63 | +Key SqsQueueUrl |
| 64 | +Description SQS URL the express Lambda Function will receive messages from |
| 65 | +Value https://sqs.us-west-2.amazonaws.com/xxxxxxxx/xxxxxxxx |
| 66 | +--------------------------------------------------------------------------------------------------------------------------------------- |
| 67 | + |
| 68 | +``` |
| 69 | + |
| 70 | +## Test |
| 71 | + |
| 72 | +Use the following command to send a message to the sqs queue. |
| 73 | + |
| 74 | +```shell |
| 75 | +aws sqs send-message --queue-url <replace with your sqs queue url> --message-body "Hello from CLI" |
| 76 | +``` |
| 77 | + |
| 78 | +Run the following command to see the Lambda function's CloudWatch logs. |
| 79 | + |
| 80 | +```shell |
| 81 | +sam logs --tail --stack-name <replace with your stack name> |
| 82 | +``` |
| 83 | + |
| 84 | +## Local Test |
| 85 | + |
| 86 | +You can also use SAM CLI for local testing. |
| 87 | + |
| 88 | +```shell |
| 89 | +sam local invoke SqsExpressFunction -e events/sqs.json |
| 90 | +``` |
| 91 | + |
| 92 | +Here is a sample output from this command. |
| 93 | + |
| 94 | +```shell |
| 95 | +Invoking Container created from sqsexpressfunction:v1 |
| 96 | +Building image................. |
| 97 | +Using local image: sqsexpressfunction:rapid-x86_64. |
| 98 | + |
| 99 | +START RequestId: ceaaf9bb-8d8c-42a5-828c-a5d4c8a506f1 Version: $LATEST |
| 100 | +Example app listening at http://localhost:8000 |
| 101 | +Received event: {"Records":[{"messageId":"19dd0b57-b21e-4ac1-bd88-01bbb068cb78","receiptHandle":"MessageReceiptHandle","body":"Hello from SQS!","attributes":{"ApproximateReceiveCount":"1","SentTimestamp":"1523232000000","SenderId":"123456789012","ApproximateFirstReceiveTimestamp":"1523232000001"},"messageAttributes":{},"md5OfBody":"7b270e59b47ff90a553787216d55d91d","eventSource":"aws:sqs","eventSourceARN":"arn:aws:sqs:us-east-1:123456789012:MyQueue","awsRegion":"us-east-1"}]} |
| 102 | +processing message: 19dd0b57-b21e-4ac1-bd88-01bbb068cb78 with body: Hello from SQS! |
| 103 | +END RequestId: ceaaf9bb-8d8c-42a5-828c-a5d4c8a506f1 |
| 104 | +REPORT RequestId: ceaaf9bb-8d8c-42a5-828c-a5d4c8a506f1 Init Duration: 0.10 ms Duration: 117.12 ms Billed Duration: 118 ms Memory Size: 1024 MB Max Memory Used: 1024 MB |
| 105 | +"success" |
| 106 | +``` |
0 commit comments