Skip to content

Commit 1feda0c

Browse files
jogoldrix0rrr
authored andcommitted
feat(ecs): allow to specify log retention for aws log driver (#2511)
Allow to specify a log retention when using the AWS Log Driver with an automatically created group. Also add tests for the driver.
1 parent 6cf4bd3 commit 1feda0c

File tree

7 files changed

+97
-16
lines changed

7 files changed

+97
-16
lines changed

packages/@aws-cdk/aws-ecs/lib/log-drivers/aws-log-driver.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ export interface AwsLogDriverProps {
2727
*/
2828
readonly logGroup?: logs.ILogGroup;
2929

30+
/**
31+
* The number of days log events are kept in CloudWatch Logs when the log
32+
* group is automatically created by this construct.
33+
*
34+
* @default logs never expire
35+
*/
36+
readonly logRetentionDays?: logs.RetentionDays;
37+
3038
/**
3139
* This option defines a multiline start pattern in Python strftime format.
3240
*
@@ -57,8 +65,13 @@ export class AwsLogDriver extends LogDriver {
5765

5866
constructor(scope: cdk.Construct, id: string, private readonly props: AwsLogDriverProps) {
5967
super(scope, id);
68+
69+
if (props.logGroup && props.logRetentionDays) {
70+
throw new Error('Cannot specify both `logGroup` and `logRetentionDays`.');
71+
}
72+
6073
this.logGroup = props.logGroup || new logs.LogGroup(this, 'LogGroup', {
61-
retentionDays: 365,
74+
retentionDays: props.logRetentionDays || Infinity,
6275
});
6376
}
6477

packages/@aws-cdk/aws-ecs/test/ec2/integ.event-task.lit.expected.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,9 +1027,6 @@
10271027
},
10281028
"TaskLoggingLogGroupC7E938D4": {
10291029
"Type": "AWS::Logs::LogGroup",
1030-
"Properties": {
1031-
"RetentionInDays": 365
1032-
},
10331030
"DeletionPolicy": "Retain"
10341031
},
10351032
"Rule4C995B7F": {

packages/@aws-cdk/aws-ecs/test/fargate/integ.asset-image.expected.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,6 @@
904904
},
905905
"FargateServiceLoggingLogGroup9B16742A": {
906906
"Type": "AWS::Logs::LogGroup",
907-
"Properties": {
908-
"RetentionInDays": 365
909-
},
910907
"DeletionPolicy": "Retain"
911908
},
912909
"FargateServiceECC8084D": {

packages/@aws-cdk/aws-ecs/test/fargate/integ.l3.expected.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,6 @@
585585
},
586586
"L3LoggingLogGroupBD1F02DD": {
587587
"Type": "AWS::Logs::LogGroup",
588-
"Properties": {
589-
"RetentionInDays": 365
590-
},
591588
"DeletionPolicy": "Retain"
592589
},
593590
"L3Service616D5A93": {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { expect, haveResource } from '@aws-cdk/assert';
2+
import logs = require('@aws-cdk/aws-logs');
3+
import cdk = require('@aws-cdk/cdk');
4+
import { Test } from 'nodeunit';
5+
import ecs = require('../lib');
6+
7+
export = {
8+
'create an aws log driver'(test: Test) {
9+
// GIVEN
10+
const stack = new cdk.Stack();
11+
12+
// WHEN
13+
const driver = new ecs.AwsLogDriver(stack, 'Log', {
14+
datetimeFormat: 'format',
15+
logRetentionDays: logs.RetentionDays.OneMonth,
16+
multilinePattern: 'pattern',
17+
streamPrefix: 'hello'
18+
});
19+
20+
// THEN
21+
expect(stack).to(haveResource('AWS::Logs::LogGroup', {
22+
RetentionInDays: logs.RetentionDays.OneMonth
23+
}));
24+
25+
test.deepEqual(
26+
stack.node.resolve(driver.renderLogDriver()),
27+
{
28+
logDriver: 'awslogs',
29+
options: {
30+
'awslogs-group': { Ref: 'LogLogGroup427F779C' },
31+
'awslogs-stream-prefix': 'hello',
32+
'awslogs-region': { Ref: 'AWS::Region' },
33+
'awslogs-datetime-format': 'format',
34+
'awslogs-multiline-pattern': 'pattern'
35+
}
36+
}
37+
);
38+
39+
test.done();
40+
},
41+
42+
'with a defined log group'(test: Test) {
43+
// GIVEN
44+
const stack = new cdk.Stack();
45+
const logGroup = new logs.LogGroup(stack, 'LogGroup');
46+
47+
// WHEN
48+
const driver = new ecs.AwsLogDriver(stack, 'Log', {
49+
logGroup,
50+
streamPrefix: 'hello'
51+
});
52+
53+
// THEN
54+
test.deepEqual(
55+
stack.node.resolve(driver.renderLogDriver()),
56+
{
57+
logDriver: 'awslogs',
58+
options: {
59+
'awslogs-group': { Ref: 'LogGroupF5B46931' },
60+
'awslogs-stream-prefix': 'hello',
61+
'awslogs-region': { Ref: 'AWS::Region' }
62+
}
63+
}
64+
);
65+
66+
test.done();
67+
},
68+
69+
'throws when specifying log retention and log group'(test: Test) {
70+
// GIVEN
71+
const stack = new cdk.Stack();
72+
const logGroup = new logs.LogGroup(stack, 'LogGroup');
73+
74+
// THEN
75+
test.throws(() => new ecs.AwsLogDriver(stack, 'Log', {
76+
logGroup,
77+
logRetentionDays: logs.RetentionDays.FiveDays,
78+
streamPrefix: 'hello'
79+
}), /`logGroup`.*`logRetentionDays`/);
80+
81+
test.done();
82+
}
83+
};

packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.ec2-task.expected.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -835,9 +835,6 @@
835835
},
836836
"TaskLoggingLogGroupC7E938D4": {
837837
"Type": "AWS::Logs::LogGroup",
838-
"Properties": {
839-
"RetentionInDays": 365
840-
},
841838
"DeletionPolicy": "Retain"
842839
},
843840
"StateMachineRoleB840431D": {

packages/@aws-cdk/aws-stepfunctions-tasks/test/integ.fargate-task.expected.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,6 @@
457457
},
458458
"TaskLoggingLogGroupC7E938D4": {
459459
"Type": "AWS::Logs::LogGroup",
460-
"Properties": {
461-
"RetentionInDays": 365
462-
},
463460
"DeletionPolicy": "Retain"
464461
},
465462
"FargateTaskSecurityGroup0BBB27CB": {

0 commit comments

Comments
 (0)