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): use enum values for applicationLogLevel and systemLogLevel #29904

Merged
merged 6 commits into from
May 29, 2024
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

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.
aaythapa marked this conversation as resolved.
Show resolved Hide resolved
* @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