Skip to content

Commit

Permalink
feat: added sqs as event source (#483)
Browse files Browse the repository at this point in the history
Co-authored-by: brett-vendia <72168202+brett-vendia@users.noreply.github.com>
  • Loading branch information
H4ad and brett-vendia committed Apr 19, 2022
1 parent fb3a934 commit 3f86b23
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 6 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ serverlessExpress({
#### eventSourceRoutes

A single function can be configured to handle additional kinds of AWS events:
- SNS
- DynamoDB Streams
- SNS
- DynamoDB Streams
- SQS
- EventBridge Events (formerlly CloudWatch Events)

Assuming the following function configuration in `serverless.yml`:
Expand All @@ -191,6 +192,8 @@ functions:
- stream:
type: dynamodb
arn: arn:aws:dynamodb:us-east-1:012345678990:table/my-table/stream/2021-07-15T15:05:51.683
- sqs:
arn: arn:aws:sqs:us-east-1:012345678990:myQueue
- eventBridge:
pattern:
source:
Expand All @@ -205,6 +208,7 @@ serverlessExpress({
eventSourceRoutes: {
'AWS_SNS': '/sns',
'AWS_DYNAMODB': '/dynamodb',
'AWS_SQS': '/sqs'
'AWS_EVENTBRIDGE': '/eventbridge',
}
})
Expand All @@ -226,9 +230,11 @@ Events will `POST` to the routes configured.
Also, to ensure the events propagated from an internal event and not externally, it is **highly recommended** to
ensure the `Host` header matches:

- SNS: `sns.amazonaws.com`
- DynamoDB: `dynamodb.amazonaws.com`
- EventBridge: `events.amazonaws.com`

- SNS: `sns.amazonaws.com`
- DynamoDB: `dynamodb.amazonaws.com`
- SQS: `sqs.amazonaws.com`
- EventBridge: `events.amazonaws.com`

### logSettings

Expand Down
20 changes: 20 additions & 0 deletions __tests__/unit.sqs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const eventSources = require('../src/event-sources')
const testUtils = require('./utils')

const sqsEventSource = eventSources.getEventSource({
eventSourceName: 'AWS_SQS'
})

test('request is correct', () => {
const req = getReq()
expect(typeof req).toEqual('object')
expect(req.headers).toEqual({ host: 'sqs.amazonaws.com' })
expect(req.body).toEqual(testUtils.sqsEvent)
expect(req.method).toEqual('POST')
})

function getReq () {
const event = testUtils.sqsEvent
const request = sqsEventSource.getRequest({ event })
return request
}
24 changes: 24 additions & 0 deletions __tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,26 @@ const snsEvent = {
]
}

// Sample event from https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html
const sqsEvent = {
Records: [
{
messageId: '059f36b4-87a3-44ab-83d2-661975830a7d',
receiptHandle: 'AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...',
body: 'Test message.',
attributes: {
ApproximateReceiveCount: '1',
SentTimestamp: '1545082649183',
SenderId: 'AIDAIENQZJOLO23YVJ4VO',
ApproximateFirstReceiveTimestamp: '1545082649185'
},
messageAttributes: {},
md5OfBody: 'e4e68fb7bd0e697a0ae8f1bb342846b3',
eventSource: 'aws:sqs',
eventSourceARN: 'arn:aws:sqs:us-east-2:123456789012:my-queue',
awsRegion: 'us-east-2'
}
]
// Sample event from https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html
const eventbridgeEvent = {
version: '0',
Expand Down Expand Up @@ -194,6 +214,9 @@ describe('getEventSourceNameBasedOnEvent', () => {
expect(result).toEqual('AWS_SNS')
})

test('recognizes sqs event', () => {
const result = getEventSourceNameBasedOnEvent({ event: sqsEvent })
expect(result).toEqual('AWS_SQS')
test('recognizes eventbridge event', () => {
const result = getEventSourceNameBasedOnEvent({ event: eventbridgeEvent })
expect(result).toEqual('AWS_EVENTBRIDGE')
Expand All @@ -209,6 +232,7 @@ module.exports = {
samHttpApiEvent,
dynamoDbEvent,
snsEvent,
sqsEvent
eventbridgeEvent,
eventbridgeScheduledEvent
}
2 changes: 1 addition & 1 deletion src/configure.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Handler } from "aws-lambda";
import Logger from "./logger";
import Framework from "./frameworks";

type EventSources = "AWS_SNS" | "AWS_DYNAMODB" | "AWS_EVENTBRIDGE";
type EventSources = "AWS_SNS" | "AWS_DYNAMODB" | "AWS_EVENTBRIDGE" | "AWS_SQS";

interface EventSource {
getRequest?: any; // TODO:
Expand Down
18 changes: 18 additions & 0 deletions src/event-sources/aws/sqs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { emptyResponseMapper } = require('../utils')

const getRequestValuesFromSns = ({ event }) => {
const method = 'POST'
const headers = { host: 'sqs.amazonaws.com' }
const body = event

return {
method,
headers,
body
}
}

module.exports = {
getRequest: getRequestValuesFromSns,
getResponse: emptyResponseMapper
}
3 changes: 3 additions & 0 deletions src/event-sources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const awsApiGatewayV2EventSource = require('./aws/api-gateway-v2')
const awsAlbEventSource = require('./aws/alb')
const awsLambdaEdgeEventSource = require('./aws/lambda-edge')
const awsSnsEventSource = require('./aws/sns')
const awsSqsEventSource = require('./aws/sqs')
const awsDynamoDbEventSource = require('./aws/dynamodb')
const awsEventBridgeEventSource = require('./aws/eventbridge')

Expand All @@ -20,6 +21,8 @@ function getEventSource ({ eventSourceName }) {
return awsDynamoDbEventSource
case 'AWS_SNS':
return awsSnsEventSource
case 'AWS_SQS':
return awsSqsEventSource
case 'AWS_EVENTBRIDGE':
return awsEventBridgeEventSource
default:
Expand Down
3 changes: 3 additions & 0 deletions src/event-sources/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ function getEventSourceNameBasedOnEvent ({
if (eventSource === 'aws:dynamodb') {
return 'AWS_DYNAMODB'
}
if (eventSource === 'aws:sqs') {
return 'AWS_SQS'
}
return 'AWS_LAMBDA_EDGE'
}
if (event.requestContext) {
Expand Down

0 comments on commit 3f86b23

Please sign in to comment.