From fd01acb23d5b6be6c40d3545968dcffc24219c2d Mon Sep 17 00:00:00 2001 From: dali546 <35352237+dali546@users.noreply.github.com> Date: Wed, 24 Apr 2024 21:05:40 +0000 Subject: [PATCH] chore: deprecate applicationLogLevel and systemLogLevel --- .../aws-lambda/test/integ.logging-config.ts | 9 ++++ packages/aws-cdk-lib/aws-lambda/README.md | 12 ++--- .../aws-cdk-lib/aws-lambda/lib/function.ts | 38 +++++++++++--- .../aws-lambda/test/logging-config.test.ts | 52 +++++++++++++++++++ 4 files changed, 98 insertions(+), 13 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.ts b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.ts index 5488727ca8413..76454daa651ce 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.ts +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.ts @@ -62,6 +62,15 @@ new Function(stack, 'LambdaWithLogLevel', { applicationLogLevel: ApplicationLogLevel.INFO, }); +new Function(stack, 'LambdaWithLogLevelV2', { + code: new InlineCode('foo'), + handler: 'index.handler', + runtime: Runtime.NODEJS_18_X, + loggingFormat: LoggingFormat.JSON, + systemLogLevelV2: SystemLogLevel.INFO, + applicationLogLevelV2: ApplicationLogLevel.INFO, +}); + new integ.IntegTest(app, 'lambda-logging-config', { testCases: [stack], }); diff --git a/packages/aws-cdk-lib/aws-lambda/README.md b/packages/aws-cdk-lib/aws-lambda/README.md index 5a876c8ae68eb..bcf108b51d1ef 100644 --- a/packages/aws-cdk-lib/aws-lambda/README.md +++ b/packages/aws-cdk-lib/aws-lambda/README.md @@ -160,20 +160,20 @@ as choosing the log group: ```ts import { ILogGroup } from 'aws-cdk-lib/aws-logs'; -declare const logGroup: ILogGroup; +declare const logGroup: ILogGroup; new lambda.Function(this, 'Lambda', { code: new lambda.InlineCode('foo'), handler: 'index.handler', runtime: lambda.Runtime.NODEJS_18_X, loggingFormat: lambda.LoggingFormat.JSON, - systemLogLevel: lambda.SystemLogLevel.INFO, - applicationLogLevel: lambda.ApplicationLogLevel.INFO, + systemLogLevelV2: lambda.SystemLogLevel.INFO, + applicationLogLevelV2: lambda.ApplicationLogLevel.INFO, logGroup: logGroup, }); ``` -To use `applicationLogLevel` and/or `systemLogLevel` you must set `loggingFormat` to `LoggingFormat.JSON`. +To use `applicationLogLevelV2` and/or `systemLogLevelV2` you must set `loggingFormat` to `LoggingFormat.JSON`. ## Resource-based Policies @@ -1077,8 +1077,8 @@ const fn = new lambda.Function(this, 'MyLambda', { ## IPv6 support -You can configure IPv6 connectivity for lambda function by setting `Ipv6AllowedForDualStack` to true. -It allows Lambda functions to specify whether the IPv6 traffic should be allowed when using dual-stack VPCs. +You can configure IPv6 connectivity for lambda function by setting `Ipv6AllowedForDualStack` to true. +It allows Lambda functions to specify whether the IPv6 traffic should be allowed when using dual-stack VPCs. To access IPv6 network using Lambda, Dual-stack VPC is required. Using dual-stack VPC a function communicates with subnet over either of IPv4 or IPv6. ```ts diff --git a/packages/aws-cdk-lib/aws-lambda/lib/function.ts b/packages/aws-cdk-lib/aws-lambda/lib/function.ts index fc13c27394208..14d86fb3ed658 100644 --- a/packages/aws-cdk-lib/aws-lambda/lib/function.ts +++ b/packages/aws-cdk-lib/aws-lambda/lib/function.ts @@ -536,15 +536,29 @@ export interface FunctionOptions extends EventInvokeConfigOptions { /** * Sets the application log level for the function. + * @deprecated Use `applicationLogLevelV2` as a property instead. * @default "INFO" */ - readonly applicationLogLevel?: ApplicationLogLevel; + readonly applicationLogLevel?: string; + + /** + * Sets the application log level for the function. + * @default ApplicationLogLevel.INFO + */ + readonly applicationLogLevelV2?: ApplicationLogLevel; /** * Sets the system log level for the function. + * @deprecated Use `systemLogLevelV2` as a property instead. * @default "INFO" */ - readonly systemLogLevel?: SystemLogLevel; + readonly systemLogLevel?: string; + + /** + * Sets the system log level for the function. + * @default SystemLogLevel.INFO + */ + readonly systemLogLevelV2?: SystemLogLevel; } export interface FunctionProps extends FunctionOptions { @@ -1162,12 +1176,14 @@ 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 - && props.loggingFormat === undefined) { + if ((props.applicationLogLevel || props.applicationLogLevelV2 || props.systemLogLevel || props.systemLogLevelV2) + && props.logFormat !== LogFormat.JSON + && props.loggingFormat === undefined) { throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LogFormat to '${LogFormat.JSON}', got '${props.logFormat}'.`); } - if ((props.applicationLogLevel || props.systemLogLevel) && props.loggingFormat !== LoggingFormat.JSON && props.logFormat === undefined) { + if ((props.applicationLogLevel || props.applicationLogLevelV2 || props.systemLogLevel || props.systemLogLevelV2) + && props.loggingFormat !== LoggingFormat.JSON && props.logFormat === undefined) { throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LoggingFormat to '${LoggingFormat.JSON}', got '${props.loggingFormat}'.`); } @@ -1175,12 +1191,20 @@ export class Function extends FunctionBase { throw new Error('Only define LogFormat or LoggingFormat, not both.'); } + if (props.applicationLogLevel && props.applicationLogLevelV2) { + throw new Error('Only define applicationLogLevel or applicationLogLevelV2, not both.'); + } + + if (props.systemLogLevel && props.systemLogLevelV2) { + throw new Error('Only define systemLogLevel or systemLogLevelV2, not both.'); + } + let loggingConfig: CfnFunction.LoggingConfigProperty; if (props.logFormat || props.logGroup || props.loggingFormat) { loggingConfig = { logFormat: props.logFormat || props.loggingFormat, - systemLogLevel: props.systemLogLevel, - applicationLogLevel: props.applicationLogLevel, + systemLogLevel: props.systemLogLevel || props.systemLogLevelV2, + applicationLogLevel: props.applicationLogLevel || props.applicationLogLevelV2, logGroup: props.logGroup?.logGroupName, }; return 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 7c8f0e861c850..a26640efe517f 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 @@ -142,6 +142,28 @@ describe('logging Config', () => { }); }); + test('Logging Config with LogLevel set with enum keys', () => { + // GIVEN + const app = new cdk.App(); + const stack = new cdk.Stack(app, 'stack'); + new lambda.Function(stack, 'Lambda', { + code: new lambda.InlineCode('foo'), + handler: 'index.handler', + runtime: lambda.Runtime.NODEJS_18_X, + logFormat: lambda.LogFormat.JSON, + systemLogLevelV2: lambda.SystemLogLevel.INFO, + applicationLogLevelV2: lambda.ApplicationLogLevel.INFO, + }); + // WHEN + Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', { + LoggingConfig: { + LogFormat: 'JSON', + SystemLogLevel: 'INFO', + ApplicationLogLevel: 'INFO', + }, + }); + }); + test('Get function custom logGroup', () => { // GIVEN const app = new cdk.App(); @@ -258,3 +280,33 @@ test('Throws when loggingFormat and logFormat are both specified', () => { }); }).toThrow(/Only define LogFormat or LoggingFormat, not both./); }); + +test('Throws when applicationLogLevel and applicationLogLevelV2 are both specified', () => { + 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, + loggingFormat: lambda.LoggingFormat.JSON, + applicationLogLevel: lambda.ApplicationLogLevel.INFO, + applicationLogLevelV2: lambda.ApplicationLogLevel.WARN, + }); + }).toThrow(/Only define applicationLogLevel or applicationLogLevelV2, not both./); +}); + +test('Throws when systemLogLevel and systemLogLevelV2 are both specified', () => { + 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, + loggingFormat: lambda.LoggingFormat.JSON, + systemLogLevel: lambda.SystemLogLevel.INFO, + systemLogLevelV2: lambda.SystemLogLevel.WARN, + }); + }).toThrow(/Only define systemLogLevel or systemLogLevelV2, not both./); +});