Skip to content

Commit 6e3590e

Browse files
tdt17Christian Gohlke
andauthored
added records to the SQS action for further processing (spring-media#43)
* added records to the SQS action for further usage * add sns and sqs example and linked to readme Co-authored-by: Christian Gohlke <[email protected]>
1 parent 6aaaa75 commit 6e3590e

File tree

6 files changed

+69
-10
lines changed

6 files changed

+69
-10
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ A small library for [AWS Lambda](https://aws.amazon.com/lambda/details) providin
1313
* Simplifies writing lambda handlers (in nodejs > 8)
1414
* Lambda Proxy Resource support for AWS API Gateway
1515
* Enable CORS for requests
16-
* No external dependencies
16+
* No external dependencies - well, almost, only types of [aws-lambda](https://www.npmjs.com/package/@types/aws-lambda) :-)
1717
* Currently there are four `processors` (callers for Lambda) implemented:
1818
* API Gateway ANY method (called proxyIntegration)
1919
* SNS
20-
* SQS
20+
* SQS
2121
* S3
2222
* Compatibility with Typescript >= 3.5
2323

@@ -245,13 +245,13 @@ export const handler = router.handler({
245245
// match complete SQS ARN:
246246
source: 'arn:aws:sqs:us-west-2:594035263019:aticle-import',
247247
// Attention: the messages Array is JSON-stringified
248-
action: (messages, context) => messages.forEach(message => console.log(JSON.parse(message)))
248+
action: (messages, context, records) => messages.forEach(message => console.log(JSON.parse(message)))
249249
},
250250
{
251251
// a regex to match the source SQS ARN:
252252
source: /.*notification/,
253253
// Attention: the messages array is JSON-stringified
254-
action: (messages, context) => service.doNotify(messages)
254+
action: (messages, context, records) => service.doNotify(messages)
255255
}
256256
]
257257
}
@@ -263,6 +263,8 @@ The `action` method gets all body elements from the router as an array.
263263
264264
If more than one route matches, only the **first** is used!
265265
266+
The *records* parameter contains the complete array of records, which handled by aws-lambda-router. An exampe can be found [here](lib/event-examples/sqs.json). This gives you the possibility to read metadata from the event. For example, you can parse the [message attributes of the SQS](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-attributes.html) and use them for further processing.
267+
266268
## S3 to Lambda Integrations
267269
268270
@@ -376,6 +378,8 @@ Increase version in **package.json** (using [semantic version syntax](https://se
376378
Thats all.
377379
378380
## Release History
381+
* 0.8.3
382+
* added records to the SQS action for further processing
379383
* 0.8.2
380384
* added support for Open API parameter definitions e.g.: /section/{id}
381385
* 0.8.1

lib/event-examples/sns.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"Records": [
3+
{
4+
"EventSource": "aws:sns",
5+
"EventVersion": "1.0",
6+
"EventSubscriptionArn": "arn:aws:sns:eu-west-1:123782373565:production-updates:2fdd994c-f2b7-4c2f-a2f9-da83b590e0fc",
7+
"Sns": {
8+
"Type": "Notification",
9+
"MessageId": "0629603b-448e-5366-88b4-309d651495c5",
10+
"TopicArn": "arn:aws:sns:eu-west-1:123782373565:production-updates",
11+
"Subject": null,
12+
"Message": "{\"id\":\"159526803\",\"model\":\"news\",\"status\":\"draft\"}",
13+
"Timestamp": "2016-11-16T08:56:58.227Z",
14+
"SignatureVersion": "1",
15+
"Signature": "dtXM9BlAJJhYkVObnKgHbqcB60pmNdEAE9f4bEOohizfPhg==",
16+
"SigningCertUrl": "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-b95095beb82e8f6a046b3aafc7f4149a.pem",
17+
"UnsubscribeUrl": "https://sns.eu-west-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-west-1:933767373565:production-escenic-updates:2fdd994c-f2b7-4c2f-a2f9-da83b590e0fc",
18+
"MessageAttributes": {
19+
"stringAttribute": {
20+
"Type": "String",
21+
"Value": "stringvalue"
22+
},
23+
"binaryAttribute": {
24+
"Type": "Binary",
25+
"Value": "Ym9udmFsdWU="
26+
},
27+
"arrayAttribute": {
28+
"Type": "String",
29+
"Value": "[\"value1\", \"value2\"]"
30+
}
31+
}
32+
}
33+
}
34+
]
35+
}

lib/event-examples/sqs.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"Records": [
3+
{
4+
"messageId": "c80e8021-a70a-42c7-a470-796e1186f753",
5+
"receiptHandle": "AQEBJQ+/u6NsnT5t8Q/VbVxgdUl4TMKZ5FqhksRdIQvLBhwNvADoBxYSOVeCBXdnS9P+erlTtwEALHsnBXynkfPLH3BOUqmgzP25U8kl8eHzq6RAlzrSOfTO8ox9dcp6GLmW33YjO3zkq5VRYyQlJgLCiAZUpY2D4UQcE5D1Vm8RoKfbE+xtVaOctYeINjaQJ1u3mWx9T7tork3uAlOe1uyFjCWU5aPX/1OHhWCGi2EPPZj6vchNqDOJC/Y2k1gkivqCjz1CZl6FlZ7UVPOx3AMoszPuOYZ+Nuqpx2uCE2MHTtMHD8PVjlsWirt56oUr6JPp9aRGo6bitPIOmi4dX0FmuMKD6u/JnuZCp+AXtJVTmSHS8IXt/twsKU7A+fiMK01NtD5msNgVPoe9JbFtlGwvTQ==",
6+
"body": "{\"foo\":\"bar\"}",
7+
"attributes": {
8+
"ApproximateReceiveCount": "3",
9+
"SentTimestamp": "1529104986221",
10+
"SenderId": "594035263019",
11+
"ApproximateFirstReceiveTimestamp": "1529104986230"
12+
},
13+
"messageAttributes": {},
14+
"md5OfBody": "9bb58f26192e4ba00f01e2e7b136bbd8",
15+
"eventSource": "aws:sqs",
16+
"eventSourceARN": "arn:aws:sqs:eu-central-1:594456463019:article-import",
17+
"awsRegion": "eu-central-1"
18+
}
19+
]
20+
}

lib/sqs.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type SqsEvent = SQSEvent
55

66
export interface SqsRoute {
77
source: string | RegExp
8-
action: (messages: SQSRecord['body'][], context: Context) => Promise<any> | any
8+
action: (messages: SQSRecord['body'][], context: Context, records: SQSRecord[]) => Promise<any> | any
99
}
1010

1111
export interface SqsConfig {
@@ -30,12 +30,12 @@ export const process: ProcessMethod<SqsConfig, SqsEvent, Context, any> = (sqsCon
3030
for (const routeConfig of sqsConfig.routes) {
3131
if (routeConfig.source instanceof RegExp) {
3232
if (routeConfig.source.test(recordSourceArn)) {
33-
const result = routeConfig.action(records.map(record => record.body), context)
33+
const result = routeConfig.action(records.map(record => record.body), context, records)
3434
return result || {}
3535
}
3636
} else {
3737
if (routeConfig.source === recordSourceArn) {
38-
const result = routeConfig.action(records.map(record => record.body), context)
38+
const result = routeConfig.action(records.map(record => record.body), context, records)
3939
return result || {}
4040
}
4141
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aws-lambda-router",
3-
"version": "0.8.2",
3+
"version": "0.8.3",
44
"description": "AWS lambda router",
55
"main": "index.js",
66
"types": "index.d.ts",

test/sqs.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { process as sqs, SqsEvent } from '../lib/sqs'
33
describe('sqs.processor', () => {
44
const context = {} as any
55

6-
it('context should be passed through', () => {
6+
it('context and records should be passed through', () => {
77
const actionSpy = jasmine.createSpy('action')
88

99
const context = { bla: 'blup' } as any
@@ -12,7 +12,7 @@ describe('sqs.processor', () => {
1212

1313
sqs(sqsCfg, event, context)
1414

15-
expect(actionSpy).toHaveBeenCalledWith([event.Records[0].body], context)
15+
expect(actionSpy).toHaveBeenCalledWith([event.Records[0].body], context, event.Records)
1616
})
1717

1818
it('should ignore event if it is no SQS event', () => {

0 commit comments

Comments
 (0)