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-sale.ts
Created June 29, 2022 11:19
An example of a Lambda which is utilising error messages and schemas at build time which are locale specific
import {
APIGatewayEvent,
APIGatewayProxyHandler,
APIGatewayProxyResult,
} from "aws-lambda";
// we import these values which are locale aware
import { errorMessages } from "@shared/error-messages";
import { schema } from "./create-sale.schema";
import { stringFormat } from "@shared/string-format";
@leegilmorecode
leegilmorecode / lambdas.ts
Created June 29, 2022 11:16
Dynamically changing the lambda entry point based on locale
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as nodeLambda from "aws-cdk-lib/aws-lambda-nodejs";
import * as path from "path";
import { dynamicPath, getLocale } from "@shared/dynamic-imports";
export const getSalesLambdaProps: nodeLambda.NodejsFunctionProps = {
runtime: lambda.Runtime.NODEJS_14_X,
functionName: "getSalesLambda",
// the entry point utilises our dynamic path generator
@leegilmorecode
leegilmorecode / sale-stack.ts
Created June 29, 2022 11:13
Example of a stack which has its properties defined at build time depending on the locale
import * as apigw from "aws-cdk-lib/aws-apigateway";
import * as nodeLambda from "aws-cdk-lib/aws-lambda-nodejs";
import { Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
interface SalesProps extends StackProps {
apiProps: apigw.RestApiProps;
getSalesLambdaProps: nodeLambda.NodejsFunctionProps;
@leegilmorecode
leegilmorecode / dynamic-imprt.ts
Created June 29, 2022 11:10
Example of dynamically importing modules based on locale environment variable
import * as fs from "fs";
import * as path from "path";
function rootDir(): string {
return __dirname;
}
// function to return the locale lowercase
export function getLocale(): string {
if (!process.env.LOCALE) {
@leegilmorecode
leegilmorecode / sales.ts
Created June 29, 2022 11:07
Example of a CDK stack having the properties for the infra pulled in at build time based on locale
#!/usr/bin/env node
import "source-map-support/register";
import * as apigw from "aws-cdk-lib/aws-apigateway";
import * as cdk from "aws-cdk-lib";
import * as nodeLambda from "aws-cdk-lib/aws-lambda-nodejs";
import { SalesStack } from "@lib/sales-stack";
import { dynamicImport } from "@shared/dynamic-imports";
@leegilmorecode
leegilmorecode / archive.ts
Created June 12, 2022 07:22
An example of using Archive and Replay with EventBridge in the CDK
// create the tires orders event bus
const ordersEventBus: events.EventBus = new events.EventBus(
this,
"orders-event-bus",
{
eventBusName: "orders-event-bus",
}
);
ordersEventBus.applyRemovalPolicy(RemovalPolicy.DESTROY);
@leegilmorecode
leegilmorecode / waf.ts
Created June 11, 2022 18:12
Example of using AWS WAF with API Gateway to restrict to whitelisted API source
// create the waf ip set for the api
const webAclIPSet = new waf.CfnIPSet(this, "TiresWhitelistIpSet", {
name: "tires-whitelist-ip-set",
addresses: [`${props.ordersApiIp}/32`], // the source orders api ip address which we allow
ipAddressVersion: "IPV4",
scope: "REGIONAL",
description: "tires api ip set",
});
const webacl = new waf.CfnWebACL(this, "TiresWhitelistWebAcl", {
@leegilmorecode
leegilmorecode / adding-pitr-to-dynamodb-table.ts
Last active June 4, 2022 07:42
Example of adding point in time recovery to a DynamoDB table in the CDK
// create the stock table for storing the tire orders
const stockTable: dynamodb.Table = new dynamodb.Table(
this,
"StockOrdersTable",
{
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
encryption: dynamodb.TableEncryption.AWS_MANAGED,
pointInTimeRecovery: true, // we add point in time recovery for our table
tableName: "StockOrders",
contributorInsightsEnabled: true,
@leegilmorecode
leegilmorecode / add-vpc-flow-logs.ts
Created June 4, 2022 07:24
Example of adding VPC Flow Logs to our VPC which are logged to CloudWatch
// ensure our flow logs go to cloudwatch
vpc.addFlowLog("FlowLogS3", {
destination: ec2.FlowLogDestination.toCloudWatchLogs(),
trafficType: ec2.FlowLogTrafficType.ALL,
});
@leegilmorecode
leegilmorecode / get-eip-of-nat-gateway.ts
Created June 4, 2022 07:17
Example of getting the elastic ip address of our nat gateway (one in this example)
// create the vpc for the car company solution
const vpc: ec2.Vpc = new ec2.Vpc(this, "CarOrdersVPC", {
cidr: "10.0.0.0/16",
maxAzs: 2, // for the demo only lets add 2 az's note: in production this should be at least 3
natGateways: 1, // The nat gateway has to be provisioned in a public subnet, with a public ip address to access the internet through internet gateway
subnetConfiguration: [
{
cidrMask: 24,
name: "private-subnet",
subnetType: ec2.SubnetType.PRIVATE_WITH_NAT,