-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(@whook/aws-lambda): refactor lambda triggers
Refactoring the wrappers to simplify them except for tricky parts like encoding/decoding of events. BREAKING CHANGE: The consumers do not manage batchs anymore and simply pass the records to the lambda implementation in order to avoid having to write custom wrappers for each event type. The user are now free to handle the batchs the way they want. fix #95 concerns #96
- Loading branch information
Showing
17 changed files
with
14,441 additions
and
4,655 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/whook-aws-lambda/src/commands/testKafkaConsumerLambda.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { loadLambda } from '../libs/utils'; | ||
import { extra, autoService } from 'knifecycle'; | ||
import { readArgs } from '@whook/cli'; | ||
import type { WhookCommandArgs, WhookCommandDefinition } from '@whook/cli'; | ||
import type { LogService } from 'common-services'; | ||
import type { MSKEvent } from 'aws-lambda'; | ||
|
||
const DEFAULT_EVENT: MSKEvent = { | ||
eventSource: 'aws:kafka', | ||
eventSourceArn: | ||
'arn:aws:kafka:eu-west-3:765225263528:cluster/production/abbacaca-abba-caca-abba-cacaabbacaca-2', | ||
records: { | ||
'ingestion-bench-1': [ | ||
{ | ||
key: 'none', | ||
topic: 'tropic', | ||
partition: 1, | ||
offset: 0, | ||
timestamp: 1608321344592, | ||
timestampType: 'CREATE_TIME', | ||
value: | ||
'WyJERy1TUi0wMDAxIiwieCIsIjIwMjAtMTAtMTVUMDg6MjE6MTAuMzA4WiIsIi0yNzIuMCJd', | ||
}, | ||
{ | ||
key: 'none', | ||
topic: 'tropic', | ||
partition: 1, | ||
offset: 1, | ||
timestamp: 1608321344801, | ||
timestampType: 'CREATE_TIME', | ||
value: | ||
'WyJERy1TUi0wMDAxIiwieCIsIjIwMjAtMTAtMTVUMDg6MjE6MTEuMjE1WiIsIi0xOTIuMCJd', | ||
}, | ||
], | ||
}, | ||
}; | ||
|
||
export const definition: WhookCommandDefinition = { | ||
description: 'A command for testing AWS lambda Kafka consumers', | ||
example: `whook KafkaConsumer --name handleKafkaConsumerLambda`, | ||
arguments: { | ||
type: 'object', | ||
additionalProperties: false, | ||
required: ['name'], | ||
properties: { | ||
name: { | ||
description: 'Name of the lamda to run', | ||
type: 'string', | ||
}, | ||
type: { | ||
description: 'Type of lambda to test', | ||
type: 'string', | ||
enum: ['main', 'index'], | ||
default: 'index', | ||
}, | ||
event: { | ||
description: 'The Kafka batch event', | ||
type: 'string', | ||
default: JSON.stringify(DEFAULT_EVENT), | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default extra( | ||
definition, | ||
autoService(initTestKafkaConsumerLambdaCommand), | ||
); | ||
|
||
async function initTestKafkaConsumerLambdaCommand({ | ||
NODE_ENV, | ||
PROJECT_DIR, | ||
log, | ||
args, | ||
}: { | ||
NODE_ENV: string; | ||
PROJECT_DIR: string; | ||
log: LogService; | ||
args: WhookCommandArgs; | ||
}) { | ||
return async () => { | ||
const { name, type, event } = readArgs(definition.arguments, args) as { | ||
name: string; | ||
type: string; | ||
event: string; | ||
}; | ||
const handler = await loadLambda( | ||
{ PROJECT_DIR, log }, | ||
NODE_ENV, | ||
name, | ||
type, | ||
); | ||
const parsedEvent = JSON.parse(event); | ||
const result = await new Promise((resolve, reject) => { | ||
const handlerPromise = handler( | ||
parsedEvent, | ||
{ | ||
succeed: (...args: unknown[]) => { | ||
handlerPromise.then(resolve.bind(null, ...args)); | ||
}, | ||
fail: reject, | ||
}, | ||
(err: Error, ...args: unknown[]) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
handlerPromise.then(resolve.bind(null, ...args)); | ||
}, | ||
).catch(reject); | ||
}); | ||
|
||
log('info', 'SUCCESS:', result); | ||
}; | ||
} |
108 changes: 108 additions & 0 deletions
108
packages/whook-aws-lambda/src/commands/testLogSubscriberLambda.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { loadLambda } from '../libs/utils'; | ||
import { extra, autoService } from 'knifecycle'; | ||
import { readArgs } from '@whook/cli'; | ||
import { encodePayload } from '../wrappers/awsLogSubscriberLambda'; | ||
import type { WhookCommandArgs, WhookCommandDefinition } from '@whook/cli'; | ||
import type { LogService } from 'common-services'; | ||
import type { | ||
CloudWatchLogsDecodedData, | ||
CloudWatchLogsEvent, | ||
} from 'aws-lambda'; | ||
|
||
// Event example from: | ||
// https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchlogs.html | ||
const DEFAULT_EVENT: CloudWatchLogsDecodedData = { | ||
messageType: 'DATA_MESSAGE', | ||
owner: '123456789012', | ||
logGroup: '/aws/lambda/echo-nodejs', | ||
logStream: '2019/03/13/[$LATEST]94fa867e5374431291a7fc14e2f56ae7', | ||
subscriptionFilters: ['LambdaStream_cloudwatchlogs-node'], | ||
logEvents: [ | ||
{ | ||
id: '34622316099697884706540976068822859012661220141643892546', | ||
timestamp: 1552518348220, | ||
message: | ||
'REPORT RequestId: 6234bffe-149a-b642-81ff-2e8e376d8aff\tDuration: 46.84 ms\tBilled Duration: 47 ms \tMemory Size: 192 MB\tMax Memory Used: 72 MB\t\n', | ||
}, | ||
], | ||
}; | ||
|
||
export const definition: WhookCommandDefinition = { | ||
description: 'A command for testing AWS consumer lambda', | ||
example: `whook testS3Lambda --name handleS3Lambda`, | ||
arguments: { | ||
type: 'object', | ||
additionalProperties: false, | ||
required: ['name'], | ||
properties: { | ||
name: { | ||
description: 'Name of the lamda to run', | ||
type: 'string', | ||
}, | ||
type: { | ||
description: 'Type of lambda to test', | ||
type: 'string', | ||
enum: ['main', 'index'], | ||
default: 'index', | ||
}, | ||
event: { | ||
description: 'The S3 actions batch event', | ||
type: 'string', | ||
default: JSON.stringify(DEFAULT_EVENT), | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default extra(definition, autoService(initTestS3LambdaCommand)); | ||
|
||
async function initTestS3LambdaCommand({ | ||
NODE_ENV, | ||
PROJECT_DIR, | ||
log, | ||
args, | ||
}: { | ||
NODE_ENV: string; | ||
PROJECT_DIR: string; | ||
log: LogService; | ||
args: WhookCommandArgs; | ||
}) { | ||
return async () => { | ||
const { name, type, event } = readArgs(definition.arguments, args) as { | ||
name: string; | ||
type: string; | ||
event: string; | ||
}; | ||
const handler = await loadLambda( | ||
{ PROJECT_DIR, log }, | ||
NODE_ENV, | ||
name, | ||
type, | ||
); | ||
const parsedEvent: CloudWatchLogsEvent = { | ||
awslogs: { | ||
data: await encodePayload(JSON.parse(event)), | ||
}, | ||
}; | ||
const result = await new Promise((resolve, reject) => { | ||
const handlerPromise = handler( | ||
parsedEvent, | ||
{ | ||
succeed: (...args: unknown[]) => { | ||
handlerPromise.then(resolve.bind(null, ...args)); | ||
}, | ||
fail: reject, | ||
}, | ||
(err: Error, ...args: unknown[]) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
handlerPromise.then(resolve.bind(null, ...args)); | ||
}, | ||
).catch(reject); | ||
}); | ||
|
||
log('info', 'SUCCESS:', result); | ||
}; | ||
} |
Oops, something went wrong.