Skip to content

Commit 3f53a45

Browse files
authored
fix(lambda): use enum values for applicationLogLevel and systemLogLevel (#29904)
### Issue # (if applicable) ### Reason for this change Enumerate `ApplicationLogLevel` and `SystemLogLevel` to help with typing ### Description of changes Both fields should use the enum type for available options ### Description of how you validated changes ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7dc6d27 commit 3f53a45

File tree

8 files changed

+305
-31
lines changed

8 files changed

+305
-31
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/aws-cdk-lambda-logging-config.assets.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/aws-cdk-lambda-logging-config.template.json

+55
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,61 @@
393393
"DependsOn": [
394394
"LambdaWithLogLevelServiceRole90A45743"
395395
]
396+
},
397+
"LambdaWithLogLevelV2ServiceRoleD184382A": {
398+
"Type": "AWS::IAM::Role",
399+
"Properties": {
400+
"AssumeRolePolicyDocument": {
401+
"Statement": [
402+
{
403+
"Action": "sts:AssumeRole",
404+
"Effect": "Allow",
405+
"Principal": {
406+
"Service": "lambda.amazonaws.com"
407+
}
408+
}
409+
],
410+
"Version": "2012-10-17"
411+
},
412+
"ManagedPolicyArns": [
413+
{
414+
"Fn::Join": [
415+
"",
416+
[
417+
"arn:",
418+
{
419+
"Ref": "AWS::Partition"
420+
},
421+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
422+
]
423+
]
424+
}
425+
]
426+
}
427+
},
428+
"LambdaWithLogLevelV2D4FB10AC": {
429+
"Type": "AWS::Lambda::Function",
430+
"Properties": {
431+
"Code": {
432+
"ZipFile": "foo"
433+
},
434+
"Handler": "index.handler",
435+
"LoggingConfig": {
436+
"ApplicationLogLevel": "INFO",
437+
"LogFormat": "JSON",
438+
"SystemLogLevel": "INFO"
439+
},
440+
"Role": {
441+
"Fn::GetAtt": [
442+
"LambdaWithLogLevelV2ServiceRoleD184382A",
443+
"Arn"
444+
]
445+
},
446+
"Runtime": "nodejs18.x"
447+
},
448+
"DependsOn": [
449+
"LambdaWithLogLevelV2ServiceRoleD184382A"
450+
]
396451
}
397452
},
398453
"Parameters": {

packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/manifest.json

+13-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.js.snapshot/tree.json

+96
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.logging-config.ts

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ new Function(stack, 'LambdaWithLogLevel', {
6262
applicationLogLevel: ApplicationLogLevel.INFO,
6363
});
6464

65+
new Function(stack, 'LambdaWithLogLevelV2', {
66+
code: new InlineCode('foo'),
67+
handler: 'index.handler',
68+
runtime: Runtime.NODEJS_18_X,
69+
loggingFormat: LoggingFormat.JSON,
70+
systemLogLevelV2: SystemLogLevel.INFO,
71+
applicationLogLevelV2: ApplicationLogLevel.INFO,
72+
});
73+
6574
new integ.IntegTest(app, 'lambda-logging-config', {
6675
testCases: [stack],
6776
});

packages/aws-cdk-lib/aws-lambda/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,20 @@ as choosing the log group:
160160
```ts
161161
import { ILogGroup } from 'aws-cdk-lib/aws-logs';
162162

163-
declare const logGroup: ILogGroup;
163+
declare const logGroup: ILogGroup;
164164

165165
new lambda.Function(this, 'Lambda', {
166166
code: new lambda.InlineCode('foo'),
167167
handler: 'index.handler',
168168
runtime: lambda.Runtime.NODEJS_18_X,
169169
loggingFormat: lambda.LoggingFormat.JSON,
170-
systemLogLevel: lambda.SystemLogLevel.INFO,
171-
applicationLogLevel: lambda.ApplicationLogLevel.INFO,
170+
systemLogLevelV2: lambda.SystemLogLevel.INFO,
171+
applicationLogLevelV2: lambda.ApplicationLogLevel.INFO,
172172
logGroup: logGroup,
173173
});
174174
```
175175

176-
To use `applicationLogLevel` and/or `systemLogLevel` you must set `loggingFormat` to `LoggingFormat.JSON`.
176+
To use `applicationLogLevelV2` and/or `systemLogLevelV2` you must set `loggingFormat` to `LoggingFormat.JSON`.
177177

178178
## Resource-based Policies
179179

@@ -1077,8 +1077,8 @@ const fn = new lambda.Function(this, 'MyLambda', {
10771077

10781078
## IPv6 support
10791079

1080-
You can configure IPv6 connectivity for lambda function by setting `Ipv6AllowedForDualStack` to true.
1081-
It allows Lambda functions to specify whether the IPv6 traffic should be allowed when using dual-stack VPCs.
1080+
You can configure IPv6 connectivity for lambda function by setting `Ipv6AllowedForDualStack` to true.
1081+
It allows Lambda functions to specify whether the IPv6 traffic should be allowed when using dual-stack VPCs.
10821082
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.
10831083

10841084
```ts

packages/aws-cdk-lib/aws-lambda/lib/function.ts

+33-9
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ export interface FunctionOptions extends EventInvokeConfigOptions {
524524

525525
/**
526526
* Sets the logFormat for the function.
527+
* @deprecated Use `loggingFormat` as a property instead.
527528
* @default "Text"
528529
*/
529530
readonly logFormat?: string;
@@ -536,15 +537,29 @@ export interface FunctionOptions extends EventInvokeConfigOptions {
536537

537538
/**
538539
* Sets the application log level for the function.
540+
* @deprecated Use `applicationLogLevelV2` as a property instead.
539541
* @default "INFO"
540542
*/
541543
readonly applicationLogLevel?: string;
542544

545+
/**
546+
* Sets the application log level for the function.
547+
* @default ApplicationLogLevel.INFO
548+
*/
549+
readonly applicationLogLevelV2?: ApplicationLogLevel;
550+
543551
/**
544552
* Sets the system log level for the function.
553+
* @deprecated Use `systemLogLevelV2` as a property instead.
545554
* @default "INFO"
546555
*/
547556
readonly systemLogLevel?: string;
557+
558+
/**
559+
* Sets the system log level for the function.
560+
* @default SystemLogLevel.INFO
561+
*/
562+
readonly systemLogLevelV2?: SystemLogLevel;
548563
}
549564

550565
export interface FunctionProps extends FunctionOptions {
@@ -1151,25 +1166,34 @@ export class Function extends FunctionBase {
11511166
* function and undefined if not.
11521167
*/
11531168
private getLoggingConfig(props: FunctionProps): CfnFunction.LoggingConfigProperty | undefined {
1154-
if ((props.applicationLogLevel || props.systemLogLevel) && props.logFormat !== LogFormat.JSON
1155-
&& props.loggingFormat === undefined) {
1156-
throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LogFormat to '${LogFormat.JSON}', got '${props.logFormat}'.`);
1169+
if (props.logFormat && props.loggingFormat) {
1170+
throw new Error('Only define LogFormat or LoggingFormat, not both.');
11571171
}
11581172

1159-
if ((props.applicationLogLevel || props.systemLogLevel) && props.loggingFormat !== LoggingFormat.JSON && props.logFormat === undefined) {
1160-
throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LoggingFormat to '${LoggingFormat.JSON}', got '${props.loggingFormat}'.`);
1173+
if (props.applicationLogLevel && props.applicationLogLevelV2) {
1174+
throw new Error('Only define applicationLogLevel or applicationLogLevelV2, not both.');
11611175
}
11621176

1163-
if (props.logFormat && props.loggingFormat) {
1164-
throw new Error('Only define LogFormat or LoggingFormat, not both.');
1177+
if (props.systemLogLevel && props.systemLogLevelV2) {
1178+
throw new Error('Only define systemLogLevel or systemLogLevelV2, not both.');
1179+
}
1180+
1181+
if (props.applicationLogLevel || props.applicationLogLevelV2 || props.systemLogLevel || props.systemLogLevelV2) {
1182+
if (props.logFormat !== LoggingFormat.JSON && props.loggingFormat === undefined) {
1183+
throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LogFormat to '${LogFormat.JSON}', got '${props.logFormat}'.`);
1184+
}
1185+
1186+
if (props.loggingFormat !== LoggingFormat.JSON && props.logFormat === undefined) {
1187+
throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LoggingFormat to '${LoggingFormat.JSON}', got '${props.loggingFormat}'.`);
1188+
}
11651189
}
11661190

11671191
let loggingConfig: CfnFunction.LoggingConfigProperty;
11681192
if (props.logFormat || props.logGroup || props.loggingFormat) {
11691193
loggingConfig = {
11701194
logFormat: props.logFormat || props.loggingFormat,
1171-
systemLogLevel: props.systemLogLevel,
1172-
applicationLogLevel: props.applicationLogLevel,
1195+
systemLogLevel: props.systemLogLevel || props.systemLogLevelV2,
1196+
applicationLogLevel: props.applicationLogLevel || props.applicationLogLevelV2,
11731197
logGroup: props.logGroup?.logGroupName,
11741198
};
11751199
return loggingConfig;

0 commit comments

Comments
 (0)