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-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";
@leegilmorecode
leegilmorecode / get-order.ts
Created July 21, 2022 15:16
An example Lambda which is utilising Middy middeware through an attached Lambda Layer
import {
APIGatewayProxyEvent,
APIGatewayProxyEventPathParameters,
APIGatewayProxyHandler,
APIGatewayProxyResult,
} from "aws-lambda";
import {
hydrateContext,
validateCompanyAccessMiddleware,
validateTokenUserAccessMiddleware,
@leegilmorecode
leegilmorecode / hydrate-context.ts
Created July 21, 2022 15:10
Lambda middleware using Middy which hydrates the user missions based on an external service
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
import { UserContext } from "../../../types";
import axios from "axios";
import middy from "@middy/core";
// middleware to ensure that the user is part of the given company i.e. permissions
export const hydrateContext = (): middy.MiddlewareObj<
APIGatewayProxyEvent,
APIGatewayProxyResult
@leegilmorecode
leegilmorecode / validate-company-access.ts
Created July 21, 2022 14:57
Middy middleware which is utilised by our Lambda functions
import { APIGatewayProxyEvent, APIGatewayProxyResult } from "aws-lambda";
import { UserContext } from "../../../types";
import middy from "@middy/core";
// middleware to ensure that the user is part of the given company i.e. permissions
// when getting their order for a specific company, as well as checking their role
export const validateCompanyAccessMiddleware = (
role: string
): middy.MiddlewareObj<APIGatewayProxyEvent, APIGatewayProxyResult> => {
@leegilmorecode
leegilmorecode / Internal.ts
Created July 21, 2022 14:49
Example of a Lambda Layer in the CDK
// create our lambda layer for our middleware
const validationLayer: lambda.LayerVersion = new lambda.LayerVersion(
this,
"ValidationLayer",
{
compatibleRuntimes: [
lambda.Runtime.NODEJS_12_X,
lambda.Runtime.NODEJS_14_X,
],
code: lambda.Code.fromAsset("../layers/validation"),
@leegilmorecode
leegilmorecode / external.ts
Created July 21, 2022 14:45
Example of a Cognito Authorizer being added to an endpoint on API Gateway
// add the cognito authorizer to our api which validates our tokens using cognito
const cognitoAuthorizer: apigw.CfnAuthorizer = new apigw.CfnAuthorizer(
this,
"APIGatewayAuthorizer",
{
name: "sushi-orders-authorizer",
identitySource: "method.request.header.Authorization",
providerArns: [userPool.userPoolArn],
restApiId: ordersAPI.restApiId,
type: apigw.AuthorizationType.COGNITO,