Skip to content

Commit b37b976

Browse files
committed
[Feature] Improve Typescript SQS standard example
1 parent e052c4b commit b37b976

File tree

9 files changed

+4418
-3830
lines changed

9 files changed

+4418
-3830
lines changed

aws-node-typescript-sqs-standard/README.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,67 @@ authorLink: 'https://github.com/jmpfrazao'
1010
authorName: 'Miguel Frazao'
1111
authorAvatar: 'https://avatars3.githubusercontent.com/u/28927258?s=460&v=4'
1212
-->
13+
1314
# Simple SQS Standard Example
1415

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

18+
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!
19+
1720
## Use Cases
21+
1822
- Decouple message producers from message consumers.
1923
- This is one way to architect for scale and reliability.
2024

2125
## Setup
22-
- sls deploy
26+
27+
`npm install` to install all needed packages.
28+
29+
## Deployment
30+
31+
In order to deploy the service run:
32+
33+
```bash
34+
sls deploy
35+
```
36+
37+
for deploying with a specific `profile` (located in `~/.aws/credentials`) you can simply use the command:
38+
39+
```bash
40+
AWS_PROFILE=YOUR_PROFILE_NAME sls deploy
41+
```
42+
43+
for deploying to the specific stage, let's say `staging` do:
44+
45+
```bash
46+
sls deploy --stage staging
47+
```
48+
49+
The expected result should be similar to:
50+
51+
```bash
52+
✔ Service deployed to stack aws-node-typescript-sqs-standard-dev (345s)
53+
54+
endpoint: POST - https://XXXXXXXXX.execute-api.REGION.amazonaws.com/sender
55+
functions:
56+
sender: aws-node-typescript-sqs-standard-STAGE-sender (8 MB)
57+
receiver: aws-node-typescript-sqs-standard-dev-receiver (8 MB)
58+
mySimpleQueueWorker: aws-node-typescript-sqs-standard-YOUR-STAGE-mySimpleQueueWorker (8 MB)
59+
updateData: https://sqs.REGION.amazonaws.com/XXXXXXXXXXX/aws-node-typescript-sqs-standard-dev-mySimpleQueue
60+
```
2361

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

28-
- send a HTTP POST request to the sender lambda with a JSON payload
64+
- Send a HTTP POST request to the sender lambda with a JSON payload (The response will be `{"message":"Message placed in the Queue!"}`)
65+
- To print out the logs of the receiver sqs handler on the terminal.
66+
`sls logs -f mySimpleQueueWorker -t`
67+
It will look like:
68+
```bash
69+
serverless logs -f mySimpleQueueWorker
70+
Running "serverless" from node_modules
71+
START
72+
2022-08-06 16:24:46.022 INFO Message Attributtes --> Attribute Value Here
73+
2022-08-06 16:24:46.023 INFO Message Body --> "test"
74+
END Duration: 2.83 ms (init: 155.47 ms) Memory Used: 56 MB
75+
```
76+
- Alternatively you can check cloudwatch logs.

aws-node-typescript-sqs-standard/handler.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SQSEvent, SQSHandler, SQSMessageAttributes } from "aws-lambda";
2+
3+
export const handler: SQSHandler = async (event: SQSEvent): Promise<void> => {
4+
try {
5+
for (const record of event.Records) {
6+
const messageAttributes: SQSMessageAttributes = record.messageAttributes;
7+
console.log(
8+
"Message Attributtes --> ",
9+
messageAttributes.AttributeNameHere.stringValue
10+
);
11+
console.log("Message Body --> ", record.body);
12+
// Do something
13+
}
14+
} catch (error) {
15+
console.log(error);
16+
}
17+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {
2+
APIGatewayProxyHandler,
3+
APIGatewayProxyEvent,
4+
APIGatewayProxyResult,
5+
} from "aws-lambda";
6+
import { SQS } from "aws-sdk";
7+
8+
const sqs = new SQS();
9+
10+
export const handler: APIGatewayProxyHandler = async (
11+
event: APIGatewayProxyEvent
12+
): Promise<APIGatewayProxyResult> => {
13+
let statusCode: number = 200;
14+
let message: string;
15+
16+
if (!event.body) {
17+
return {
18+
statusCode: 400,
19+
body: JSON.stringify({
20+
message: "No body was found",
21+
}),
22+
};
23+
}
24+
25+
const queueUrl: string = process.env.QUEUE_URL;
26+
console.log("event.body"), event.body;
27+
try {
28+
await sqs
29+
.sendMessage({
30+
QueueUrl: queueUrl,
31+
MessageBody: event.body,
32+
MessageAttributes: {
33+
AttributeNameHere: {
34+
StringValue: "Attribute Value Here",
35+
DataType: "String",
36+
},
37+
},
38+
})
39+
.promise();
40+
41+
message = "Message placed in the Queue!";
42+
} catch (error) {
43+
console.log(error);
44+
message = error;
45+
statusCode = 500;
46+
}
47+
48+
return {
49+
statusCode,
50+
body: JSON.stringify({
51+
message,
52+
}),
53+
};
54+
};

0 commit comments

Comments
 (0)