Skip to content

Instantly share code, notes, and snippets.

View leegilmorecode's full-sized avatar
:atom:
Serverless Hero

Lee Gilmore leegilmorecode

:atom:
Serverless Hero
View GitHub Profile
@leegilmorecode
leegilmorecode / create-customer-account.ts
Created November 6, 2022 15:15
Example of our create customer account use case
import {
CreateCustomerAccountProps,
CustomerAccountProps,
} from '@models/types';
import { eventName, eventSource } from '@events/customer-account-created';
import { CustomerAccount } from '@domain/customer-account';
import { createCustomerAccount } from '@repositories/create-customer-account-repository';
import { publishEvent } from '@repositories/publish-event-recipient';
@leegilmorecode
leegilmorecode / customer-account.ts
Created November 6, 2022 15:08
Example of our customer account entity
import {
CreateCustomerAccountProps,
NewCustomerAccountProps,
PaymentStatus,
SubscriptionType,
UnmarshalledCustomerAccount,
} from '@models/types';
import { Entity } from '@entity/entity';
import { PaymentInvalidError } from '@errors/payment-invalid-error';
@leegilmorecode
leegilmorecode / create-order.ts
Created October 28, 2022 09:42
Example of a bad lambda function handler not using Hexagonal Architectures
import * as AWS from 'aws-sdk'; // <-- service specific
// <-- lambda specific
import {
APIGatewayEvent,
APIGatewayProxyHandler,
APIGatewayProxyResult,
} from 'aws-lambda';
import { OrderCreate } from '../../../../types';
@leegilmorecode
leegilmorecode / create-order.ts
Last active October 10, 2022 02:40
create-order.ts sample
// we get the order details from the event
const order: OrderCreated = JSON.parse(body);
// we generate an idempotency key
const idempotencyKey: string = uuidV5(
JSON.stringify(order),
namespaces.orders
);
// create the detail of the event
@leegilmorecode
leegilmorecode / validate.ts
Created October 9, 2022 09:08
validate.ts
import Ajv from 'ajv';
import addFormats from 'ajv-formats';
const ajvOptions = {
allErrors: true,
};
// as this is not a monorepo and an example repo only we are using a typescript path alias
// whereas this would typically be published to npm and reused in all domain service
export function validate(
@leegilmorecode
leegilmorecode / order.ts
Created October 9, 2022 09:07
order.ts
// the order regexes can be used across our domain objects,
// events, api validation and more..
export const orderRegexes = {
orderId: '^[A-Z]{3}-\\d{1,4}$',
addressLine: '^[a-zA-Z0-9 _.-]*$',
productId: '^[A-Z]{4}-\\d{1,4}$',
};
import { orderRegexes } from '@regexes/orders/order';
export const schema = {
openapi: '3.0.0',
info: { version: '2.0.0', title: 'OrderCreated' },
paths: {},
components: {
schemas: {
AWSEvent: {
type: 'object',
export const schema = {
openapi: '3.0.0',
info: { version: '1.0.0', title: 'OrderCreated' },
paths: {},
components: {
schemas: {
AWSEvent: {
type: 'object',
required: [
'detail-type',
@leegilmorecode
leegilmorecode / orders-service.ts
Created July 31, 2022 06:05
Direct integration example between API Gateway and Amazon EventBridge
const eventBus: IEventBus = EventBus.fromEventBusName(
this,
"OnlineOrders",
"OnlineOrdersEventBus"
);
// event bridge options for the api gateway integration
const eventBridgeOptions: apigw.IntegrationOptions = {
credentialsRole: apigwRole,
requestParameters: {
@leegilmorecode
leegilmorecode / order-service-stack.ts
Last active July 31, 2022 06:41
Example of invoking a Step Function Express Workflow from Amazon API Gateway
// create the state machine defintion for cancelling an order
const cancelOrderStateMachineDefinition: sfn.TaskStateBase =
new tasks.LambdaInvoke(this, "CancelOrder", {
lambdaFunction: cancelOrderHandler,
resultPath: "$",
timeout: Duration.seconds(30),
comment: "Cancel order task",
retryOnServiceExceptions: true,
}).addCatch(
new tasks.SqsSendMessage(this, "SendSQSFailure", {