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 / get-orders.adapter.ts
Created July 25, 2025 12:18
Closing connections in DSQL on each Lambda invocation
import {
closeSequelizeConnection,
createSequelizeInstance,
} from '@shared/dsql-common';
import { GetOrdersRequest, schema } from './get-orders.schema';
import { MetricUnit } from '@aws-lambda-powertools/metrics';
import { logger } from '@shared';
import { withHttpHandler } from '@shared/http-handler';
import { getOrdersUseCase } from '@use-cases/get-orders';
@leegilmorecode
leegilmorecode / index.ts
Created July 25, 2025 11:27
Caching Sequalize models
import { Order, initOrderModel } from '@models/order';
import { OrderEvent, initOrderEventModel } from '@models/order-event';
import { Product, initProductModel } from '@models/product';
import { Sequelize } from 'sequelize';
let initialized = false;
export async function initModels(sequelize: Sequelize) {
if (initialized) return;
@leegilmorecode
leegilmorecode / get-order-events.adapter.ts
Created July 25, 2025 10:28
Example of caching the DSQL connection in the Lambda handler
import { GetOrderEventsRequest, schema } from './get-order-events.schema';
import { MetricUnit } from '@aws-lambda-powertools/metrics';
import { logger } from '@shared';
import { getSequelizeConnection } from '@shared/dsql-common';
import { withHttpHandler } from '@shared/http-handler';
import { getOrderEventsUseCase } from '@use-cases/get-order-events';
let connectionInitialized = false;
@leegilmorecode
leegilmorecode / dsql-connect-without-caching.ts
Created July 25, 2025 10:19
An example of DSQL connection management without caching
import { DsqlSigner } from '@aws-sdk/dsql-signer';
import { config } from '@config';
import { initModels } from '@models';
import { logger } from '@shared';
import pg from 'pg';
import { Sequelize } from 'sequelize';
const clusterId = config.get('clusterId');
const region = config.get('region');
const databaseUser = config.get('databaseUser');
@leegilmorecode
leegilmorecode / dsql-connect.ts
Created July 25, 2025 10:12
Example of caching DSQL connections and tokens
import { DsqlSigner } from '@aws-sdk/dsql-signer';
import { config } from '@config';
import { initModels } from '@models';
import { logger } from '@shared';
import pg from 'pg';
import { Sequelize } from 'sequelize';
const clusterId = config.get('clusterId');
const region = config.get('region');
const databaseUser = config.get('databaseUser');
@leegilmorecode
leegilmorecode / deploy-client.sh
Created January 7, 2025 21:13
Deploy a client static website build to an Amazon S3 bucket and create a cache invalidation for the Amazon CloudFront distribution
#!/bin/bash
# run example: ./deploy-client.sh <s3-bucket-name> <cloudfront-distribution-id> <path-to-local-files>
if [ $# -ne 3 ]; then
echo "Usage: $0 <s3-bucket-name> <cloudfront-distribution-id> <path-to-local-files>"
exit 1
fi
BUCKET_NAME=$1
@leegilmorecode
leegilmorecode / example-state-machine-handler.ts
Created August 6, 2024 11:46
An example of a typed state machine lambda handler with middy and powertools
import { Logger } from '@aws-lambda-powertools/logger';
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware';
import { Metrics } from '@aws-lambda-powertools/metrics';
import { logMetrics } from '@aws-lambda-powertools/metrics/middleware';
import { Tracer } from '@aws-lambda-powertools/tracer';
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer/middleware';
import middy from '@middy/core';
import { Handler } from 'aws-lambda';
interface StateMachineEvent {
@leegilmorecode
leegilmorecode / pipeline-stack.ts
Created March 24, 2023 18:09
Example running Cypress in a CDK Pipelines pipeline
...
// add the feature-dev stage with the relevant environment config to the pipeline
// this is the test stage (beta)
const featureDevStage: PipelineStage = new PipelineStage(
this,
'FeatureDev',
{
...environments.featureDev,
}
@leegilmorecode
leegilmorecode / create-order.cy.ts
Created March 24, 2023 18:04
Example Cypress file which loads our webpage and ensures we can interact with it as defined
describe('create-order', () => {
beforeEach(() => {
cy.visit('/');
});
it('should have modal closed on initial page load', () => {
cy.get('[data-test="create-order-modal"]').should('not.exist');
});
it('should open the create order modal', () => {
@leegilmorecode
leegilmorecode / client-stack.ts
Created March 24, 2023 17:41
Runtime configuration created for a ReactJS frontend and hosted in S3
// Setup Bucket Deployment to automatically deploy new assets and invalidate cache
new s3deploy.BucketDeployment(this, 'ClientBucketDeployment', {
sources: [
s3deploy.Source.asset(path.join(__dirname, '../../../../client/build')),
s3deploy.Source.jsonData('config.json', {
stage,
domainName: props.domainName,
subDomain,
api: `https://api-${stage}.${props.domainName}`,
}), // runtime config for client