Skip to content

Instantly share code, notes, and snippets.

View louislatreille's full-sized avatar

Louis Latreille louislatreille

View GitHub Profile
@louislatreille
louislatreille / serverless.ts
Created April 10, 2022 23:01
Serverless Streamlined - serverless.ts resources
const serverlessConfiguration: AWS = {
...
resources: {
Resources: {
...apiGatewayDefaultResponse4xx(),
...apiGatewayDefaultResponse5xx(),
...cognitoUserPool.resources,
...ddbMainTable.resources,
},
},
@louislatreille
louislatreille / cognitoUserPool.ts
Created April 10, 2022 22:57
Serverless Streamlined - cognitoUserPool.ts
import { varToString } from "./utilities";
export const createUserPool = () => {
const identityPoolRole = {
Type: "AWS::IAM::Role",
Properties: {
...
},
};
@louislatreille
louislatreille / uglyCfFunctions.ts
Created April 10, 2022 22:43
Serverless Streamlined - Ugly CF functions
...
postMessage: postMessage({
"Fn::GetAtt": ["mainTable", "Arn"]
}, {
Ref: "mainTable"
}),
...
@louislatreille
louislatreille / dynamodbMainTable.ts
Created April 10, 2022 22:40
Serverless Streamlined - dynamodbMainTable.ts
import { varToString } from "./utilities";
export const createMainTable = () => {
const mainTable = {
Type: "AWS::DynamoDB::Table",
Properties: {
AttributeDefinitions: [
{
AttributeName: "PK",
AttributeType: "S",
@louislatreille
louislatreille / createFunctionHandler.ts
Created April 10, 2022 22:25
Serverless Streamlined - createFunctionHandler.ts
import { AwsCfInstruction } from "@serverless/typescript";
import { AuthorizerConfig } from "./utilities";
export type AdditionalPolicyStatement = {
Action: string[];
Resource: AwsCfInstruction[];
Effect: "Allow" | "Deny";
};
export const createFunctionHandler = (
@louislatreille
louislatreille / postMessage.ts
Created April 10, 2022 22:23
Serverless Streamlined - postMessage
import { createFunctionHandler } from "@resources/createFunctionHandler";
import { AwsCfInstruction } from "@serverless/typescript";
export const postMessage = (mainTableArn: AwsCfInstruction, mainTableName: AwsCfInstruction) =>
createFunctionHandler(
"postMessage",
"/messages",
"POST",
undefined,
[
@louislatreille
louislatreille / serverless.ts
Created April 10, 2022 22:20
Serverless Streamlined - functions
functions: {
postMessage: postMessage(ddbMainTable.arn, ddbMainTable.name),
getMessages: getMessages(ddbMainTable.arn, ddbMainTable.name),
function3: function3(cognitoAuthorizer, cognitoUserPool.userPoolId, cognitoUserPool.identityPoolId),
},
@louislatreille
louislatreille / middleware.ts
Created April 10, 2022 22:03
Serverless Streamlined - middleware.ts
import middy from "@middy/core";
import httpJsonBodyParser from "@middy/http-json-body-parser";
import httpHeaderNormalizer from "@middy/http-header-normalizer";
import httpErrorHandler from "@middy/http-error-handler";
import cors from "@middy/http-cors";
import validator from "@middy/validator";
import httpSecurityHeaders from "@middy/http-security-headers";
import httpResponseSerializer from "@middy/http-response-serializer";
export const middyfy = (handler: any, inputSchema?: Record<string, unknown>, outputSchema?: Record<string, unknown>, origins: string[] = ["*"]) => {
@louislatreille
louislatreille / serverless.ts
Created April 10, 2022 21:38
ServerlessStreamlined - serverless.ts
import type { AWS } from "@serverless/typescript";
const serverlessConfiguration: AWS = {
service: "serverless-streamlined",
frameworkVersion: "3",
provider: {
name: "aws",
runtime: "nodejs14.x",
...
@louislatreille
louislatreille / index.ts
Created April 3, 2022 00:50
pulumiServerlessJourney - index.ts
import * as apigateway from "@pulumi/aws-apigateway";
import * as aws from "@pulumi/aws";
export const api = new apigateway.RestAPI("pulumi-serverless-journey", {
stageName: "dev",
routes: [
{
path: "/function1",
method: "POST",
eventHandler: new aws.lambda.CallbackFunction("function1", {