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 / docker-compose.yml
Created March 8, 2022 20:16
Docker compose file for DynamoDB Local
version: '3.8'
services:
dynamodb-local:
command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
image: "amazon/dynamodb-local:latest"
container_name: dynamodb-local-latest
ports:
- "8000:8000"
volumes:
- "./docker/dynamodb:/home/dynamodblocal/data"
@leegilmorecode
leegilmorecode / jest.int.config.js
Created March 8, 2022 20:12
Jest integration test config
module.exports = {
testEnvironment: "node",
roots: ["<rootDir>/src"],
testMatch: ["**/*.int.ts"],
transform: {
"^.+\\.tsx?$": "ts-jest",
},
};
@leegilmorecode
leegilmorecode / jest.unit.config.js
Created March 8, 2022 20:10
Jest unit test config
module.exports = {
testEnvironment: "node",
roots: ["<rootDir>/src"],
testMatch: ["**/*.spec.ts"],
transform: {
"^.+\\.tsx?$": "ts-jest",
},
};
@leegilmorecode
leegilmorecode / index.ts
Last active February 18, 2022 09:50
DocumentDB proxy service layer using Express and ran using Fargate
import {
Collection,
Db,
Document,
FindOptions,
InsertOneOptions,
InsertOneResult,
MongoClient,
MongoClientOptions,
WithId,
@leegilmorecode
leegilmorecode / create-order.ts
Created February 17, 2022 13:50
Example Lambda which consumes the DocumentDB database using our proxy service layer over HTTP
import {
APIGatewayEvent,
APIGatewayProxyHandler,
APIGatewayProxyResult,
} from "aws-lambda";
import { config } from "../../../config";
import { create } from "../../../service-layer";
export const handler: APIGatewayProxyHandler = async ({
@leegilmorecode
leegilmorecode / create-order.ts
Created February 17, 2022 13:47
Lambda which creates an order directly against the DocumentDB database
import {
APIGatewayEvent,
APIGatewayProxyHandler,
APIGatewayProxyResult,
} from "aws-lambda";
import { Collection, Db, InsertOneResult, MongoClient } from "mongodb";
import { buildMongoClient, connectToDatabase } from "../../../common";
import { config } from "../../../config";
@leegilmorecode
leegilmorecode / service-layer.ts
Created February 17, 2022 13:46
Consumer service layer package for calling out to our DocumentDB service layer
import axios, { AxiosResponse } from "axios";
export const findOne = async (
serviceLayerUrl: string,
collectionName: string,
id: string | number
) => {
const { data }: { data: Order } = await axios.post(
`http://${serviceLayerUrl}/findOne`,
{
@leegilmorecode
leegilmorecode / serverless-blog-stack.ts
Created January 25, 2022 13:57
Example of a mutation resolver which evicts the entry from the AppSync cache
// updateBlog mutation with cache invalidation
blogTableDataSource.createResolver({
typeName: "Mutation",
fieldName: "updateBlog",
// this is an example of a vtl template generated with the helper methods
requestMappingTemplate: appsync.MappingTemplate.dynamoDbPutItem(
appsync.PrimaryKey.partition("id").is("input.id"),
appsync.Values.projecting("input")
),
// this is an example of an inline vtl response template (you can also pull in from a file)
@leegilmorecode
leegilmorecode / invalidate-cache.vtl
Created January 25, 2022 05:26
Example of evicting a blog article from the AppSync cache dynamically
#set($cachingKeys = {})
$util.qr($cachingKeys.put("context.arguments.id", $context.arguments.id))
$extensions.evictFromApiCache("Query", "getBlogNoDax", $cachingKeys)
@leegilmorecode
leegilmorecode / serverless-blog-stack.ts
Created January 25, 2022 05:12
Example of a CDK cached resolver in AppSync api
// appsync lambda data source
const blogNoDaxDataSource: appsync.LambdaDataSource =
new appsync.LambdaDataSource(this, "GetBlogNoDaxLambdaDataSource", {
api,
lambdaFunction: getBlogNoDaxHandler,
description: "Get Blog (no dax) Lambda Data Source",
name: "GetBlogNoDaxLambdaDataSource",
});
// appsync resolver