Skip to content

Commit 4a09720

Browse files
authored
feat(lambda): deprecate logRetention properties in favor of logGroup (#28737)
#28039 introduced support for custom logging configurations for AWS Lambda Functions. This change deprecates the `logRetention`, `logRetentionRole` and `logRetentionRetryOptions` properties in favor of using a custom logging configuration. By default, Lambda functions send logs to an automatically created default log group named `/aws/lambda/<function name>`. However you cannot change the properties of this auto-created log group using the AWS CDK, e.g. you cannot set a different log retention. To overcome the limitation, a custom resource was introduced and configuration exposed via the `logRetention` properties. This is what we are deprecating in this change. With the introduction of custom logging configuration and the new `logGroup` property, users can now create a fully customizable `LogGroup` ahead of time, and instruct the Lambda function to send logs to it. Migrating from `logRetention` to `logGroup` will cause the name of the log group to change. Don't attempt to use the name of the auto-created log group, this will cause subtle issue. We recommend using auto-naming for lambda log groups, they can easily be accessed via the Lambda Console. If you want use a well-known name, we recommend using a pattern like `/<your service>/lambda/<function name>`. Be aware that a names log group can prevent a stack from being recreated without manual intervention after it has been deployed (error `Resource already exists`). This is because `LogGroups` are retained by default. Either way, users will have to adjust and documentation will need to be updated. Any code referencing the old log group name verbatim will have to be changed as well. Keep in mind that in AWS CDK code, you can access the log group name directly from the `LogGroup` construct: ```ts declare const myLogGroup: logs.LogGroup; myLogGroup.logGroupName; ``` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 0f2b8f8 commit 4a09720

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -988,24 +988,28 @@ See [the AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/invocat
988988

989989
## Log Group
990990

991-
Lambda functions automatically create a log group with the name `/aws/lambda/<function-name>` upon first execution with
991+
By default, Lambda functions automatically create a log group with the name `/aws/lambda/<function-name>` upon first execution with
992992
log data set to never expire.
993+
This is convenient, but prevents you from changing any of the properties of this auto-created log group using the AWS CDK.
994+
For example you cannot set log retention or assign a data protection policy.
993995

994-
The `logRetention` property can be used to set a different expiration period.
996+
To fully customize the logging behavior of your Lambda function, create a `logs.LogGroup` ahead of time and use the `logGroup` property to instruct the Lambda function to send logs to it.
997+
This way you can use the full features set supported by Amazon CloudWatch Logs.
995998

996-
It is possible to obtain the function's log group as a `logs.ILogGroup` by calling the `logGroup` property of the
997-
`Function` construct.
998-
999-
By default, CDK uses the AWS SDK retry options when creating a log group. The `logRetentionRetryOptions` property
1000-
allows you to customize the maximum number of retries and base backoff duration.
999+
```ts
1000+
import { LogGroup } from 'aws-cdk-lib/aws-logs';
10011001

1002-
*Note* that, if either `logRetention` is set or `logGroup` property is called, a [CloudFormation custom
1003-
resource](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cfn-customresource.html) is added
1004-
to the stack that pre-creates the log group as part of the stack deployment, if it already doesn't exist, and sets the
1005-
correct log retention period (never expire, by default).
1002+
const myLogGroup = new LogGroup(this, 'MyLogGroupWithLogGroupName', {
1003+
logGroupName: 'customLogGroup',
1004+
});
10061005

1007-
*Further note* that, if the log group already exists and the `logRetention` is not set, the custom resource will reset
1008-
the log retention to never expire even if it was configured with a different value.
1006+
new lambda.Function(this, 'Lambda', {
1007+
code: new lambda.InlineCode('foo'),
1008+
handler: 'index.handler',
1009+
runtime: lambda.Runtime.NODEJS_18_X,
1010+
logGroup: myLogGroup,
1011+
});
1012+
```
10091013

10101014
## FileSystem Access
10111015

@@ -1082,8 +1086,7 @@ A typical use case of this function is when a higher level construct needs to de
10821086
needs to guarantee that the function is declared once. However, a user of this higher level construct can declare it any
10831087
number of times and with different properties. Using `SingletonFunction` here with a fixed `uuid` will guarantee this.
10841088

1085-
For example, the `LogRetention` construct requires only one single lambda function for all different log groups whose
1086-
retention it seeks to manage.
1089+
For example, the `AwsCustomResource` construct requires only one single lambda function for all api calls that are made.
10871090

10881091
## Bundling Asset Code
10891092

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,15 @@ export interface FunctionOptions extends EventInvokeConfigOptions {
385385
* remove the retention policy, set the value to `INFINITE`.
386386
*
387387
* @default logs.RetentionDays.INFINITE
388+
*
389+
* @deprecated instead create a fully customizable log group with `logs.LogGroup` and use the `logGroup` property to instruct the Lambda function to send logs to it.
390+
* Migrating from `logRetention` to `logGroup` will cause the name of the log group to change.
391+
* Users and code and referencing the name verbatim will have to adjust.\
392+
* In AWS CDK code, you can access the log group name directly from the LogGroup construct:
393+
* ```ts
394+
* declare const myLogGroup: logs.LogGroup;
395+
* myLogGroup.logGroupName;
396+
* ```
388397
*/
389398
readonly logRetention?: logs.RetentionDays;
390399

@@ -393,6 +402,8 @@ export interface FunctionOptions extends EventInvokeConfigOptions {
393402
* that sets the retention policy.
394403
*
395404
* @default - A new role is created.
405+
*
406+
* @deprecated instead use `logGroup` to create a fully customizable log group and instruct the Lambda function to send logs to it.
396407
*/
397408
readonly logRetentionRole?: iam.IRole;
398409

@@ -401,6 +412,8 @@ export interface FunctionOptions extends EventInvokeConfigOptions {
401412
* These options control the retry policy when interacting with CloudWatch APIs.
402413
*
403414
* @default - Default AWS SDK retry options.
415+
*
416+
* @deprecated instead use `logGroup` to create a fully customizable log group and instruct the Lambda function to send logs to it.
404417
*/
405418
readonly logRetentionRetryOptions?: LogRetentionRetryOptions;
406419

@@ -461,26 +474,32 @@ export interface FunctionOptions extends EventInvokeConfigOptions {
461474
readonly runtimeManagementMode?: RuntimeManagementMode;
462475

463476
/**
464-
* Sets the log group name for the function.
465-
* @default `/aws/lambda/${this.functionName}` default log group name created by Lambda
477+
* The log group the function sends logs to.
478+
*
479+
* By default, Lambda functions send logs to an automatically created default log group named /aws/lambda/<function name>.
480+
* However you cannot change the properties of this auto-created log group using the AWS CDK, e.g. you cannot set a different log retention.
481+
*
482+
* Use the `logGroup` property to create a fully customizable LogGroup ahead of time, and instruct the Lambda function to send logs to it.
483+
*
484+
* @default `/aws/lambda/${this.functionName}` - default log group created by Lambda
466485
*/
467486
readonly logGroup?: logs.ILogGroup;
468487

469488
/**
470489
* Sets the logFormat for the function.
471-
* @default Text format
490+
* @default "Text"
472491
*/
473492
readonly logFormat?: string;
474493

475494
/**
476495
* Sets the application log level for the function.
477-
* @default INFO
496+
* @default "INFO"
478497
*/
479498
readonly applicationLogLevel?: string;
480499

481500
/**
482501
* Sets the system log level for the function.
483-
* @default INFO
502+
* @default "INFO"
484503
*/
485504
readonly systemLogLevel?: string;
486505
}

0 commit comments

Comments
 (0)