From 5142325030d876cbc31665b14cd4d69570b6d5c4 Mon Sep 17 00:00:00 2001 From: Connor Robertson Date: Wed, 31 Jan 2024 14:58:36 -0800 Subject: [PATCH] feat(lambda): support enums in loggingFormat (#28942) > # Issue > > The issue is that LogFormat is a String so it doesn't allow the enum LogFormat. > # Solution > > Created a new enum for the LoggingFormat and added testing. So the solution sets these values as potential environment variables. The main difference is LoggingFormat is assigned to an enum instead of a string. > # Important Design Decisions > > This is so that an enum could be used for LoggingFormat without breaking JSII target languages. Some background information is in this pr https://github.com/aws/aws-cdk/pull/28127. Was a recommended solution here. https://github.com/aws/aws-cdk/pull/28127 > > Remember to follow the [CONTRIBUTING GUIDE] and [DESIGN GUIDELINES] for any > code you submit. > > [CONTRIBUTING GUIDE]: https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md > [DESIGN GUIDELINES]: https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md Closes https://github.com/aws/aws-cdk/issues/28114. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../aws-cdk-lambda-logging-config.assets.json | 6 +- ...ws-cdk-lambda-logging-config.template.json | 106 ++++++++++ .../integ.logging-config.js.snapshot/cdk.out | 2 +- .../integ.json | 2 +- ...efaultTestDeployAssert24012B25.assets.json | 2 +- .../manifest.json | 28 ++- .../tree.json | 192 +++++++++++++++++- .../aws-lambda/test/integ.logging-config.ts | 16 +- packages/aws-cdk-lib/aws-lambda/README.md | 4 +- .../aws-cdk-lib/aws-lambda/lib/function.ts | 39 +++- .../aws-lambda/test/logging-config.test.ts | 68 +++++++ 11 files changed, 449 insertions(+), 16 deletions(-) diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/aws-cdk-lambda-logging-config.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/aws-cdk-lambda-logging-config.assets.json index cc242bdce487c..7c3b04d5c455e 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/aws-cdk-lambda-logging-config.assets.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/aws-cdk-lambda-logging-config.assets.json @@ -1,7 +1,7 @@ { - "version": "34.0.0", + "version": "36.0.0", "files": { - "c10a26dd32e7f7d2a9c057ec71b82b23ad867563a9b092f09ed73ef671035641": { + "04a0e9e899cf60a12349bd536e5e4764854e9e0c2e4bf28caf29641d28a1aabc": { "source": { "path": "aws-cdk-lambda-logging-config.template.json", "packaging": "file" @@ -9,7 +9,7 @@ "destinations": { "current_account-current_region": { "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}", - "objectKey": "c10a26dd32e7f7d2a9c057ec71b82b23ad867563a9b092f09ed73ef671035641.json", + "objectKey": "04a0e9e899cf60a12349bd536e5e4764854e9e0c2e4bf28caf29641d28a1aabc.json", "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}" } } diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/aws-cdk-lambda-logging-config.template.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/aws-cdk-lambda-logging-config.template.json index e121f281cd843..66aaf9cdff899 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/aws-cdk-lambda-logging-config.template.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/aws-cdk-lambda-logging-config.template.json @@ -233,6 +233,112 @@ "LambdaWithJSONFormatServiceRole37FFB486" ] }, + "LambdaWithTextLoggingFormatServiceRole3ADC91E1": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "LambdaWithTextLoggingFormatD614C37E": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "ZipFile": "foo" + }, + "Handler": "index.handler", + "LoggingConfig": { + "LogFormat": "Text" + }, + "Role": { + "Fn::GetAtt": [ + "LambdaWithTextLoggingFormatServiceRole3ADC91E1", + "Arn" + ] + }, + "Runtime": "nodejs18.x" + }, + "DependsOn": [ + "LambdaWithTextLoggingFormatServiceRole3ADC91E1" + ] + }, + "LambdaWithJSONLoggingFormatServiceRole0B3D5672": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "ManagedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "LambdaWithJSONLoggingFormatFA0D6903": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "ZipFile": "foo" + }, + "Handler": "index.handler", + "LoggingConfig": { + "LogFormat": "JSON" + }, + "Role": { + "Fn::GetAtt": [ + "LambdaWithJSONLoggingFormatServiceRole0B3D5672", + "Arn" + ] + }, + "Runtime": "nodejs18.x" + }, + "DependsOn": [ + "LambdaWithJSONLoggingFormatServiceRole0B3D5672" + ] + }, "LambdaWithLogLevelServiceRole90A45743": { "Type": "AWS::IAM::Role", "Properties": { diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/cdk.out b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/cdk.out index 2313ab5436501..1f0068d32659a 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/cdk.out +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/cdk.out @@ -1 +1 @@ -{"version":"34.0.0"} \ No newline at end of file +{"version":"36.0.0"} \ No newline at end of file diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/integ.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/integ.json index e5feef0abe915..5903d2efa9cc4 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/integ.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/integ.json @@ -1,5 +1,5 @@ { - "version": "34.0.0", + "version": "36.0.0", "testCases": { "lambda-logging-config/DefaultTest": { "stacks": [ diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/lambdaloggingconfigDefaultTestDeployAssert24012B25.assets.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/lambdaloggingconfigDefaultTestDeployAssert24012B25.assets.json index a8e852ceaf06f..609fe34ff0fd9 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/lambdaloggingconfigDefaultTestDeployAssert24012B25.assets.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/lambdaloggingconfigDefaultTestDeployAssert24012B25.assets.json @@ -1,5 +1,5 @@ { - "version": "34.0.0", + "version": "36.0.0", "files": { "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": { "source": { diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/manifest.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/manifest.json index 8feb2b46a85b2..a9452713ec2e1 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/manifest.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/manifest.json @@ -1,5 +1,5 @@ { - "version": "34.0.0", + "version": "36.0.0", "artifacts": { "aws-cdk-lambda-logging-config.assets": { "type": "cdk:asset-manifest", @@ -18,7 +18,7 @@ "validateOnSynth": false, "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-deploy-role-${AWS::AccountId}-${AWS::Region}", "cloudFormationExecutionRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-cfn-exec-role-${AWS::AccountId}-${AWS::Region}", - "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/c10a26dd32e7f7d2a9c057ec71b82b23ad867563a9b092f09ed73ef671035641.json", + "stackTemplateAssetObjectUrl": "s3://cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}/04a0e9e899cf60a12349bd536e5e4764854e9e0c2e4bf28caf29641d28a1aabc.json", "requiresBootstrapStackVersion": 6, "bootstrapStackVersionSsmParameter": "/cdk-bootstrap/hnb659fds/version", "additionalDependencies": [ @@ -94,6 +94,30 @@ "data": "LambdaWithJSONFormat20E01C00" } ], + "/aws-cdk-lambda-logging-config/LambdaWithTextLoggingFormat/ServiceRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "LambdaWithTextLoggingFormatServiceRole3ADC91E1" + } + ], + "/aws-cdk-lambda-logging-config/LambdaWithTextLoggingFormat/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "LambdaWithTextLoggingFormatD614C37E" + } + ], + "/aws-cdk-lambda-logging-config/LambdaWithJSONLoggingFormat/ServiceRole/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "LambdaWithJSONLoggingFormatServiceRole0B3D5672" + } + ], + "/aws-cdk-lambda-logging-config/LambdaWithJSONLoggingFormat/Resource": [ + { + "type": "aws:cdk:logicalId", + "data": "LambdaWithJSONLoggingFormatFA0D6903" + } + ], "/aws-cdk-lambda-logging-config/LambdaWithLogLevel/ServiceRole/Resource": [ { "type": "aws:cdk:logicalId", diff --git a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/tree.json b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/tree.json index 3807537378927..92e1464903af9 100644 --- a/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/tree.json +++ b/packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/tree.json @@ -437,6 +437,194 @@ "version": "0.0.0" } }, + "LambdaWithTextLoggingFormat": { + "id": "LambdaWithTextLoggingFormat", + "path": "aws-cdk-lambda-logging-config/LambdaWithTextLoggingFormat", + "children": { + "ServiceRole": { + "id": "ServiceRole", + "path": "aws-cdk-lambda-logging-config/LambdaWithTextLoggingFormat/ServiceRole", + "children": { + "ImportServiceRole": { + "id": "ImportServiceRole", + "path": "aws-cdk-lambda-logging-config/LambdaWithTextLoggingFormat/ServiceRole/ImportServiceRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-cdk-lambda-logging-config/LambdaWithTextLoggingFormat/ServiceRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "managedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Role", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-cdk-lambda-logging-config/LambdaWithTextLoggingFormat/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Function", + "aws:cdk:cloudformation:props": { + "code": { + "zipFile": "foo" + }, + "handler": "index.handler", + "loggingConfig": { + "logFormat": "Text" + }, + "role": { + "Fn::GetAtt": [ + "LambdaWithTextLoggingFormatServiceRole3ADC91E1", + "Arn" + ] + }, + "runtime": "nodejs18.x" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnFunction", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.Function", + "version": "0.0.0" + } + }, + "LambdaWithJSONLoggingFormat": { + "id": "LambdaWithJSONLoggingFormat", + "path": "aws-cdk-lambda-logging-config/LambdaWithJSONLoggingFormat", + "children": { + "ServiceRole": { + "id": "ServiceRole", + "path": "aws-cdk-lambda-logging-config/LambdaWithJSONLoggingFormat/ServiceRole", + "children": { + "ImportServiceRole": { + "id": "ImportServiceRole", + "path": "aws-cdk-lambda-logging-config/LambdaWithJSONLoggingFormat/ServiceRole/ImportServiceRole", + "constructInfo": { + "fqn": "aws-cdk-lib.Resource", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-cdk-lambda-logging-config/LambdaWithJSONLoggingFormat/ServiceRole/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::IAM::Role", + "aws:cdk:cloudformation:props": { + "assumeRolePolicyDocument": { + "Statement": [ + { + "Action": "sts:AssumeRole", + "Effect": "Allow", + "Principal": { + "Service": "lambda.amazonaws.com" + } + } + ], + "Version": "2012-10-17" + }, + "managedPolicyArns": [ + { + "Fn::Join": [ + "", + [ + "arn:", + { + "Ref": "AWS::Partition" + }, + ":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" + ] + ] + } + ] + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.CfnRole", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_iam.Role", + "version": "0.0.0" + } + }, + "Resource": { + "id": "Resource", + "path": "aws-cdk-lambda-logging-config/LambdaWithJSONLoggingFormat/Resource", + "attributes": { + "aws:cdk:cloudformation:type": "AWS::Lambda::Function", + "aws:cdk:cloudformation:props": { + "code": { + "zipFile": "foo" + }, + "handler": "index.handler", + "loggingConfig": { + "logFormat": "JSON" + }, + "role": { + "Fn::GetAtt": [ + "LambdaWithJSONLoggingFormatServiceRole0B3D5672", + "Arn" + ] + }, + "runtime": "nodejs18.x" + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.CfnFunction", + "version": "0.0.0" + } + } + }, + "constructInfo": { + "fqn": "aws-cdk-lib.aws_lambda.Function", + "version": "0.0.0" + } + }, "LambdaWithLogLevel": { "id": "LambdaWithLogLevel", "path": "aws-cdk-lambda-logging-config/LambdaWithLogLevel", @@ -568,7 +756,7 @@ "path": "lambda-logging-config/DefaultTest/Default", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.2.70" + "version": "10.3.0" } }, "DeployAssert": { @@ -614,7 +802,7 @@ "path": "Tree", "constructInfo": { "fqn": "constructs.Construct", - "version": "10.2.70" + "version": "10.3.0" } } }, 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 7029acfe3a94c..5488727ca8413 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 @@ -1,7 +1,7 @@ import { App, Stack } from 'aws-cdk-lib'; import * as logs from 'aws-cdk-lib/aws-logs'; import * as integ from '@aws-cdk/integ-tests-alpha'; -import { Function, InlineCode, Runtime, LogFormat, SystemLogLevel, ApplicationLogLevel } from 'aws-cdk-lib/aws-lambda'; +import { Function, InlineCode, Runtime, LogFormat, LoggingFormat, SystemLogLevel, ApplicationLogLevel } from 'aws-cdk-lib/aws-lambda'; const app = new App(); @@ -39,6 +39,20 @@ new Function(stack, 'LambdaWithJSONFormat', { logFormat: LogFormat.JSON, }); +new Function(stack, 'LambdaWithTextLoggingFormat', { + code: new InlineCode('foo'), + handler: 'index.handler', + runtime: Runtime.NODEJS_18_X, + logFormat: LoggingFormat.TEXT, +}); + +new Function(stack, 'LambdaWithJSONLoggingFormat', { + code: new InlineCode('foo'), + handler: 'index.handler', + runtime: Runtime.NODEJS_18_X, + logFormat: LoggingFormat.JSON, +}); + new Function(stack, 'LambdaWithLogLevel', { code: new InlineCode('foo'), handler: 'index.handler', diff --git a/packages/aws-cdk-lib/aws-lambda/README.md b/packages/aws-cdk-lib/aws-lambda/README.md index 8358e353ab422..13b3c319ec7ff 100644 --- a/packages/aws-cdk-lib/aws-lambda/README.md +++ b/packages/aws-cdk-lib/aws-lambda/README.md @@ -166,14 +166,14 @@ new lambda.Function(this, 'Lambda', { code: new lambda.InlineCode('foo'), handler: 'index.handler', runtime: lambda.Runtime.NODEJS_18_X, - logFormat: lambda.LogFormat.JSON, + loggingFormat: lambda.LoggingFormat.JSON, systemLogLevel: lambda.SystemLogLevel.INFO, applicationLogLevel: lambda.ApplicationLogLevel.INFO, logGroup: logGroup, }); ``` -To use `applicationLogLevel` and/or `systemLogLevel` you must set `logFormat` to `LogFormat.JSON`. +To use `applicationLogLevel` and/or `systemLogLevel` you must set `loggingFormat` to `LoggingFormat.JSON`. ## Resource-based Policies diff --git a/packages/aws-cdk-lib/aws-lambda/lib/function.ts b/packages/aws-cdk-lib/aws-lambda/lib/function.ts index b887017d65e4d..8dd0add920921 100644 --- a/packages/aws-cdk-lib/aws-lambda/lib/function.ts +++ b/packages/aws-cdk-lib/aws-lambda/lib/function.ts @@ -122,6 +122,24 @@ export enum LogFormat { JSON = 'JSON', } +/** + * This field takes in 2 values either Text or JSON. By setting this value to Text, + * will result in the current structure of logs format, whereas, by setting this value to JSON, + * Lambda will print the logs as Structured JSON Logs, with the corresponding timestamp and log level + * of each event. Selecting ‘JSON’ format will only allow customer’s to have different log level + * Application log level and the System log level. + */ +export enum LoggingFormat { + /** + * Lambda Logs text format. + */ + TEXT = 'Text', + /** + * Lambda structured logging in Json format. + */ + JSON = 'JSON', +} + /** * Non runtime options */ @@ -501,6 +519,12 @@ export interface FunctionOptions extends EventInvokeConfigOptions { */ readonly logFormat?: string; + /** + * Sets the loggingFormat for the function. + * @default Text format + */ + readonly loggingFormat?: LoggingFormat; + /** * Sets the application log level for the function. * @default "INFO" @@ -1118,14 +1142,23 @@ 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) { + if ((props.applicationLogLevel || props.systemLogLevel) && 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) { + throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LoggingFormat to '${LoggingFormat.JSON}', got '${props.loggingFormat}'.`); + } + + if (props.logFormat && props.loggingFormat) { + throw new Error('Only define LogFormat or LoggingFormat, not both.'); + } + let loggingConfig: CfnFunction.LoggingConfigProperty; - if (props.logFormat || props.logGroup) { + if (props.logFormat || props.logGroup || props.loggingFormat) { loggingConfig = { - logFormat: props.logFormat, + logFormat: props.logFormat || props.loggingFormat, systemLogLevel: props.systemLogLevel, applicationLogLevel: props.applicationLogLevel, logGroup: props.logGroup?.logGroupName, 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 c114405ed3827..7c8f0e861c850 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 @@ -24,6 +24,7 @@ describe('logging Config', () => { }, }); }); + test('Logging Config with LogGroup', () => { // GIVEN const app = new cdk.App(); @@ -46,6 +47,7 @@ describe('logging Config', () => { }, }); }); + test('Logging Config TEXT logFormat', () => { // GIVEN const app = new cdk.App(); @@ -63,6 +65,7 @@ describe('logging Config', () => { }, }); }); + test('Logging Config JSON logFormat', () => { // GIVEN const app = new cdk.App(); @@ -80,6 +83,43 @@ describe('logging Config', () => { }, }); }); + + test('Logging Config TEXT loggingFormat', () => { + // 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, + loggingFormat: lambda.LoggingFormat.TEXT, + }); + // WHEN + Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', { + LoggingConfig: { + LogFormat: 'Text', + }, + }); + }); + + test('Logging Config JSON loggingFormat', () => { + // 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, + loggingFormat: lambda.LoggingFormat.JSON, + }); + // WHEN + Template.fromStack(stack).hasResourceProperties('AWS::Lambda::Function', { + LoggingConfig: { + LogFormat: 'JSON', + }, + }); + }); + test('Logging Config with LogLevel set', () => { // GIVEN const app = new cdk.App(); @@ -164,6 +204,20 @@ describe('logging Config', () => { }).toThrow(/To use ApplicationLogLevel and\/or SystemLogLevel you must set LogFormat to 'JSON', got 'Text'./); }); + test('Throws when applicationLogLevel is specified with TEXT loggingFormat', () => { + 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.TEXT, + applicationLogLevel: lambda.ApplicationLogLevel.INFO, + }); + }).toThrow(/To use ApplicationLogLevel and\/or SystemLogLevel you must set LoggingFormat to 'JSON', got 'Text'./); + }); + test('Throws when applicationLogLevel is specified if logFormat is undefined', () => { const app = new cdk.App(); const stack = new cdk.Stack(app, 'stack'); @@ -190,3 +244,17 @@ describe('logging Config', () => { }).toThrow(/To use ApplicationLogLevel and\/or SystemLogLevel you must set LogFormat to 'JSON', got 'undefined'./); }); }); + +test('Throws when loggingFormat and logFormat 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, + logFormat: lambda.LogFormat.TEXT, + }); + }).toThrow(/Only define LogFormat or LoggingFormat, not both./); +});