Skip to content

Commit 6e3a7f2

Browse files
authored
feat: add aws self-managed kafka event source (#686)
1 parent 04fe1f5 commit 6e3a7f2

File tree

7 files changed

+86
-2
lines changed

7 files changed

+86
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ serverlessExpress({
283283
'AWS_KINESIS_DATA_STREAM': '/kinesis',
284284
'AWS_S3': '/s3',
285285
'AWS_STEP_FUNCTIONS': '/step-functions',
286+
'AWS_SELF_MANAGED_KAFKA': '/self-managed-kafka',
286287
}
287288
})
288289
```

__tests__/unit.self-managed-kafka.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const eventSources = require('../src/event-sources')
2+
const testUtils = require('./utils')
3+
4+
const selfManagedKafkaEventSource = eventSources.getEventSource({
5+
eventSourceName: 'AWS_SELF_MANAGED_KAFKA'
6+
})
7+
8+
test('request is correct', () => {
9+
const req = getReq()
10+
expect(typeof req).toEqual('object')
11+
expect(req.headers).toEqual({ host: 'self-managed-kafka' })
12+
expect(req.body).toEqual(testUtils.selfManagedKafkaEvent)
13+
expect(req.method).toEqual('POST')
14+
})
15+
16+
function getReq () {
17+
const event = testUtils.selfManagedKafkaEvent
18+
const request = selfManagedKafkaEventSource.getRequest({ event })
19+
return request
20+
}

__tests__/utils.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,42 @@ const kinesisDataStreamEvent = {
231231
]
232232
}
233233

234+
// Sample event from https://docs.aws.amazon.com/lambda/latest/dg/with-kafka.html
235+
const selfManagedKafkaEvent = {
236+
eventSource: 'SelfManagedKafka',
237+
bootstrapServers: 'b-2.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092,b-1.demo-cluster-1.a1bcde.c1.kafka.us-east-1.amazonaws.com:9092',
238+
records: {
239+
'mytopic-0': [
240+
{
241+
topic: 'mytopic',
242+
partition: 0,
243+
offset: 15,
244+
timestamp: 1545084650987,
245+
timestampType: 'CREATE_TIME',
246+
key: 'abcDEFghiJKLmnoPQRstuVWXyz1234==',
247+
value: 'SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==',
248+
headers: [
249+
{
250+
headerKey: [
251+
104,
252+
101,
253+
97,
254+
100,
255+
101,
256+
114,
257+
86,
258+
97,
259+
108,
260+
117,
261+
101
262+
]
263+
}
264+
]
265+
}
266+
]
267+
}
268+
}
269+
234270
describe('getEventSourceNameBasedOnEvent', () => {
235271
test('throws error on empty event', () => {
236272
expect(() => getEventSourceNameBasedOnEvent({ event: {} })).toThrow(
@@ -263,6 +299,11 @@ describe('getEventSourceNameBasedOnEvent', () => {
263299
expect(result).toEqual('AWS_KINESIS_DATA_STREAM')
264300
})
265301

302+
test('recognises self managed kafka event', () => {
303+
const result = getEventSourceNameBasedOnEvent({ event: selfManagedKafkaEvent })
304+
expect(result).toEqual('AWS_SELF_MANAGED_KAFKA')
305+
})
306+
266307
test('recognizes eventbridge event', () => {
267308
const result = getEventSourceNameBasedOnEvent({ event: eventbridgeEvent })
268309
expect(result).toEqual('AWS_EVENTBRIDGE')
@@ -287,5 +328,6 @@ module.exports = {
287328
eventbridgeEvent,
288329
eventbridgeScheduledEvent,
289330
eventbridgeCustomerEvent,
290-
kinesisDataStreamEvent
331+
kinesisDataStreamEvent,
332+
selfManagedKafkaEvent
291333
}

src/configure.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Handler } from 'aws-lambda';
33
import { Logger } from './logger';
44
import Framework from './frameworks';
55

6-
type EventSources = 'AWS_SNS' | 'AWS_DYNAMODB' | 'AWS_EVENTBRIDGE' | 'AWS_SQS' | 'AWS_KINESIS_DATA_STREAM' | 'AWS_S3' | 'AWS_STEP_FUNCTIONS';
6+
type EventSources = 'AWS_SNS' | 'AWS_DYNAMODB' | 'AWS_EVENTBRIDGE' | 'AWS_SQS' | 'AWS_KINESIS_DATA_STREAM' | 'AWS_S3' | 'AWS_STEP_FUNCTIONS' | 'AWS_SELF_MANAGED_KAFKA';
77

88
interface EventSource {
99
getRequest?: any; // TODO:
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { emptyResponseMapper } = require('../utils')
2+
3+
const getRequestValuesFromSelfManagedKafka = ({ event }) => {
4+
const method = 'POST'
5+
const headers = { host: 'self-managed-kafka' }
6+
const body = event
7+
8+
return {
9+
method,
10+
headers,
11+
body
12+
}
13+
}
14+
module.exports = {
15+
getRequest: getRequestValuesFromSelfManagedKafka,
16+
getResponse: emptyResponseMapper
17+
}

src/event-sources/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const awsEventBridgeEventSource = require('./aws/eventbridge')
1111
const awsKinesisEventSource = require('./aws/kinesis')
1212
const awsS3 = require('./aws/s3')
1313
const awsStepFunctionsEventSource = require('./aws/step-functions')
14+
const awsSelfManagedKafkaEventSource = require('./aws/self-managed-kafka')
1415

1516
function getEventSource ({ eventSourceName }) {
1617
switch (eventSourceName) {
@@ -40,6 +41,8 @@ function getEventSource ({ eventSourceName }) {
4041
return awsS3
4142
case 'AWS_STEP_FUNCTIONS':
4243
return awsStepFunctionsEventSource
44+
case 'AWS_SELF_MANAGED_KAFKA':
45+
return awsSelfManagedKafkaEventSource
4346
default:
4447
throw new Error('Couldn\'t detect valid event source.')
4548
}

src/event-sources/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function getEventSourceNameBasedOnEvent ({
7171
event
7272
}) {
7373
if (event.requestContext && event.requestContext.elb) return 'AWS_ALB'
74+
if (event.eventSource === 'SelfManagedKafka') return 'AWS_SELF_MANAGED_KAFKA'
7475
if (event.Records) {
7576
const eventSource = event.Records[0] ? event.Records[0].EventSource || event.Records[0].eventSource : undefined
7677
if (eventSource === 'aws:sns') {

0 commit comments

Comments
 (0)