Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Pigius committed Sep 24, 2022
1 parent b37b976 commit 15ba239
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 52 deletions.
2 changes: 1 addition & 1 deletion aws-node-typescript-sqs-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ authorAvatar: 'https://avatars3.githubusercontent.com/u/28927258?s=460&v=4'

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!
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

Expand Down
20 changes: 8 additions & 12 deletions aws-node-typescript-sqs-standard/handlers/receiver.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
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);
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
}
};
49 changes: 13 additions & 36 deletions aws-node-typescript-sqs-standard/handlers/sender.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
import {
APIGatewayProxyHandler,
APIGatewayProxyEvent,
APIGatewayProxyResult,
} from "aws-lambda";
import { APIGatewayProxyHandler, 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;
export const handler: APIGatewayProxyHandler =
async (): Promise<APIGatewayProxyResult> => {
let statusCode: number = 200;
const message = "Hello world";

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 {
const queueUrl: string = process.env.QUEUE_URL;
await sqs
.sendMessage({
QueueUrl: queueUrl,
MessageBody: event.body,
MessageBody: message,
MessageAttributes: {
AttributeNameHere: {
StringValue: "Attribute Value Here",
Expand All @@ -38,17 +22,10 @@ export const handler: APIGatewayProxyHandler = async (
})
.promise();

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

return {
statusCode,
body: JSON.stringify({
message,
}),
return {
statusCode,
body: JSON.stringify({
message,
}),
};
};
};
5 changes: 2 additions & 3 deletions aws-node-typescript-sqs-standard/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ plugins:
provider:
name: aws
runtime: nodejs14.x
logRetentionInDays: 30

constructs:
mySimpleQueue:
queue:
type: queue
worker:
handler: handlers/receiver.handler
Expand All @@ -21,7 +20,7 @@ functions:
sender:
handler: handlers/sender.handler
environment:
QUEUE_URL: ${construct:mySimpleQueue.queueUrl}
QUEUE_URL: ${construct:queue.queueUrl}
events:
- httpApi:
method: post
Expand Down

0 comments on commit 15ba239

Please sign in to comment.