Skip to content

Commit 0388483

Browse files
authored
feat(lambda): function log removal policy (#34723)
### Issue # (if applicable) Closes #34669 ### Reason for this change Support configuring function log removal policy ### Description of changes Expose log group removal policy to function props ### Describe any new or updated permissions being added ### Description of how you validated changes Unit + Integ ### 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 68a00ce commit 0388483

File tree

11 files changed

+215
-16
lines changed

11 files changed

+215
-16
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.log-retention.js.snapshot/LambdaLogRetentionIntegDefaultTestDeployAssert90E53934.assets.json

Lines changed: 1 addition & 1 deletion
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.log-retention.js.snapshot/aws-cdk-lambda-log-retention.assets.json

Lines changed: 3 additions & 3 deletions
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.log-retention.js.snapshot/aws-cdk-lambda-log-retention.template.json

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
]
7171
]
7272
},
73-
"RetentionInDays": 7
73+
"RetentionInDays": 7,
74+
"RemovalPolicy": "destroy"
7475
}
7576
},
7677
"LogRetentionaae0aa3c5b4d4f87b02d85b201efdd8aServiceRole9741ECFB": {
@@ -116,6 +117,84 @@
116117
],
117118
"Effect": "Allow",
118119
"Resource": "*"
120+
},
121+
{
122+
"Action": "logs:DeleteLogGroup",
123+
"Effect": "Allow",
124+
"Resource": [
125+
{
126+
"Fn::Join": [
127+
"",
128+
[
129+
"arn:",
130+
{
131+
"Ref": "AWS::Partition"
132+
},
133+
":logs:",
134+
{
135+
"Ref": "AWS::Region"
136+
},
137+
":",
138+
{
139+
"Ref": "AWS::AccountId"
140+
},
141+
":log-group:/aws/lambda/",
142+
{
143+
"Ref": "OneMonth64E966BF"
144+
},
145+
":*"
146+
]
147+
]
148+
},
149+
{
150+
"Fn::Join": [
151+
"",
152+
[
153+
"arn:",
154+
{
155+
"Ref": "AWS::Partition"
156+
},
157+
":logs:",
158+
{
159+
"Ref": "AWS::Region"
160+
},
161+
":",
162+
{
163+
"Ref": "AWS::AccountId"
164+
},
165+
":log-group:/aws/lambda/",
166+
{
167+
"Ref": "OneWeekFE56F6A4"
168+
},
169+
":*"
170+
]
171+
]
172+
},
173+
{
174+
"Fn::Join": [
175+
"",
176+
[
177+
"arn:",
178+
{
179+
"Ref": "AWS::Partition"
180+
},
181+
":logs:",
182+
{
183+
"Ref": "AWS::Region"
184+
},
185+
":",
186+
{
187+
"Ref": "AWS::AccountId"
188+
},
189+
":log-group:/aws/lambda/",
190+
{
191+
"Ref": "OneYearA82EBDA9"
192+
},
193+
":*"
194+
]
195+
]
196+
}
197+
]
119198
}
120199
],
121200
"Version": "2012-10-17"
@@ -230,7 +309,8 @@
230309
]
231310
]
232311
},
233-
"RetentionInDays": 30
312+
"RetentionInDays": 30,
313+
"RemovalPolicy": "destroy"
234314
}
235315
},
236316
"OneYearServiceRole24D47762": {
@@ -303,7 +383,8 @@
303383
]
304384
]
305385
},
306-
"RetentionInDays": 365
386+
"RetentionInDays": 365,
387+
"RemovalPolicy": "destroy"
307388
}
308389
}
309390
},

packages/@aws-cdk-testing/framework-integ/test/aws-lambda/test/integ.log-retention.js.snapshot/cdk.out

Lines changed: 1 addition & 1 deletion
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.log-retention.js.snapshot/integ.json

Lines changed: 2 additions & 2 deletions
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.log-retention.js.snapshot/manifest.json

Lines changed: 51 additions & 3 deletions
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.log-retention.js.snapshot/tree.json

Lines changed: 1 addition & 1 deletion
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.log-retention.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@ new lambda.Function(stack, 'OneWeek', {
1313
handler: 'index.handler',
1414
runtime: STANDARD_NODEJS_RUNTIME,
1515
logRetention: logs.RetentionDays.ONE_WEEK,
16+
logRemovalPolicy: cdk.RemovalPolicy.DESTROY,
1617
});
1718

1819
new lambda.Function(stack, 'OneMonth', {
1920
code: new lambda.InlineCode('exports.handler = (event) => console.log(JSON.stringify(event));'),
2021
handler: 'index.handler',
2122
runtime: STANDARD_NODEJS_RUNTIME,
2223
logRetention: logs.RetentionDays.ONE_MONTH,
24+
logRemovalPolicy: cdk.RemovalPolicy.DESTROY,
2325
});
2426

