Skip to content

Commit

Permalink
fix(lambda): use enum values for applicationLogLevel and systemLogLev…
Browse files Browse the repository at this point in the history
…el (aws#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*
  • Loading branch information
dali546 authored and atanaspam committed Jun 3, 2024
1 parent 0f6c8b8 commit 699c83c
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 31 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,61 @@
"DependsOn": [
"LambdaWithLogLevelServiceRole90A45743"
]
},
"LambdaWithLogLevelV2ServiceRoleD184382A": {
"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"
]
]
}
]
}
},
"LambdaWithLogLevelV2D4FB10AC": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "foo"
},
"Handler": "index.handler",
"LoggingConfig": {
"ApplicationLogLevel": "INFO",
"LogFormat": "JSON",
"SystemLogLevel": "INFO"
},
"Role": {
"Fn::GetAtt": [
"LambdaWithLogLevelV2ServiceRoleD184382A",
"Arn"
]
},
"Runtime": "nodejs18.x"
},
"DependsOn": [
"LambdaWithLogLevelV2ServiceRoleD184382A"
]
}
},
"Parameters": {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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],
});
Expand Down
12 changes: 6 additions & 6 deletions packages/aws-cdk-lib/aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
42 changes: 33 additions & 9 deletions packages/aws-cdk-lib/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ export interface FunctionOptions extends EventInvokeConfigOptions {

/**
* Sets the logFormat for the function.
* @deprecated Use `loggingFormat` as a property instead.
* @default "Text"
*/
readonly logFormat?: string;
Expand All @@ -536,15 +537,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?: 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?: string;

/**
* Sets the system log level for the function.
* @default SystemLogLevel.INFO
*/
readonly systemLogLevelV2?: SystemLogLevel;
}

export interface FunctionProps extends FunctionOptions {
Expand Down Expand Up @@ -1151,25 +1166,34 @@ 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) {
throw new Error(`To use ApplicationLogLevel and/or SystemLogLevel you must set LogFormat to '${LogFormat.JSON}', got '${props.logFormat}'.`);
if (props.logFormat && props.loggingFormat) {
throw new Error('Only define LogFormat or LoggingFormat, not both.');
}

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.applicationLogLevel && props.applicationLogLevelV2) {
throw new Error('Only define applicationLogLevel or applicationLogLevelV2, not both.');
}

if (props.logFormat && props.loggingFormat) {
throw new Error('Only define LogFormat or LoggingFormat, not both.');
if (props.systemLogLevel && props.systemLogLevelV2) {
throw new Error('Only define systemLogLevel or systemLogLevelV2, not both.');
}

if (props.applicationLogLevel || props.applicationLogLevelV2 || props.systemLogLevel || props.systemLogLevelV2) {
if (props.logFormat !== LoggingFormat.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.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}'.`);
}
}

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;
Expand Down
Loading

0 comments on commit 699c83c

Please sign in to comment.