From f533ba2d2c00c6c1a8d143aea4b29857a6ed018a Mon Sep 17 00:00:00 2001 From: go-to-k <24818752+go-to-k@users.noreply.github.com> Date: Fri, 17 Nov 2023 15:04:02 +0900 Subject: [PATCH 1/3] chore(lambda): validate logLevel with JSON logFormat for advanced logging --- .../aws-cdk-lib/aws-lambda/lib/function.ts | 4 ++ .../aws-lambda/test/logging-config.test.ts | 56 ++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-lambda/lib/function.ts b/packages/aws-cdk-lib/aws-lambda/lib/function.ts index 5c64c7069f65e..cc461fa788607 100644 --- a/packages/aws-cdk-lib/aws-lambda/lib/function.ts +++ b/packages/aws-cdk-lib/aws-lambda/lib/function.ts @@ -1077,6 +1077,10 @@ export class Function extends FunctionBase { * function and undefined if not. */ private getLoggingConfig(props: FunctionProps): CfnFunction.LoggingConfigProperty | undefined { + if ((props.applicationLogLevel || props.systemLogLevel) && props.logFormat !== LogFormat.JSON) { + throw new Error('ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON.'); + } + let loggingConfig: CfnFunction.LoggingConfigProperty; if (props.logFormat || props.logGroup) { loggingConfig = { diff --git a/packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts b/packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts index ad4c5dd2b956b..73405aa805b9f 100644 --- a/packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts +++ b/packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts @@ -133,6 +133,60 @@ describe('logging Config', () => { logGroupName: 'customLogGroup', }), }); - }).toThrowError('CDK does not support setting logRetention and logGroup'); + }).toThrow(/CDK does not support setting logRetention and logGroup/); + }); + + test('Throws when applicationLogLevel is specified with TEXT logFormat', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'stack'); + expect(() => { + new lambda.Function(stack, 'Lambda', { + code: new lambda.InlineCode('foo'), + handler: 'index.handler', + runtime: lambda.Runtime.NODEJS_18_X, + logFormat: lambda.LogFormat.TEXT, + applicationLogLevel: lambda.ApplicationLogLevel.INFO, + }); + }).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./); + }); + + test('Throws when systemLogLevel is specified with TEXT logFormat', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'stack'); + expect(() => { + new lambda.Function(stack, 'Lambda', { + code: new lambda.InlineCode('foo'), + handler: 'index.handler', + runtime: lambda.Runtime.NODEJS_18_X, + logFormat: lambda.LogFormat.TEXT, + systemLogLevel: lambda.SystemLogLevel.INFO, + }); + }).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./); + }); + + test('Throws when applicationLogLevel is specified if logFormat is undefined', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'stack'); + expect(() => { + new lambda.Function(stack, 'Lambda', { + code: new lambda.InlineCode('foo'), + handler: 'index.handler', + runtime: lambda.Runtime.NODEJS_18_X, + applicationLogLevel: lambda.ApplicationLogLevel.INFO, + }); + }).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./); + }); + + test('Throws when systemLogLevel is specified if logFormat is undefined', () => { + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'stack'); + expect(() => { + new lambda.Function(stack, 'Lambda', { + code: new lambda.InlineCode('foo'), + handler: 'index.handler', + runtime: lambda.Runtime.NODEJS_18_X, + systemLogLevel: lambda.SystemLogLevel.INFO, + }); + }).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./); }); }); From 37d73baba15ce9e09c0ecd3908fa5ee3661e307d Mon Sep 17 00:00:00 2001 From: go-to-k <24818752+go-to-k@users.noreply.github.com> Date: Sat, 18 Nov 2023 05:12:50 +0900 Subject: [PATCH 2/3] fix by the review --- packages/aws-cdk-lib/aws-lambda/README.md | 2 ++ packages/aws-cdk-lib/aws-lambda/lib/function.ts | 2 +- .../aws-cdk-lib/aws-lambda/test/logging-config.test.ts | 8 ++++---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/aws-cdk-lib/aws-lambda/README.md b/packages/aws-cdk-lib/aws-lambda/README.md index bf78d75090b26..79d8a5484ea1a 100644 --- a/packages/aws-cdk-lib/aws-lambda/README.md +++ b/packages/aws-cdk-lib/aws-lambda/README.md @@ -173,6 +173,8 @@ new lambda.Function(this, 'Lambda', { }); ``` +To use `ApplicationLogLevel` and/or `SystemLogLevel` you must set `LogFormat` to `LogFormat.JSON`. + ## Resource-based Policies AWS Lambda supports resource-based policies for controlling access to Lambda diff --git a/packages/aws-cdk-lib/aws-lambda/lib/function.ts b/packages/aws-cdk-lib/aws-lambda/lib/function.ts index cc461fa788607..cd0f60157cedd 100644 --- a/packages/aws-cdk-lib/aws-lambda/lib/function.ts +++ b/packages/aws-cdk-lib/aws-lambda/lib/function.ts @@ -1078,7 +1078,7 @@ export class Function extends FunctionBase { */ private getLoggingConfig(props: FunctionProps): CfnFunction.LoggingConfigProperty | undefined { if ((props.applicationLogLevel || props.systemLogLevel) && props.logFormat !== LogFormat.JSON) { - throw new Error('ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON.'); + throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LogFormat to '${LogFormat.JSON}', got '${props.logFormat}'.`); } let loggingConfig: CfnFunction.LoggingConfigProperty; diff --git a/packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts b/packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts index 73405aa805b9f..c114405ed3827 100644 --- a/packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts +++ b/packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts @@ -147,7 +147,7 @@ describe('logging Config', () => { logFormat: lambda.LogFormat.TEXT, applicationLogLevel: lambda.ApplicationLogLevel.INFO, }); - }).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./); + }).toThrow(/To use ApplicationLogLevel and\/or SystemLogLevel you must set LogFormat to 'JSON', got 'Text'./); }); test('Throws when systemLogLevel is specified with TEXT logFormat', () => { @@ -161,7 +161,7 @@ describe('logging Config', () => { logFormat: lambda.LogFormat.TEXT, systemLogLevel: lambda.SystemLogLevel.INFO, }); - }).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./); + }).toThrow(/To use ApplicationLogLevel and\/or SystemLogLevel you must set LogFormat to 'JSON', got 'Text'./); }); test('Throws when applicationLogLevel is specified if logFormat is undefined', () => { @@ -174,7 +174,7 @@ describe('logging Config', () => { runtime: lambda.Runtime.NODEJS_18_X, applicationLogLevel: lambda.ApplicationLogLevel.INFO, }); - }).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./); + }).toThrow(/To use ApplicationLogLevel and\/or SystemLogLevel you must set LogFormat to 'JSON', got 'undefined'./); }); test('Throws when systemLogLevel is specified if logFormat is undefined', () => { @@ -187,6 +187,6 @@ describe('logging Config', () => { runtime: lambda.Runtime.NODEJS_18_X, systemLogLevel: lambda.SystemLogLevel.INFO, }); - }).toThrow(/ApplicationLogLevel and SystemLogLevel cannot be specified without LogFormat being set to JSON./); + }).toThrow(/To use ApplicationLogLevel and\/or SystemLogLevel you must set LogFormat to 'JSON', got 'undefined'./); }); }); From 0e45f0512bcbf5e57443e5eeb831fecd7d2e44f0 Mon Sep 17 00:00:00 2001 From: go-to-k <24818752+go-to-k@users.noreply.github.com> Date: Sat, 18 Nov 2023 05:13:28 +0900 Subject: [PATCH 3/3] change README --- packages/aws-cdk-lib/aws-lambda/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/aws-cdk-lib/aws-lambda/README.md b/packages/aws-cdk-lib/aws-lambda/README.md index 79d8a5484ea1a..41ee3f6953b69 100644 --- a/packages/aws-cdk-lib/aws-lambda/README.md +++ b/packages/aws-cdk-lib/aws-lambda/README.md @@ -173,7 +173,7 @@ new lambda.Function(this, 'Lambda', { }); ``` -To use `ApplicationLogLevel` and/or `SystemLogLevel` you must set `LogFormat` to `LogFormat.JSON`. +To use `applicationLogLevel` and/or `systemLogLevel` you must set `logFormat` to `LogFormat.JSON`. ## Resource-based Policies