Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Improve Typescript SQS standard example #710

Open
wants to merge 2 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions aws-node-typescript-sqs-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,67 @@ authorLink: 'https://github.com/jmpfrazao'
authorName: 'Miguel Frazao'
authorAvatar: 'https://avatars3.githubusercontent.com/u/28927258?s=460&v=4'
-->

# Simple SQS Standard Example

This example demonstrates how to setup a SQS Standard and send messages through the message body and attributes.

The queue was created by using the [queue construct from the Lift plugin](https://github.com/getlift/lift/blob/master/docs/queue.md).

## Use Cases

- Decouple message producers from message consumers.
- This is one way to architect for scale and reliability.

## Setup
- sls deploy

`npm install` to install all needed packages.

## Deployment

In order to deploy the service run:

```bash
sls deploy
```

for deploying with a specific `profile` (located in `~/.aws/credentials`) you can simply use the command:

```bash
AWS_PROFILE=YOUR_PROFILE_NAME sls deploy
```

for deploying to the specific stage, let's say `staging` do:

```bash
sls deploy --stage staging
```

The expected result should be similar to:

```bash
✔ Service deployed to stack aws-node-typescript-sqs-standard-dev (345s)

endpoint: POST - https://XXXXXXXXX.execute-api.REGION.amazonaws.com/sender
functions:
sender: aws-node-typescript-sqs-standard-STAGE-sender (8 MB)
receiver: aws-node-typescript-sqs-standard-dev-receiver (8 MB)
mySimpleQueueWorker: aws-node-typescript-sqs-standard-YOUR-STAGE-mySimpleQueueWorker (8 MB)
updateData: https://sqs.REGION.amazonaws.com/XXXXXXXXXXX/aws-node-typescript-sqs-standard-dev-mySimpleQueue
```

## Usage
- To print out the logs of the receiver sqs handler on the terminal
`sls logs -f receiver -t`

- send a HTTP POST request to the sender lambda with a JSON payload
- Send a HTTP POST request to the sender lambda with a JSON payload (The response will be `{"message":"Message placed in the Queue!"}`)
- To print out the logs of the receiver sqs handler on the terminal.
`sls logs -f mySimpleQueueWorker -t`
It will look like:
```bash
serverless logs -f mySimpleQueueWorker
Running "serverless" from node_modules
START
2022-08-06 16:24:46.022 INFO Message Attributtes --> Attribute Value Here
2022-08-06 16:24:46.023 INFO Message Body --> "test"
END Duration: 2.83 ms (init: 155.47 ms) Memory Used: 56 MB
```
- Alternatively you can check cloudwatch logs.
2 changes: 0 additions & 2 deletions aws-node-typescript-sqs-standard/handler.ts

This file was deleted.

13 changes: 13 additions & 0 deletions aws-node-typescript-sqs-standard/handlers/receiver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { SQSEvent, SQSHandler, SQSMessageAttributes } from "aws-lambda";

export const handler: SQSHandler = async (event: SQSEvent): Promise<void> => {
for (const record of event.Records) {
const messageAttributes: SQSMessageAttributes = record.messageAttributes;
console.log(
"Message Attributtes --> ",
messageAttributes.AttributeNameHere.stringValue
);
console.log("Message Body --> ", record.body);
// Do something
}
Pigius marked this conversation as resolved.
Show resolved Hide resolved
};
31 changes: 31 additions & 0 deletions aws-node-typescript-sqs-standard/handlers/sender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { APIGatewayProxyHandler, APIGatewayProxyResult } from "aws-lambda";
import { SQS } from "aws-sdk";

const sqs = new SQS();

export const handler: APIGatewayProxyHandler =
async (): Promise<APIGatewayProxyResult> => {
let statusCode: number = 200;
const message = "Hello world";

const queueUrl: string = process.env.QUEUE_URL;
await sqs
.sendMessage({
QueueUrl: queueUrl,
MessageBody: message,
MessageAttributes: {
AttributeNameHere: {
StringValue: "Attribute Value Here",
DataType: "String",
},
},
})
.promise();

return {
statusCode,
body: JSON.stringify({
message,
}),
};
};
Loading