Ömer Toraman
10/30/2021, 2:19 PMevent
, and EventBridge
.
import { EventBridgeClient, PutEventsCommand, PutEventsCommandInput } from '@aws-sdk/client-eventbridge'
import { StatusCodes } from 'http-status-codes'
import type { APIGatewayProxyHandler } from "aws-lambda"
const eventBridgeClient = new EventBridgeClient({ region: 'eu-central-1' })
export const handler: APIGatewayProxyHandler = async (evt, ctx, cb) => {
const requestBody = JSON.parse(evt.body)
const { orderID, price } = requestBody
//////////
// Check if the body has required properties
//////////
if (!orderID || !price) {
return {
statusCode: StatusCodes.BAD_REQUEST,
body: 'Invalid request'
}
}
//////////
// Put it into EventBridge
//////////
try {
const putEventsCommandInput: PutEventsCommandInput = {
Entries: [
{ EventBusName: 'OrderBus', Detail: JSON.stringify({ id: orderID, price }) }
]
}
const putEventsCommand = new PutEventsCommand(putEventsCommandInput)
const putEventsCommandOutput = await eventBridgeClient.send(putEventsCommand)
return {
statusCode: StatusCodes.CREATED,
body: JSON.stringify({ eventID: putEventsCommandOutput.Entries[0].EventId })
}
} catch (error) {
return {
statusCode: StatusCodes.ACCEPTED,
body: JSON.stringify(error)
}
}
}
thdxr
10/30/2021, 2:20 PMthdxr
10/30/2021, 2:20 PMÖmer Toraman
10/30/2021, 2:27 PMRoss Coundon
10/30/2021, 2:42 PMimport { DynamoDBStreamEvent, Context, Callback, DynamoDBRecord } from 'aws-lambda';
test('it should handle the stream event successfully', async () => {
/*******************
* SETUP
*******************/
const event: DynamoDBStreamEvent = {
Records: [
{
//...
}
],
};
/*******************
* ACT
*******************/
// Call the handler
await handler(event, {} as Context, {} as Callback);
/*******************
* VERIFY
*******************/
// Expect the build responses service to be called once with an array of the four new activities
expect(someMockedService.buildResponses).toBeCalledTimes(1);
...
});
thdxr
10/30/2021, 3:30 PMRoss Coundon
10/30/2021, 4:14 PMSeth Geoghegan
10/30/2021, 4:15 PMSeth Geoghegan
10/30/2021, 4:15 PMthdxr
10/30/2021, 4:16 PMSeth Geoghegan
10/30/2021, 4:17 PMSeth Geoghegan
10/30/2021, 4:17 PMSeth Geoghegan
10/30/2021, 4:17 PMthdxr
10/30/2021, 4:19 PMthdxr
10/30/2021, 4:20 PMSimon Reilly
10/30/2021, 6:20 PMthdxr
10/30/2021, 6:28 PMSimon Reilly
10/30/2021, 6:30 PMSimon Reilly
10/30/2021, 6:38 PM