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 / 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", {
@leegilmorecode
leegilmorecode / create-table.ts
Created July 31, 2022 05:44
Basic example of a CDK custom resource to create a Postgres table
import * as AWS from "aws-sdk";
import {
CdkCustomResourceEvent,
CdkCustomResourceHandler,
CdkCustomResourceResponse,
} from "aws-lambda";
import { Client } from "pg";
import { v4 as uuid } from "uuid";
@leegilmorecode
leegilmorecode / order-service.ts
Created July 31, 2022 05:42
Custom Resource Example with the AWS CDK
// this custom resource will create our postgres table on deploy if it doesn't exist
const provider: cr.Provider = new cr.Provider(
this,
"CreateOnlineOrdersTableCustomResource",
{
onEventHandler: createTableHandler, // this lambda will be called on cfn deploy
logRetention: logs.RetentionDays.ONE_DAY,
providerFunctionName: "create-online-table-custom-resource",
}
);
@leegilmorecode
leegilmorecode / infra-stack.ts
Created July 31, 2022 05:35
Example of setting up RDS Proxy with AWS CDK
const dbConnectionGroup: ec2.SecurityGroup = new ec2.SecurityGroup(
this,
"RdsProxyDBConnection",
{
vpc,
securityGroupName: "rds-proxy-sg",
}
);
dbConnectionGroup.addIngressRule(
@leegilmorecode
leegilmorecode / infra-stack.ts
Created July 31, 2022 05:31
Workaround of using Serverless Aurora V2 with the AWS CDK
// https://github.com/aws/aws-cdk/issues/20197
enum ServerlessInstanceType {
SERVERLESS = "serverless",
}
type CustomInstanceType = ServerlessInstanceType | ec2.InstanceType;
const CustomInstanceType = {
...ServerlessInstanceType,
...ec2.InstanceType,
@leegilmorecode
leegilmorecode / auth-handler.ts
Created July 21, 2022 15:35
An example Lambda Authorizer that is performing both authZ and authN
import {
APIGatewayRequestAuthorizerEvent,
AuthResponse,
PolicyDocument,
} from "aws-lambda";
import { CognitoJwtVerifier } from "aws-jwt-verify";
import { CognitoJwtVerifierSingleUserPool } from "aws-jwt-verify/cognito-verifier";
import axios from "axios";
import jwt from "jsonwebtoken";