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-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
@leegilmorecode
leegilmorecode / list-blogs-cache-in-tmp.ts
Created January 20, 2022 11:07
An example of caching the blog logos in the Lambda tmp directory
import * as AWS from "aws-sdk";
import { APIGatewayProxyHandler, APIGatewayProxyResult } from "aws-lambda";
import { v4 as uuid } from "uuid";
const fs = require("fs").promises;
const s3 = new AWS.S3();
// a function to write the files to tmp on the lambda
@leegilmorecode
leegilmorecode / list-blogs-cache-in-memory.ts
Created January 18, 2022 12:35
Example of a simple in memory Lambda cache
import { APIGatewayProxyHandler, APIGatewayProxyResult } from "aws-lambda";
import { v4 as uuid } from "uuid";
const data = require("data-api-client")({
secretArn: process.env.SECRET_ARN as string,
resourceArn: process.env.CLUSTER_ARN as string,
database: process.env.DB,
continueAfterTimeout: true,
includeResultMetadata: false,
@leegilmorecode
leegilmorecode / get-blog.ts
Created January 17, 2022 14:57
Example of using DAX within a Lambda function which accessed via AppSync
import * as AWS from "aws-sdk";
import { AppSyncResolverEvent, AppSyncResolverHandler } from "aws-lambda";
import { Blog, QueryIdArgs } from "../../types";
import { v4 as uuid } from "uuid";
const AmazonDaxClient = require("amazon-dax-client");
const dax = new AmazonDaxClient({
@leegilmorecode
leegilmorecode / aws-blog-stack.ts
Created December 31, 2021 13:54
CDK API Gateway setting for path based caching
// integrate the lambda to the method - GET /blogs
blogs.addMethod(
"GET",
new apigw.LambdaIntegration(listBlogsHandler, {
proxy: true,
allowTestInvoke: true,
})
);
// integrate the lambda to the method - GET /blog/{id}
@leegilmorecode
leegilmorecode / aws-block-stack.ts
Created December 31, 2021 13:49
Example of API Gateway caching on AWS using the CDK
// create the rest API for accessing our lambdas
const api: apigw.RestApi = new apigw.RestApi(this, "blogs-api", {
description: "blogs api gateway",
deploy: true,
deployOptions: {
// this enables caching on our api gateway, with a ttl of five minutes (unless overridden per method)
cachingEnabled: true,
cacheClusterEnabled: true,
cacheDataEncrypted: true,
stageName: "prod",