Skip to content

Commit

Permalink
Replace CDK context config (#1541)
Browse files Browse the repository at this point in the history
Co-authored-by: skuba <34733141+seek-oss-ci@users.noreply.github.com>
  • Loading branch information
samchungy and seek-oss-ci committed Apr 17, 2024
1 parent af29e3a commit aa9a11d
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 81 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-jeans-drum.md
@@ -0,0 +1,5 @@
---
'skuba': patch
---

template/lambda-sqs-worker-cdk: Replace CDK context based config with TypeScript config
23 changes: 0 additions & 23 deletions template/lambda-sqs-worker-cdk/cdk.json
@@ -1,28 +1,5 @@
{
"app": "pnpm exec skuba node infra/index.ts",
"context": {
"global": {
"appName": "<%- serviceName %>"
},
"dev": {
"workerLambda": {
"reservedConcurrency": 2,
"environment": {
"ENVIRONMENT": "dev"
}
},
"sourceSnsTopicArn": "TODO: sourceSnsTopicArn"
},
"prod": {
"workerLambda": {
"reservedConcurrency": 20,
"environment": {
"ENVIRONMENT": "prod"
}
},
"sourceSnsTopicArn": "TODO: sourceSnsTopicArn"
}
},
"progress": "events",
"watch": {
"include": "src/**/*.ts",
Expand Down
38 changes: 18 additions & 20 deletions template/lambda-sqs-worker-cdk/infra/appStack.test.ts
@@ -1,22 +1,6 @@
import { App, aws_sns } from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';

import cdkJson from '../cdk.json';

import { AppStack } from './appStack';

const contexts = [
{
stage: 'dev',
...cdkJson.context,
},

{
stage: 'prod',
...cdkJson.context,
},
];

const currentDate = '1212-12-12T12:12:12.121Z';

jest.useFakeTimers({
Expand All @@ -31,14 +15,28 @@ jest.useFakeTimers({
now: new Date(currentDate),
});

it.each(contexts)(
'returns expected CloudFormation stack for $stage',
(context) => {
const originalEnv = process.env.ENVIRONMENT;

afterAll(() => {
process.env.ENVIRONMENT = originalEnv;
});

afterEach(() => {
jest.resetModules();
});

it.each(['dev', 'prod'])(
'returns expected CloudFormation stack for %s',
async (env) => {
process.env.ENVIRONMENT = env;

const { AppStack } = await import('./appStack');

jest
.spyOn(aws_sns.Topic, 'fromTopicArn')
.mockImplementation((scope, id) => new aws_sns.Topic(scope, id));

const app = new App({ context });
const app = new App();

const stack = new AppStack(app, 'appStack');

Expand Down
17 changes: 7 additions & 10 deletions template/lambda-sqs-worker-cdk/infra/appStack.ts
Expand Up @@ -15,15 +15,12 @@ import {
} from 'aws-cdk-lib';
import type { Construct } from 'constructs';

import { EnvContextSchema, StageContextSchema } from '../shared/context-types';
import { config } from './config';

export class AppStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);

const stage = StageContextSchema.parse(this.node.tryGetContext('stage'));
const context = EnvContextSchema.parse(this.node.tryGetContext(stage));

const accountPrincipal = new aws_iam.AccountPrincipal(this.account);

const kmsKey = new aws_kms.Key(this, 'kms-key', {
Expand Down Expand Up @@ -52,7 +49,7 @@ export class AppStack extends Stack {
const topic = aws_sns.Topic.fromTopicArn(
this,
'source-topic',
context.sourceSnsTopicArn,
config.sourceSnsTopicArn,
);

topic.addSubscription(new aws_sns_subscriptions.SqsSubscription(queue));
Expand Down Expand Up @@ -89,13 +86,13 @@ export class AppStack extends Stack {
functionName: '<%- serviceName %>',
environment: {
...defaultWorkerEnvironment,
...context.workerLambda.environment,
...config.workerLambda.environment,
},
// https://github.com/aws/aws-cdk/issues/28237
// This forces the lambda to be updated on every deployment
// If you do not wish to use hotswap, you can remove the new Date().toISOString() from the description
description: `Updated at ${new Date().toISOString()}`,
reservedConcurrentExecutions: context.workerLambda.reservedConcurrency,
reservedConcurrentExecutions: config.workerLambda.reservedConcurrency,
});

const alias = worker.addAlias('live', {
Expand All @@ -104,7 +101,7 @@ export class AppStack extends Stack {

alias.addEventSource(
new aws_lambda_event_sources.SqsEventSource(queue, {
maxConcurrency: context.workerLambda.reservedConcurrency,
maxConcurrency: config.workerLambda.reservedConcurrency,
}),
);

Expand All @@ -119,7 +116,7 @@ export class AppStack extends Stack {
functionName: '<%- serviceName %>-pre-hook',
environment: {
...defaultWorkerEnvironment,
...context.workerLambda.environment,
...config.workerLambda.environment,
FUNCTION_NAME_TO_INVOKE: worker.functionName,
},
},
Expand All @@ -138,7 +135,7 @@ export class AppStack extends Stack {
functionName: '<%- serviceName %>-post-hook',
environment: {
...defaultWorkerEnvironment,
...context.workerLambda.environment,
...config.workerLambda.environment,
FUNCTION_NAME_TO_PRUNE: worker.functionName,
},
},
Expand Down
41 changes: 41 additions & 0 deletions template/lambda-sqs-worker-cdk/infra/config.ts
@@ -0,0 +1,41 @@
import { z } from 'zod';

const environment = z.enum(['dev', 'prod']).parse(process.env.ENVIRONMENT);

type Environment = typeof environment;

export interface Config {
appName: string;
workerLambda: {
reservedConcurrency: number;
environment: {
ENVIRONMENT: Environment;
};
};
sourceSnsTopicArn: string;
}

export const configs: Record<Environment, Config> = {
dev: {
appName: '<%- serviceName %>',
workerLambda: {
reservedConcurrency: 2,
environment: {
ENVIRONMENT: 'dev',
},
},
sourceSnsTopicArn: 'TODO: sourceSnsTopicArn',
},
prod: {
appName: '<%- serviceName %>',
workerLambda: {
reservedConcurrency: 20,
environment: {
ENVIRONMENT: 'prod',
},
},
sourceSnsTopicArn: 'TODO: sourceSnsTopicArn',
},
};

export const config = configs[environment];
7 changes: 2 additions & 5 deletions template/lambda-sqs-worker-cdk/infra/index.ts
@@ -1,14 +1,11 @@
import { App } from 'aws-cdk-lib';

import { GlobalContextSchema } from '../shared/context-types';

import { AppStack } from './appStack';
import { config } from './config';

const app = new App();

const context = GlobalContextSchema.parse(app.node.tryGetContext('global'));

// eslint-disable-next-line no-new
new AppStack(app, 'appStack', {
stackName: context.appName,
stackName: config.appName,
});
2 changes: 1 addition & 1 deletion template/lambda-sqs-worker-cdk/package.json
Expand Up @@ -2,7 +2,7 @@
"private": true,
"license": "UNLICENSED",
"scripts": {
"deploy": "cdk deploy appStack --require-approval never --context stage=${ENVIRONMENT}",
"deploy": "cdk deploy appStack --require-approval never",
"deploy:hotswap": "pnpm --silent deploy --hotswap",
"deploy:watch": "pnpm --silent deploy:hotswap --watch",
"format": "skuba format",
Expand Down
22 changes: 0 additions & 22 deletions template/lambda-sqs-worker-cdk/shared/context-types.ts

This file was deleted.

0 comments on commit aa9a11d

Please sign in to comment.