Skip to content

Commit

Permalink
[Feature] Improve Typescript SQS standard example
Browse files Browse the repository at this point in the history
  • Loading branch information
Pigius committed Aug 6, 2022
1 parent e052c4b commit b37b976
Show file tree
Hide file tree
Showing 9 changed files with 4,418 additions and 3,830 deletions.
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 [queue construct from the Lift plugin](https://github.com/getlift/lift/blob/master/docs/queue.md), which works perfectly!

## 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.

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

export const handler: SQSHandler = async (event: SQSEvent): Promise<void> => {
try {
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
}
} catch (error) {
console.log(error);
}
};
54 changes: 54 additions & 0 deletions aws-node-typescript-sqs-standard/handlers/sender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
APIGatewayProxyHandler,
APIGatewayProxyEvent,
APIGatewayProxyResult,
} from "aws-lambda";
import { SQS } from "aws-sdk";

const sqs = new SQS();

export const handler: APIGatewayProxyHandler = async (
event: APIGatewayProxyEvent
): Promise<APIGatewayProxyResult> => {
let statusCode: number = 200;
let message: string;

if (!event.body) {
return {
statusCode: 400,
body: JSON.stringify({
message: "No body was found",
}),
};
}

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

message = "Message placed in the Queue!";
} catch (error) {
console.log(error);
message = error;
statusCode = 500;
}

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

0 comments on commit b37b976

Please sign in to comment.