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

fix(lambda): validate logLevel with logFormat for advanced logging #28045

Merged
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
2 changes: 2 additions & 0 deletions packages/aws-cdk-lib/aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions packages/aws-cdk-lib/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,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(`To use ApplicationLogLevel and/or SystemLogLevel you must set LogFormat to '${LogFormat.JSON}', got '${props.logFormat}'.`);
}

let loggingConfig: CfnFunction.LoggingConfigProperty;
if (props.logFormat || props.logGroup) {
loggingConfig = {
Expand Down
56 changes: 55 additions & 1 deletion packages/aws-cdk-lib/aws-lambda/test/logging-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/To use ApplicationLogLevel and\/or SystemLogLevel you must set LogFormat to 'JSON', got 'Text'./);
});

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(/To use ApplicationLogLevel and\/or SystemLogLevel you must set LogFormat 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');
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(/To use ApplicationLogLevel and\/or SystemLogLevel you must set LogFormat to 'JSON', got 'undefined'./);
});

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(/To use ApplicationLogLevel and\/or SystemLogLevel you must set LogFormat to 'JSON', got 'undefined'./);
});
});
Loading