Skip to content

Commit

Permalink
Iterate on Cosmo Lambda version
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehnix committed Mar 11, 2024
1 parent 5dd78b0 commit 45edd57
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 96 deletions.
Binary file modified benchmark/bun.lockb
Binary file not shown.
15 changes: 11 additions & 4 deletions benchmark/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import benchmarkPayloadProducts from './payload-products.json';
/**
* Benchmark configuration values.
*/
const COLD_STARTS = 10;
const WARM_STARTS = 10;
const COLD_STARTS = 1;
const WARM_STARTS = 1;
// const MEMORY_SIZES = [128, 256, 512, 1024, 2048] as const;
const MEMORY_SIZES = [512, 1024, 2048] as const;
const MEMORY_SIZES = [1024] as const;

// How long to wait for XRay to gather all the traces.
const WAIT_FOR_XRAY = 15;
Expand Down Expand Up @@ -281,7 +281,14 @@ const invokeFunctions = async (functionName: string, memorySize: number) => {
body: mkPayload(),
headers: { 'Content-Type': 'application/json' },
});
const payload = await res.json();
let payload: { error: any; errorMessage: any; errorType: any; data: any };
try {
payload = await res.json();
} catch (err) {
console.error('Error:', err);
console.error('Response:', res);
process.exit(1);
}
const statusCode = res.status;

if (statusCode !== 200 || payload.error || payload.errorMessage || payload.errorType || !payload.data) {
Expand Down
5 changes: 3 additions & 2 deletions deployment/bin/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';

/** We want to be able to target a specific stack for build and deployment. We extract
* the requested stack from CDK and use that to determine what we should build.
Expand All @@ -8,13 +9,13 @@ import * as cdk from 'aws-cdk-lib';
* cdk synth <stack-name> --exclusively
* ```
*/
export const matchesStack = (app: cdk.App, stackName: string): boolean => {
export const matchesStack = (app: cdk.App | Construct, stackName: string, exactMatch: boolean = false): boolean => {
const bundlingStacks = app.node.tryGetContext('aws:cdk:bundling-stacks') as Array<string>;
const buildAllStacks = bundlingStacks.includes('**');
const matches =
buildAllStacks ||
bundlingStacks.length === 0 ||
bundlingStacks.some((s) => s === stackName || s === `${stackName}/*`);
bundlingStacks.some((s) => s === stackName || (!exactMatch && s === `${stackName}/*`));
if (matches && process.env.VERBOSE) {
console.log(`Building stack: ${stackName}`);
}
Expand Down
25 changes: 3 additions & 22 deletions deployment/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const base: Config = {
runtime: 'lambda',
path: '/graphql',
pinToVersionedApi: false,
developmentMode: false,
},

subgraphs: [
Expand Down Expand Up @@ -52,28 +53,7 @@ const development: Config = {
runtime: 'lambda',
path: '/graphql',
pinToVersionedApi: false,
},
experimental: {
additionalSupergraphs: [
{
service: 'cosmo',
runtime: 'lambda',
path: '/graphql-cosmo',
pinToVersionedApi: false,
},
{
service: 'gateway',
runtime: 'lambda',
path: '/graphql-gateway',
pinToVersionedApi: false,
},
{
service: 'mesh',
runtime: 'lambda',
path: '/graphql-mesh',
pinToVersionedApi: false,
},
],
developmentMode: true,
},
};

Expand All @@ -84,6 +64,7 @@ const production: Config = {
runtime: 'lambda',
path: '/graphql',
pinToVersionedApi: false,
developmentMode: false,
},
};

Expand Down
28 changes: 26 additions & 2 deletions deployment/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';

import { matchesStack } from '../bin/helpers';
import { config as configMap } from '../config';
import { App, Config, Supergraph, validEnvironments } from './types';

Expand All @@ -11,8 +15,10 @@ const resolveConfig = (env: string | undefined): Config => {
} else if (!validEnvironments.includes(env as any)) {
throw new Error(`ENVIRONMENT '${env}' is not a valid option. Possible values ${validEnvironments.join(', ')}`);
} else if (env in configMap) {
console.log(`👉 Using '${env}' configuration.`);
return configMap[env];
}
console.log("👉 Using 'Base' configuration as no specific environment was set via 'ENVIRONMENT'.");
return configMap['Base'];
};

Expand Down Expand Up @@ -61,7 +67,7 @@ export const setupSupergraph = <N extends Supergraph['service'], R extends Super
name: N,
runtime: R,
supergraphRoutes: { [key: string]: string },
stackFn: (additionalConfig?: Specific<Supergraph, { service: N; runtime: R }>) => string,
stackFn: (config: Specific<Supergraph, { service: N; runtime: R }>) => string,
) => {
const isMainSupergraph = config.supergraph.service === name && config.supergraph.runtime === runtime;
// We cast our result to `undefined | Specific` to narrow down the type.
Expand All @@ -71,11 +77,12 @@ export const setupSupergraph = <N extends Supergraph['service'], R extends Super

// If the supergraph is the main one, or if it's an additional supergraph, set up the stack.
if (isMainSupergraph || additionalSupergraphConfig) {
const url = stackFn(additionalSupergraphConfig);
if (isMainSupergraph) {
const url = stackFn(config.supergraph as Specific<Supergraph, { service: N; runtime: R }>);
supergraphRoutes[config.supergraph.path] = url;
}
if (additionalSupergraphConfig) {
const url = stackFn(additionalSupergraphConfig);
supergraphRoutes[additionalSupergraphConfig.path] = url;
}
}
Expand Down Expand Up @@ -113,3 +120,20 @@ export const setupApp = <N extends App['service']>(
return stackFn(appConfig);
}
};

