Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add version variable to cdk template logger #1548

Merged
merged 22 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slow-seahorses-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'skuba': patch
---

template/lambda-sqs-worker-cdk: Add worker config file
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ exports[`returns expected CloudFormation stack for dev 1`] = `
"ENVIRONMENT": "dev",
"NODE_ENV": "production",
"NODE_OPTIONS": "--enable-source-maps",
"SERVICE": "serviceName",
"VERSION": "local",
},
},
"FunctionName": "serviceName",
Expand Down Expand Up @@ -644,6 +646,8 @@ exports[`returns expected CloudFormation stack for dev 1`] = `
},
"NODE_ENV": "production",
"NODE_OPTIONS": "--enable-source-maps",
"SERVICE": "serviceName",
"VERSION": "local",
},
},
"FunctionName": "serviceName-post-hook",
Expand Down Expand Up @@ -796,6 +800,8 @@ exports[`returns expected CloudFormation stack for dev 1`] = `
},
"NODE_ENV": "production",
"NODE_OPTIONS": "--enable-source-maps",
"SERVICE": "serviceName",
"VERSION": "local",
},
},
"FunctionName": "serviceName-pre-hook",
Expand Down Expand Up @@ -1493,6 +1499,8 @@ exports[`returns expected CloudFormation stack for prod 1`] = `
"ENVIRONMENT": "prod",
"NODE_ENV": "production",
"NODE_OPTIONS": "--enable-source-maps",
"SERVICE": "serviceName",
"VERSION": "local",
},
},
"FunctionName": "serviceName",
Expand Down Expand Up @@ -1686,6 +1694,8 @@ exports[`returns expected CloudFormation stack for prod 1`] = `
},
"NODE_ENV": "production",
"NODE_OPTIONS": "--enable-source-maps",
"SERVICE": "serviceName",
"VERSION": "local",
},
},
"FunctionName": "serviceName-post-hook",
Expand Down Expand Up @@ -1838,6 +1848,8 @@ exports[`returns expected CloudFormation stack for prod 1`] = `
},
"NODE_ENV": "production",
"NODE_OPTIONS": "--enable-source-maps",
"SERVICE": "serviceName",
"VERSION": "local",
},
},
"FunctionName": "serviceName-pre-hook",
Expand Down
20 changes: 14 additions & 6 deletions template/lambda-sqs-worker-cdk/infra/config.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import { z } from 'zod';
import { Env } from 'skuba-dive';

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

type Environment = typeof environment;
type Environment = (typeof ENVIRONMENTS)[number];

export interface Config {
const environment = Env.oneOf(ENVIRONMENTS)('ENVIRONMENT');

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

export const configs: Record<Environment, Config> = {
const configs: Record<Environment, Config> = {
dev: {
appName: '<%- serviceName %>',
workerLambda: {
reservedConcurrency: 2,
environment: {
ENVIRONMENT: 'dev',
SERVICE: '<%- serviceName %>',
VERSION: Env.string('VERSION', { default: 'local' }),
},
},
sourceSnsTopicArn: 'TODO: sourceSnsTopicArn',
Expand All @@ -32,10 +38,12 @@ export const configs: Record<Environment, Config> = {
reservedConcurrency: 20,
environment: {
ENVIRONMENT: 'prod',
SERVICE: '<%- serviceName %>',
VERSION: Env.string('VERSION', { default: 'local' }),
},
},
sourceSnsTopicArn: 'TODO: sourceSnsTopicArn',
},
};

export const config = configs[environment];
export const config: Config = configs[environment];
2 changes: 2 additions & 0 deletions template/lambda-sqs-worker-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@aws-sdk/client-lambda": "^3.363.0",
"@aws-sdk/client-sns": "^3.363.0",
"@seek/logger": "^6.0.0",
"skuba-dive": "^2.0.0",
"zod": "^3.19.1"
},
"devDependencies": {
Expand All @@ -24,6 +25,7 @@
"aws-cdk": "^2.109.0",
"aws-cdk-lib": "^2.109.0",
"constructs": "^10.0.17",
"pino-pretty": "^11.0.0",
"skuba": "*"
},
"packageManager": "[email protected]",
Expand Down
16 changes: 14 additions & 2 deletions template/lambda-sqs-worker-cdk/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import createLogger from '@seek/logger';
import type { SQSEvent, SQSHandler } from 'aws-lambda';

const logger = createLogger({
name: '<%- serviceName %>',
import { config } from './config';

export const logger = createLogger({
base: {
environment: config.environment,
version: config.version,
},

level: config.logLevel,

name: config.name,

transport:
config.environment === 'local' ? { target: 'pino-pretty' } : undefined,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to declare pino-pretty as a dev dep?

});

/**
Expand Down
51 changes: 51 additions & 0 deletions template/lambda-sqs-worker-cdk/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Env } from 'skuba-dive';

interface Config {
environment: Environment;

logLevel: string;
name: string;
version: string;
}

type Environment = (typeof environments)[number];

const dev = 'dev';
const prod = 'prod';

const environments = ['local', 'test', dev, prod] as const;

const environment = Env.oneOf(environments)('ENVIRONMENT');

/* istanbul ignore next: config verification makes more sense in a smoke test */
const configs: Record<Environment, () => Omit<Config, 'environment'>> = {
local: () => ({
logLevel: 'debug',
name: '<%- serviceName %>',
version: 'local',
}),

test: () => ({
...configs.local(),

logLevel: Env.string('LOG_LEVEL', { default: 'silent' }),
version: 'test',
}),

[dev]: () => ({
...configs[prod](),

logLevel: 'debug',
}),

[prod]: () => ({
logLevel: 'info',
name: Env.string('SERVICE'),
samchungy marked this conversation as resolved.
Show resolved Hide resolved
version: Env.string('VERSION'),
}),
};

export const config: Config = {
...configs[environment](),
environment,
};