Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

added records to the SQS action for further processing #43

Merged
merged 2 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ A small library for [AWS Lambda](https://aws.amazon.com/lambda/details) providin
* Simplifies writing lambda handlers (in nodejs > 8)
* Lambda Proxy Resource support for AWS API Gateway
* Enable CORS for requests
* No external dependencies
* No external dependencies - well, almost, only types of [aws-lambda](https://www.npmjs.com/package/@types/aws-lambda) :-)
* Currently there are four `processors` (callers for Lambda) implemented:
* API Gateway ANY method (called proxyIntegration)
* SNS
* SQS
* SQS
* S3
* Compatibility with Typescript >= 3.5

Expand Down Expand Up @@ -245,13 +245,13 @@ export const handler = router.handler({
// match complete SQS ARN:
source: 'arn:aws:sqs:us-west-2:594035263019:aticle-import',
// Attention: the messages Array is JSON-stringified
action: (messages, context) => messages.forEach(message => console.log(JSON.parse(message)))
action: (messages, context, records) => messages.forEach(message => console.log(JSON.parse(message)))
},
{
// a regex to match the source SQS ARN:
source: /.*notification/,
// Attention: the messages array is JSON-stringified
action: (messages, context) => service.doNotify(messages)
action: (messages, context, records) => service.doNotify(messages)
}
]
}
Expand All @@ -263,6 +263,8 @@ The `action` method gets all body elements from the router as an array.

If more than one route matches, only the **first** is used!

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.

## S3 to Lambda Integrations


Expand Down Expand Up @@ -376,6 +378,8 @@ Increase version in **package.json** (using [semantic version syntax](https://se
Thats all.

## Release History
* 0.8.3
* added records to the SQS action for further processing
* 0.8.2
* added support for Open API parameter definitions e.g.: /section/{id}
* 0.8.1
Expand Down
35 changes: 35 additions & 0 deletions lib/event-examples/sns.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"Records": [
{
"EventSource": "aws:sns",
"EventVersion": "1.0",
"EventSubscriptionArn": "arn:aws:sns:eu-west-1:123782373565:production-updates:2fdd994c-f2b7-4c2f-a2f9-da83b590e0fc",
"Sns": {
"Type": "Notification",
"MessageId": "0629603b-448e-5366-88b4-309d651495c5",
"TopicArn": "arn:aws:sns:eu-west-1:123782373565:production-updates",
"Subject": null,
"Message": "{\"id\":\"159526803\",\"model\":\"news\",\"status\":\"draft\"}",
"Timestamp": "2016-11-16T08:56:58.227Z",
"SignatureVersion": "1",
"Signature": "dtXM9BlAJJhYkVObnKgHbqcB60pmNdEAE9f4bEOohizfPhg==",
"SigningCertUrl": "https://sns.eu-west-1.amazonaws.com/SimpleNotificationService-b95095beb82e8f6a046b3aafc7f4149a.pem",
"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",
"MessageAttributes": {
"stringAttribute": {
"Type": "String",
"Value": "stringvalue"
},
"binaryAttribute": {
"Type": "Binary",
"Value": "Ym9udmFsdWU="
},
"arrayAttribute": {
"Type": "String",
"Value": "[\"value1\", \"value2\"]"
}
}
}
}
]
}
20 changes: 20 additions & 0 deletions lib/event-examples/sqs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Records": [
{
"messageId": "c80e8021-a70a-42c7-a470-796e1186f753",
"receiptHandle": "AQEBJQ+/u6NsnT5t8Q/VbVxgdUl4TMKZ5FqhksRdIQvLBhwNvADoBxYSOVeCBXdnS9P+erlTtwEALHsnBXynkfPLH3BOUqmgzP25U8kl8eHzq6RAlzrSOfTO8ox9dcp6GLmW33YjO3zkq5VRYyQlJgLCiAZUpY2D4UQcE5D1Vm8RoKfbE+xtVaOctYeINjaQJ1u3mWx9T7tork3uAlOe1uyFjCWU5aPX/1OHhWCGi2EPPZj6vchNqDOJC/Y2k1gkivqCjz1CZl6FlZ7UVPOx3AMoszPuOYZ+Nuqpx2uCE2MHTtMHD8PVjlsWirt56oUr6JPp9aRGo6bitPIOmi4dX0FmuMKD6u/JnuZCp+AXtJVTmSHS8IXt/twsKU7A+fiMK01NtD5msNgVPoe9JbFtlGwvTQ==",
"body": "{\"foo\":\"bar\"}",
"attributes": {
"ApproximateReceiveCount": "3",
"SentTimestamp": "1529104986221",
"SenderId": "594035263019",
"ApproximateFirstReceiveTimestamp": "1529104986230"
},
"messageAttributes": {},
"md5OfBody": "9bb58f26192e4ba00f01e2e7b136bbd8",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:eu-central-1:594456463019:article-import",
"awsRegion": "eu-central-1"
}
]
}
6 changes: 3 additions & 3 deletions lib/sqs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export type SqsEvent = SQSEvent

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

export interface SqsConfig {
Expand All @@ -30,12 +30,12 @@ export const process: ProcessMethod<SqsConfig, SqsEvent, Context, any> = (sqsCon
for (const routeConfig of sqsConfig.routes) {
if (routeConfig.source instanceof RegExp) {
if (routeConfig.source.test(recordSourceArn)) {
const result = routeConfig.action(records.map(record => record.body), context)
const result = routeConfig.action(records.map(record => record.body), context, records)
return result || {}
}
} else {
if (routeConfig.source === recordSourceArn) {
const result = routeConfig.action(records.map(record => record.body), context)
const result = routeConfig.action(records.map(record => record.body), context, records)
return result || {}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aws-lambda-router",
"version": "0.8.2",
"version": "0.8.3",
"description": "AWS lambda router",
"main": "index.js",
"types": "index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions test/sqs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { process as sqs, SqsEvent } from '../lib/sqs'
describe('sqs.processor', () => {
const context = {} as any

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

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

sqs(sqsCfg, event, context)

expect(actionSpy).toHaveBeenCalledWith([event.Records[0].body], context)
expect(actionSpy).toHaveBeenCalledWith([event.Records[0].body], context, event.Records)
})

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