/**
* Add dependencies to the Supergraph stack and skip dependencies if the Supergraph stack is
* being directly deployed.
*
* Example:
* ```ts
* addSupergraphDependencies(scope, `${id}/${supergraphId}`, supergraph, subgraphs);
* ```
*/
export const addSupergraphDependencies = (scope: Construct, id: string, supergraph: cdk.Stack, subgraphs: any[]) => {
if (!matchesStack(scope, id, true)) {
subgraphs.forEach((subgraph) => supergraph.addDependency(subgraph));
} else {
console.log(`👉 Skipping dependencies for ${id} as it is being directly deployed`);
}
};
61 changes: 28 additions & 33 deletions deployment/lib/services/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';

import { config, setupApp, setupSupergraph } from '../helpers';
import { addSupergraphDependencies, config, setupApp, setupSupergraph } from '../helpers';
import * as routerAppRunner from './app-runner';
import * as lambdaFn from './lambda';
import * as s3Website from './s3-website';
Expand Down Expand Up @@ -31,7 +31,7 @@ export class Stack extends cdk.Stack {
super(scope, id, props);

// Collect environment variables pointing to each subgraph URL for the Supergraph.
const subgraphEnvsSsm = {};
const subgraphEnvsSsm: { [key: string]: string } = {};
// Collect all subgraphs that the supergraph will depend on.
const subgraphs: Stack[] = [];

Expand Down Expand Up @@ -62,7 +62,8 @@ export class Stack extends cdk.Stack {

// Set up our Apollo Gateway that pieces together the microservices.
setupSupergraph('gateway', 'lambda', supergraphRoutesSsm, (config) => {
const supergraph = new lambdaFn.Stack(this, 'MsGateway', {
const supergraphId = 'MsGateway';
const supergraph = new lambdaFn.Stack(this, supergraphId, {
...props,
functionName: 'ms-gateway',
handler: 'lambda.graphqlHandler',
Expand All @@ -75,13 +76,14 @@ export class Stack extends cdk.Stack {
},
});
supergraphs.push(supergraph);
subgraphs.forEach((subgraph) => supergraph.addDependency(subgraph));
addSupergraphDependencies(scope, `${id}/${supergraphId}`, supergraph, subgraphs);
return config?.pinToVersionedApi ? supergraph.aliasUrlParameterName : supergraph.latestUrlParameterName;
});

// Set up our GraphQL Mesh that pieces together the microservices.
setupSupergraph('mesh', 'lambda', supergraphRoutesSsm, (config) => {
const supergraph = new lambdaFn.Stack(this, 'MsMesh', {
const supergraphId = 'MsMesh';
const supergraph = new lambdaFn.Stack(this, supergraphId, {
...props,
functionName: 'ms-mesh',
handler: 'lambda.graphqlHandler',
Expand All @@ -94,13 +96,14 @@ export class Stack extends cdk.Stack {
},
});
supergraphs.push(supergraph);
subgraphs.forEach((subgraph) => supergraph.addDependency(subgraph));
addSupergraphDependencies(scope, `${id}/${supergraphId}`, supergraph, subgraphs);
return config?.pinToVersionedApi ? supergraph.aliasUrlParameterName : supergraph.latestUrlParameterName;
});

// Set up our Apollo Router Lambda that pieces together the microservices.
// Set up our Cosmo Router Lambda that pieces together the microservices.
setupSupergraph('router', 'lambda', supergraphRoutesSsm, (config) => {
const supergraph = new lambdaFn.Stack(this, 'MsRouterLambda', {
const supergraphId = 'MsRouterLambda';
const supergraph = new lambdaFn.Stack(this, supergraphId, {
...props,
functionName: 'ms-router',
assets: 'artifacts/ms-router',
Expand All @@ -111,15 +114,26 @@ export class Stack extends cdk.Stack {
environmentFromSsm: {
...subgraphEnvsSsm,
},
environment: {
DEV_MODE: config.developmentMode ? 'true' : 'false',
DISABLE_TELEMETRY: 'true',
// FIXME: These are not read by the Lambda Cosmo Router.
GRAPHQL_PATH: '/',
PLAYGROUND_PATH: '/graphiql',
CONFIG_PATH: 'config.yaml',
ROUTER_CONFIG_PATH: 'router.json',
ENGINE_ENABLE_REQUEST_TRACING: 'false',
},
});
supergraphs.push(supergraph);
subgraphs.forEach((subgraph) => supergraph.addDependency(subgraph));
addSupergraphDependencies(scope, `${id}/${supergraphId}`, supergraph, subgraphs);
return config?.pinToVersionedApi ? supergraph.aliasUrlParameterName : supergraph.latestUrlParameterName;
});

// Set up our Apollo Router App Runner that pieces together the microservices.
setupSupergraph('router', 'app-runner', supergraphRoutesSsm, () => {
const supergraph = new routerAppRunner.Stack(this, 'MsRouterApp', {
const supergraphId = 'MsRouterApp';
const supergraph = new routerAppRunner.Stack(this, supergraphId, {
...props,
repo: 'ms-router',
tag: process.env.SUPERGRAPH_ROUTER_IMAGE_TAG ?? `latest`,
Expand All @@ -131,33 +145,14 @@ export class Stack extends cdk.Stack {
memory: appRunner.Memory.HALF_GB,
});
supergraphs.push(supergraph);
subgraphs.forEach((subgraph) => supergraph.addDependency(subgraph));
addSupergraphDependencies(scope, `${id}/${supergraphId}`, supergraph, subgraphs);
return supergraph.urlParameterName;
});

// Set up our Cosmo Router Lambda that pieces together the microservices.
// setupSupergraph('cosmo', 'lambda', supergraphRoutesSsm, (config) => {
// const supergraph = new lambdaFn.Stack(this, 'MsCosmo', {
// ...props,
// functionName: 'ms-cosmo',
// assets: 'artifacts/ms-cosmo',
// billingGroup: 'ms-cosmo',
// runtime: lambda.Runtime.PROVIDED_AL2023,
// lambdaInsights: false,
// environmentFromSsm: {
// ...subgraphEnvsSsm,
// },
// environment: {
// PATH_ROUTER: './router',
// },
// });
// supergraphs.push(supergraph);
// subgraphs.forEach((subgraph) => supergraph.addDependency(subgraph));
// return config?.pinToVersionedApi ? supergraph.aliasUrlParameterName : supergraph.latestUrlParameterName;
// });

setupSupergraph('cosmo', 'lambda', supergraphRoutesSsm, (config) => {
const supergraph = new lambdaFn.Stack(this, 'MsCosmo', {
const supergraphId = 'MsCosmo';
const supergraph = new lambdaFn.Stack(this, supergraphId, {
...props,
functionName: 'ms-cosmo',
assets: 'artifacts/ms-cosmo',
Expand All @@ -174,7 +169,7 @@ export class Stack extends cdk.Stack {
},
});
supergraphs.push(supergraph);
subgraphs.forEach((subgraph) => supergraph.addDependency(subgraph));
addSupergraphDependencies(scope, `${id}/${supergraphId}`, supergraph, subgraphs);
return config?.pinToVersionedApi ? supergraph.aliasUrlParameterName : supergraph.latestUrlParameterName;
});

Expand Down
1 change: 1 addition & 0 deletions deployment/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export type Supergraph =
runtime: 'lambda';
path: string;
pinToVersionedApi: boolean;
developmentMode: boolean;
}
| {
service: 'router';
Expand Down
Loading

0 comments on commit 45edd57

Please sign in to comment.