Skip to content

Commit 046537d

Browse files
committed
feat(cdk-docker-cluster): support logging retention
1 parent afee1fe commit 046537d

5 files changed

Lines changed: 74 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@codedazur/cdk-docker-cluster": minor
3+
---
4+
5+
Add `logging` configuration option with `retention` to `ServiceProps`.

‎package-lock.json‎

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/cdk-docker-cluster/src/constructs/DockerCluster.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { App, Stack } from "aws-cdk-lib";
22
import { Match, Template } from "aws-cdk-lib/assertions";
33
import { ContainerImage } from "aws-cdk-lib/aws-ecs";
4+
import { RetentionDays } from "aws-cdk-lib/aws-logs";
45
import { describe, it } from "vitest";
56
import { DockerCluster } from "./DockerCluster";
67

@@ -83,4 +84,24 @@ describe("DockerCluster", () => {
8384
HealthCheckPath: "/api/health",
8485
});
8586
});
87+
88+
it("configures log retention when specified", () => {
89+
const app = new App();
90+
const stack = new Stack(app, "Test");
91+
92+
new DockerCluster(stack, "DockerCluster", {
93+
source: ContainerImage.fromRegistry("nginx:alpine"),
94+
service: {
95+
logging: {
96+
retention: RetentionDays.ONE_WEEK,
97+
},
98+
},
99+
});
100+
101+
const template = Template.fromStack(stack);
102+
103+
template.hasResourceProperties("AWS::Logs::LogGroup", {
104+
RetentionInDays: 7,
105+
});
106+
});
86107
});

‎packages/cdk-docker-cluster/src/constructs/DockerCluster.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
AssetImageProps,
1616
Cluster,
1717
ContainerImage,
18+
LogDriver,
1819
ScalableTaskCount,
1920
Secret,
2021
} from "aws-cdk-lib/aws-ecs";
@@ -23,6 +24,7 @@ import {
2324
ApplicationLoadBalancedFargateServiceProps,
2425
} from "aws-cdk-lib/aws-ecs-patterns";
2526
import { HealthCheck } from "aws-cdk-lib/aws-elasticloadbalancingv2";
27+
import { RetentionDays } from "aws-cdk-lib/aws-logs";
2628
import { Construct } from "constructs";
2729

2830
export interface ClusterConfig {
@@ -48,6 +50,13 @@ export interface DockerClusterProps {
4850
readonly cluster?: ClusterConfig;
4951
}
5052

53+
export interface LoggingConfig {
54+
/**
55+
* The number of days log events are kept in CloudWatch Logs.
56+
*/
57+
readonly retention?: RetentionDays;
58+
}
59+
5160
export interface ServiceProps {
5261
readonly port?: number;
5362
readonly tasks?: number | AutoScalingConfig;
@@ -59,6 +68,10 @@ export interface ServiceProps {
5968
* Health check configuration for the Application Load Balancer target group.
6069
*/
6170
readonly healthCheck?: HealthCheck;
71+
/**
72+
* CloudWatch logging configuration for the container.
73+
*/
74+
readonly logging?: LoggingConfig;
6275
}
6376

6477
interface AutoScalingConfig {
@@ -163,6 +176,13 @@ export class DockerCluster extends Construct {
163176
const containerInsights =
164177
this.props.containerInsights ?? this.props.cluster?.containerInsights;
165178

179+
const logDriver = this.props.service?.logging?.retention
180+
? LogDriver.awsLogs({
181+
streamPrefix: this.node.id,
182+
logRetention: this.props.service.logging.retention,
183+
})
184+
: undefined;
185+
166186
const service = new ApplicationLoadBalancedFargateService(this, "Service", {
167187
cluster: new Cluster(this, "Cluster", {
168188
containerInsights,
@@ -173,6 +193,7 @@ export class DockerCluster extends Construct {
173193
taskImageOptions: {
174194
image: this.image,
175195
containerPort: this.props.service?.port,
196+
logDriver,
176197
environment: this.props.service?.environment,
177198
secrets: this.props.service?.secrets,
178199
},

‎packages/cdk-next-app/src/constructs/NextApp.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { App, Stack } from "aws-cdk-lib";
22
import { Template } from "aws-cdk-lib/assertions";
33
import { ContainerImage } from "aws-cdk-lib/aws-ecs";
4+
import { RetentionDays } from "aws-cdk-lib/aws-logs";
45
import { describe, it } from "vitest";
56
import { NextApp } from "./NextApp";
67

@@ -45,4 +46,24 @@ describe("NextApp", () => {
4546
HealthCheckPath: "/api/health",
4647
});
4748
});
49+
50+
it("forwards log retention configuration to the log group", () => {
51+
const app = new App();
52+
const stack = new Stack(app, "Test");
53+
54+
new NextApp(stack, "NextApp", {
55+
source: ContainerImage.fromRegistry("nginx:alpine"),
56+
service: {
57+
logging: {
58+
retention: RetentionDays.ONE_WEEK,
59+
},
60+
},
61+
});
62+
63+
const template = Template.fromStack(stack);
64+
65+
template.hasResourceProperties("AWS::Logs::LogGroup", {
66+
RetentionInDays: 7,
67+
});
68+
});
4869
});

0 commit comments

Comments
 (0)