Created
October 28, 2022 09:42
-
-
Save leegilmorecode/b6d3b89bae347a2d9bab4919163d644a to your computer and use it in GitHub Desktop.
Example of a bad lambda function handler not using Hexagonal Architectures
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
import * as AWS from 'aws-sdk'; // <-- service specific | |
// <-- lambda specific | |
import { | |
APIGatewayEvent, | |
APIGatewayProxyHandler, | |
APIGatewayProxyResult, | |
} from 'aws-lambda'; | |
import { OrderCreate } from '../../../../types'; | |
import { v4 as uuid } from 'uuid'; | |
const dynamoDb = new AWS.DynamoDB.DocumentClient(); // <-- service specific | |
const eventBridge = new AWS.EventBridge(); // <-- service specific | |
export const handler: APIGatewayProxyHandler = async ( | |
// <-- lambda function specific | |
event: APIGatewayEvent | |
): Promise<APIGatewayProxyResult> => { | |
// <-- lambda function specific | |
try { | |
const correlationId = uuid(); | |
const method = 'create-order.handler'; | |
const prefix = `${correlationId} - ${method}`; | |
console.log(`${prefix} - started`); | |
if (!event?.body) throw new Error('no body on the event'); // <-- lambda specific | |
const { id, quantity } = JSON.parse(event.body); // <-- lambda specific | |
if (quantity === 0) throw new Error('quantity of 0 not valid'); // <-- domain specific | |
const ordersTable = process.env.TABLE_NAME as string; // <-- service specific | |
// <-- domain specific | |
const order: OrderCreate = { | |
id, | |
quantity, | |
created: new Date().toISOString(), | |
updated: new Date().toISOString(), | |
}; // <-- domain specific | |
const params: AWS.DynamoDB.DocumentClient.PutItemInput = { | |
TableName: ordersTable, | |
Item: order, | |
}; // <-- service specific | |
console.log(`${prefix} - creating order: ${JSON.stringify(order)}`); | |
await dynamoDb.put(params).promise(); // <-- service specific | |
// <-- service specific | |
const orderCreatedEvent: AWS.EventBridge.PutEventsRequestEntry = { | |
Detail: JSON.stringify(order), | |
DetailType: 'OrderCreated', | |
EventBusName: 'OrderEventBus', | |
Source: 'com.service.order', | |
}; | |
// <-- service specific | |
const orderEvent: AWS.EventBridge.PutEventsRequest = { | |
Entries: [orderCreatedEvent], | |
}; // <-- service specific | |
// <-- service specific | |
const result: AWS.EventBridge.PutEventsResponse = await eventBridge | |
.putEvents(orderEvent) | |
.promise(); | |
// <-- service specific | |
console.log(`${prefix} - creating order event: ${JSON.stringify(result)}`); | |
// <-- lambda function specific | |
return { | |
statusCode: 201, | |
body: JSON.stringify(order), | |
}; | |
// <-- lambda function specific | |
} catch (error) { | |
console.log(error); | |
throw error; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment