File tree 4 files changed +24
-52
lines changed
aws-node-typescript-sqs-standard
4 files changed +24
-52
lines changed Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ authorAvatar: 'https://avatars3.githubusercontent.com/u/28927258?s=460&v=4'
15
15
16
16
This example demonstrates how to setup a SQS Standard and send messages through the message body and attributes.
17
17
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!
18
+ The queue was created by using the [ queue construct from the Lift plugin] ( https://github.com/getlift/lift/blob/master/docs/queue.md ) .
19
19
20
20
## Use Cases
21
21
Original file line number Diff line number Diff line change 1
1
import { SQSEvent , SQSHandler , SQSMessageAttributes } from "aws-lambda" ;
2
2
3
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 ) ;
4
+ for ( const record of event . Records ) {
5
+ const messageAttributes : SQSMessageAttributes = record . messageAttributes ;
6
+ console . log (
7
+ "Message Attributtes --> " ,
8
+ messageAttributes . AttributeNameHere . stringValue
9
+ ) ;
10
+ console . log ( "Message Body --> " , record . body ) ;
11
+ // Do something
16
12
}
17
13
} ;
Original file line number Diff line number Diff line change 1
- import {
2
- APIGatewayProxyHandler ,
3
- APIGatewayProxyEvent ,
4
- APIGatewayProxyResult ,
5
- } from "aws-lambda" ;
1
+ import { APIGatewayProxyHandler , APIGatewayProxyResult } from "aws-lambda" ;
6
2
import { SQS } from "aws-sdk" ;
7
3
8
4
const sqs = new SQS ( ) ;
9
5
10
- export const handler : APIGatewayProxyHandler = async (
11
- event : APIGatewayProxyEvent
12
- ) : Promise < APIGatewayProxyResult > => {
13
- let statusCode : number = 200 ;
14
- let message : string ;
6
+ export const handler : APIGatewayProxyHandler =
7
+ async ( ) : Promise < APIGatewayProxyResult > => {
8
+ let statusCode : number = 200 ;
9
+ const message = "Hello world" ;
15
10
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 {
11
+ const queueUrl : string = process . env . QUEUE_URL ;
28
12
await sqs
29
13
. sendMessage ( {
30
14
QueueUrl : queueUrl ,
31
- MessageBody : event . body ,
15
+ MessageBody : message ,
32
16
MessageAttributes : {
33
17
AttributeNameHere : {
34
18
StringValue : "Attribute Value Here" ,
@@ -38,17 +22,10 @@ export const handler: APIGatewayProxyHandler = async (
38
22
} )
39
23
. promise ( ) ;
40
24
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
- } ) ,
25
+ return {
26
+ statusCode,
27
+ body : JSON . stringify ( {
28
+ message,
29
+ } ) ,
30
+ } ;
53
31
} ;
54
- } ;
Original file line number Diff line number Diff line change @@ -9,10 +9,9 @@ plugins:
9
9
provider :
10
10
name : aws
11
11
runtime : nodejs14.x
12
- logRetentionInDays : 30
13
12
14
13
constructs :
15
- mySimpleQueue :
14
+ queue :
16
15
type : queue
17
16
worker :
18
17
handler : handlers/receiver.handler
@@ -21,7 +20,7 @@ functions:
21
20
sender :
22
21
handler : handlers/sender.handler
23
22
environment :
24
- QUEUE_URL : ${construct:mySimpleQueue .queueUrl}
23
+ QUEUE_URL : ${construct:queue .queueUrl}
25
24
events :
26
25
- httpApi :
27
26
method : post
You can’t perform that action at this time.
0 commit comments