2527
new lambda.Function(stack, 'OneYear', {
2628
code: new lambda.InlineCode('exports.handler = (event) => console.log(JSON.stringify(event));'),
2729
handler: 'index.handler',
2830
runtime: STANDARD_NODEJS_RUNTIME,
2931
logRetention: logs.RetentionDays.ONE_YEAR,
32+
logRemovalPolicy: cdk.RemovalPolicy.DESTROY,
3033
});
3134

3235
new IntegTest(app, 'LambdaLogRetentionInteg', {
3336
testCases: [stack],
3437
diffAssets: true,
3538
});
36-
app.synth();

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,21 @@ const fn = new lambda.Function(this, 'MyFunctionWithFFTrue', {
281281
cdk.Tags.of(fn).add('env', 'dev'); // the tag is also added to the log group
282282
```
283283

284+
### Log removal policy
285+
286+
When using the deprecated `logRetention` property for creating a LogGroup, you can configure log removal policy:
287+
```ts
288+
import * as logs from 'aws-cdk-lib/aws-logs';
289+
290+
const fn = new lambda.Function(this, 'MyFunctionWithFFTrue', {
291+
runtime: lambda.Runtime.NODEJS_LATEST,
292+
handler: 'handler.main',
293+
code: lambda.Code.fromAsset('lambda'),
294+
logRetention: logs.RetentionDays.INFINITE,
295+
logRemovalPolicy: RemovalPolicy.RETAIN,
296+
});
297+
```
298+
284299
## Resource-based Policies
285300

286301
AWS Lambda supports resource-based policies for controlling access to Lambda

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import * as sns from '../../aws-sns';
3131
import * as sqs from '../../aws-sqs';
3232
import {
3333
Annotations, ArnFormat, CfnResource, Duration, FeatureFlags, Fn, IAspect, Lazy,
34-
Names, Size, Stack, Token,
34+
Names, RemovalPolicy, Size, Stack, Token,
3535
} from '../../core';
3636
import { UnscopedValidationError, ValidationError } from '../../core/lib/errors';
3737
import { addConstructMetadata, MethodMetadata } from '../../core/lib/metadata-resource';
@@ -456,10 +456,24 @@ export interface FunctionOptions extends EventInvokeConfigOptions {
456456
* myLogGroup.logGroupName;
457457
* ```
458458
*
459+
* @deprecated use `logGroup` instead
459460
* @default logs.RetentionDays.INFINITE
460461
*/
461462
readonly logRetention?: logs.RetentionDays;
462463

464+
/**
465+
* Determine the removal policy of the log group that is auto-created by this construct.
466+
*
467+
* Normally you want to retain the log group so you can diagnose issues
468+
* from logs even after a deployment that no longer includes the log group.
469+
* In that case, use the normal date-based retention policy to age out your
470+
* logs.
471+
*
472+
* @deprecated use `logGroup` instead
473+
* @default RemovalPolicy.Retain
474+
*/
475+
readonly logRemovalPolicy?: RemovalPolicy;
476+
463477
/**
464478
* The IAM role for the Lambda function associated with the custom resource
465479
* that sets the retention policy.
@@ -1120,6 +1134,14 @@ export class Function extends FunctionBase {
11201134
}
11211135

11221136
// Log retention
1137+
if (props.logRemovalPolicy) {
1138+
if (props.logGroup) {
1139+
throw new ValidationError('Cannot use `logRemovalPolicy` and `logGroup` together. Please set the removal policy on the logGroup directly', this);
1140+
} else if (FeatureFlags.of(this).isEnabled(USE_CDK_MANAGED_LAMBDA_LOGGROUP)) {
1141+
throw new ValidationError('Cannot use `logRemovalPolicy` and `@aws-cdk/aws-lambda:useCdkManagedLogGroup` flag together. Please set the removal policy on the automatically created log group directly', this);
1142+
}
1143+
}
1144+
11231145
if (props.logRetention) {
11241146
if (props.logGroup) {
11251147
throw new ValidationError('CDK does not support setting logRetention and logGroup', this);
@@ -1129,6 +1151,7 @@ export class Function extends FunctionBase {
11291151
retention: props.logRetention,
11301152
role: props.logRetentionRole,
11311153
logRetentionRetryOptions: props.logRetentionRetryOptions as logs.LogRetentionRetryOptions,
1154+
removalPolicy: props.logRemovalPolicy,
11321155
});
11331156
this._logGroup = logs.LogGroup.fromLogGroupArn(this, 'LogGroup', logRetention.logGroupArn);
11341157
this._logRetention = logRetention;

0 commit comments

Comments
 (0)