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 / 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",
async function handler(): Promise<{ body: string; statusCode: number }> {
console.log("get-stock.handler - started");
return {
body: JSON.stringify({
stock: [
{
stockId: 1,
description: "hammers",
},
// add a security group for the vpc endpoint
const sg: ec2.SecurityGroup = new ec2.SecurityGroup(this, "stock-vpc-sg", {
vpc,
allowAllOutbound: true,
securityGroupName: "stock-vpc-sg",
});
sg.addIngressRule(ec2.Peer.ipv4(props.cidr), ec2.Port.tcp(443));
// create the vpc endpoint
// create an internal application load balancer
this.stockLoadBalancer = new elbv2.ApplicationLoadBalancer(
this,
"stock-internal-elb",
{
vpc,
http2Enabled: false,
loadBalancerName: "stock-internal-elb",
vpcSubnets: vpc.selectSubnets({
subnetType: ec2.SubnetType.PRIVATE_ISOLATED,
import axios from "axios";
type Stock = {
stockId: number;
description: string;
};
type StockResponse = {
stock: Stock[];
};
@leegilmorecode
leegilmorecode / example.ts
Created December 13, 2021 21:26
Private DNS on VPC with VPC Endpoints
import axios from "axios";
async function handler() {
const { data } = await axios.get(
`https://private-api-id.execute-api.eu-west-1.amazonaws.com/prod/stock`, // 👈 this private call would work
{
headers: {
"x-api-key": "super-secret-api-key",
},
}