Skip to content

Commit

Permalink
feature: added kinesis data stream eventsource (CodeGenieApp#531)
Browse files Browse the repository at this point in the history
* Added Kinesis DataStream EventSource

* test: added for kinesis dataStream events

* docs: added documentation for kinesis data stream events

Co-authored-by: brett-vendia <72168202+brett-vendia@users.noreply.github.com>
  • Loading branch information
agniswarm and brett-vendia authored Jun 30, 2022
1 parent 36db4b1 commit f7f3f6c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ serverlessExpress({
'AWS_DYNAMODB': '/dynamodb',
'AWS_SQS': '/sqs'
'AWS_EVENTBRIDGE': '/eventbridge',
'AWS_KINESIS_DATA_STREAM': '/kinesis',
}
})
```
Expand All @@ -276,6 +277,7 @@ ensure the `Host` header matches:
- DynamoDB: `dynamodb.amazonaws.com`
- SQS: `sqs.amazonaws.com`
- EventBridge: `events.amazonaws.com`
- KinesisDataStream: `kinesis.amazonaws.com`

### logSettings

Expand Down
20 changes: 20 additions & 0 deletions __tests__/unit.kinesis-data-stream.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 kinesisDataStreamEventSource = eventSources.getEventSource({
eventSourceName: 'AWS_KINESIS_DATA_STREAM'
})

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

function getReq () {
const event = testUtils.kinesisDataStreamEvent
const request = kinesisDataStreamEventSource.getRequest({ event })
return request
}
30 changes: 29 additions & 1 deletion __tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,28 @@ const eventbridgeScheduledEvent = {
resources: ['arn:aws:events:us-east-2:123456789012:rule/my-schedule']
}

// Sample event from https://docs.aws.amazon.com/lambda/latest/dg/with-kinesis-example.html
const kinesisDataStreamEvent = {
Records: [
{
kinesis: {
kinesisSchemaVersion: '1.0',
partitionKey: '1',
sequenceNumber: '49590338271490256608559692538361571095921575989136588898',
data: 'SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==',
approximateArrivalTimestamp: 1545084650.987
},
eventSource: 'aws:kinesis',
eventVersion: '1.0',
eventID: 'shardId-000000000006:49590338271490256608559692538361571095921575989136588898',
eventName: 'aws:kinesis:record',
invokeIdentityArn: 'arn:aws:iam::123456789012:role/lambda-kinesis-role',
awsRegion: 'us-east-2',
eventSourceARN: 'arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream'
}
]
}

describe('getEventSourceNameBasedOnEvent', () => {
test('throws error on empty event', () => {
expect(() => getEventSourceNameBasedOnEvent({ event: {} })).toThrow(
Expand Down Expand Up @@ -221,6 +243,11 @@ describe('getEventSourceNameBasedOnEvent', () => {
expect(result).toEqual('AWS_SQS')
})

test('recognizes kinesis data stream event', () => {
const result = getEventSourceNameBasedOnEvent({ event: kinesisDataStreamEvent })
expect(result).toEqual('AWS_KINESIS_DATA_STREAM')
})

test('recognizes eventbridge event', () => {
const result = getEventSourceNameBasedOnEvent({ event: eventbridgeEvent })
expect(result).toEqual('AWS_EVENTBRIDGE')
Expand All @@ -238,5 +265,6 @@ module.exports = {
snsEvent,
sqsEvent,
eventbridgeEvent,
eventbridgeScheduledEvent
eventbridgeScheduledEvent,
kinesisDataStreamEvent
}
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" | "AWS_SQS";
type EventSources = "AWS_SNS" | "AWS_DYNAMODB" | "AWS_EVENTBRIDGE" | "AWS_SQS" | "AWS_KINESIS_DATA_STREAM";

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

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

return {
method,
headers,
body
}
}

module.exports = {
getRequest: getRequestValuesFromKinesis,
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 @@ -7,6 +7,7 @@ const awsSqsEventSource = require('./aws/sqs')
const awsDynamoDbEventSource = require('./aws/dynamodb')
const azureHttpFunctionV3EventSource = require('./azure/http-function-runtime-v3')
const awsEventBridgeEventSource = require('./aws/eventbridge')
const awsKinesisEventSource = require('./aws/kinesis')

function getEventSource ({ eventSourceName }) {
switch (eventSourceName) {
Expand All @@ -28,6 +29,8 @@ function getEventSource ({ eventSourceName }) {
return awsSqsEventSource
case 'AWS_EVENTBRIDGE':
return awsEventBridgeEventSource
case 'AWS_KINESIS_DATA_STREAM':
return awsKinesisEventSource
default:
throw new Error('Couldn\'t detect valid event source.')
}
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 @@ -82,6 +82,9 @@ function getEventSourceNameBasedOnEvent ({
if (eventSource === 'aws:sqs') {
return 'AWS_SQS'
}
if (eventSource === 'aws:kinesis') {
return 'AWS_KINESIS_DATA_STREAM'
}
return 'AWS_LAMBDA_EDGE'
}
if (event.requestContext) {
Expand Down

0 comments on commit f7f3f6c

Please sign in to comment.