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 / 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,
@leegilmorecode
leegilmorecode / internal-stack.ts
Created July 21, 2022 06:50
Example of adding a pre token generation lambda to a Cognito userpool
// create the cognito user pool for auth
const authUserPool: cognito.UserPool = new cognito.UserPool(
this,
"SushiAuthUserPool",
{
userPoolName: "SushiUserAuthUserPool",
removalPolicy: RemovalPolicy.DESTROY,
lambdaTriggers: {
// https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html
preTokenGeneration,
@leegilmorecode
leegilmorecode / pre-generation.ts
Created July 21, 2022 06:48
Example of a pre token generation lambda with Amazon Cognito
import {
PreTokenGenerationTriggerEvent,
PreTokenGenerationTriggerHandler,
} from "aws-lambda";
import axios from "axios";
// get the users permissions from a separate service using their token sub
async function getUserPermissions(userId: string) {
const { data: userPermissions } = await axios.request({
@leegilmorecode
leegilmorecode / regexes.ts
Created June 29, 2022 11:27
Locale regexes
interface Regexes {
[key: string]: any;
}
import { getLocale } from "../dynamic-imports";
const localeRegexes: Regexes = {
global: {
firstName: "^[a-z A-Z]+$",
surname: "^[a-z A-Z]+$",
@leegilmorecode
leegilmorecode / create-sale.schema.ts
Created June 29, 2022 11:26
UK specific version of the create sale schema
import { regexes } from "@shared/regexes";
export const schema = {
type: "object",
required: ["id", "firstName", "surname", "age"],
maxProperties: 4,
minProperties: 4,
properties: {
id: {
type: "number",
@leegilmorecode
leegilmorecode / error-messages.ts
Created June 29, 2022 11:22
Example of returning locale specific error messages using placeholders at build time
interface Errors {
[key: string]: any;
}
import { getLocale } from "../dynamic-imports";
// each of these could have different languages or messages
// specific to the locale
const localeErrorMessages: Errors = {
global: {