Skip to content

Commit 66d0ca8

Browse files
feat: env-specific log retention (prod 90d, staging 60d, dev 30d) (#646)
* log retention 30 days * feat: env-specific log retention (prod 90d, staging 60d, dev 30d) Replace hardcoded ONE_MONTH retention with a logRetentionDays() helper that maps STAGE to the appropriate RetentionDays value. Add stage prop to CronStackProps so all stacks derive retention from their environment. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ab2dd98 commit 66d0ca8

5 files changed

Lines changed: 33 additions & 4 deletions

File tree

bin/stacks/api-stack.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as aws_logs from 'aws-cdk-lib/aws-logs'
99
import * as aws_sns from 'aws-cdk-lib/aws-sns'
1010
import * as aws_waf from 'aws-cdk-lib/aws-wafv2'
1111
import { Construct } from 'constructs'
12-
import { STAGE } from '../../lib/util/stage'
12+
import { STAGE, logRetentionDays } from '../../lib/util/stage'
1313
import { SERVICE_NAME } from '../constants'
1414
import { DashboardStack } from './dashboard-stack'
1515
import { IndexCapacityConfig, TableCapacityConfig } from './dynamo-stack'
@@ -74,7 +74,9 @@ export class APIStack extends cdk.Stack {
7474
chatbotSNSArn,
7575
})
7676

77-
const accessLogGroup = new aws_logs.LogGroup(this, `${SERVICE_NAME}APIGAccessLogs`)
77+
const accessLogGroup = new aws_logs.LogGroup(this, `${SERVICE_NAME}APIGAccessLogs`, {
78+
retention: logRetentionDays(stage as STAGE),
79+
})
7880

7981
const api = new aws_apigateway.RestApi(this, `${SERVICE_NAME}`, {
8082
restApiName: `${SERVICE_NAME}`,

bin/stacks/cron-stack.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import { Construct } from 'constructs'
77
import path from 'path'
88

99
import { SERVICE_NAME, UNIMIND_ALGORITHM_CRON_INTERVAL, FILTER_PATTERNS } from '../constants'
10+
import { STAGE, logRetentionDays } from '../../lib/util/stage'
1011

1112
export interface CronStackProps extends cdk.NestedStackProps {
1213
lambdaRole: aws_iam.Role
14+
stage: STAGE
1315
chatbotSNSArn?: string
1416
envVars?: { [key: string]: string }
1517
}
@@ -35,6 +37,7 @@ export class CronStack extends cdk.NestedStack {
3537
environment: {
3638
NODE_OPTIONS: '--enable-source-maps',
3739
},
40+
logRetention: logRetentionDays(props.stage),
3841
})
3942

4043
new aws_events.Rule(this, `${SERVICE_NAME}UnimindAlgorithmCron`, {

bin/stacks/lambda-stack.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Queue } from 'aws-cdk-lib/aws-sqs'
1212
import { Construct } from 'constructs'
1313
import * as path from 'path'
1414
import { SUPPORTED_CHAINS } from '../../lib/util/chain'
15-
import { STAGE } from '../../lib/util/stage'
15+
import { STAGE, logRetentionDays } from '../../lib/util/stage'
1616
import { SERVICE_NAME, FILTER_PATTERNS } from '../constants'
1717
import { CronStack } from './cron-stack'
1818
import { DynamoStack, IndexCapacityConfig, TableCapacityConfig } from './dynamo-stack'
@@ -146,6 +146,7 @@ export class LambdaStack extends cdk.NestedStack {
146146
},
147147
environment: getOrdersEnv,
148148
tracing: aws_lambda.Tracing.ACTIVE,
149+
logRetention: logRetentionDays(props.stage),
149150
})
150151

151152
this.orderNotificationLambda = new aws_lambda_nodejs.NodejsFunction(this, `OrderNotification${lambdaName}`, {
@@ -171,6 +172,7 @@ export class LambdaStack extends cdk.NestedStack {
171172
vpcSubnets: {
172173
subnets: [...vpc.privateSubnets],
173174
},
175+
logRetention: logRetentionDays(props.stage),
174176
})
175177

176178
const notificationConfig = {
@@ -226,6 +228,7 @@ export class LambdaStack extends cdk.NestedStack {
226228
},
227229
environment: postOrderEnv,
228230
tracing: aws_lambda.Tracing.ACTIVE,
231+
logRetention: logRetentionDays(props.stage),
229232
})
230233

231234
this.postLimitOrderLambda = new aws_lambda_nodejs.NodejsFunction(this, `PostLimitOrder${lambdaName}`, {
@@ -241,6 +244,7 @@ export class LambdaStack extends cdk.NestedStack {
241244
},
242245
environment: postOrderEnv,
243246
tracing: aws_lambda.Tracing.ACTIVE,
247+
logRetention: logRetentionDays(props.stage),
244248
})
245249

246250
this.getLimitOrdersLambda = new aws_lambda_nodejs.NodejsFunction(this, `GetLimitOrders${lambdaName}`, {
@@ -255,6 +259,7 @@ export class LambdaStack extends cdk.NestedStack {
255259
sourceMap: true,
256260
},
257261
environment: getOrdersEnv,
262+
logRetention: logRetentionDays(props.stage),
258263
})
259264

260265
this.getNonceLambda = new aws_lambda_nodejs.NodejsFunction(this, `GetNonce${lambdaName}`, {
@@ -276,6 +281,7 @@ export class LambdaStack extends cdk.NestedStack {
276281
NODE_OPTIONS: '--enable-source-maps',
277282
},
278283
tracing: aws_lambda.Tracing.ACTIVE,
284+
logRetention: logRetentionDays(props.stage),
279285
})
280286

281287
this.getDocsLambda = new aws_lambda_nodejs.NodejsFunction(this, `GetDocs${lambdaName}`, {
@@ -295,6 +301,7 @@ export class LambdaStack extends cdk.NestedStack {
295301
VERSION: '6',
296302
NODE_OPTIONS: '--enable-source-maps',
297303
},
304+
logRetention: logRetentionDays(props.stage),
298305
})
299306

300307
this.getDocsUILambda = new aws_lambda_nodejs.NodejsFunction(this, `GetDocsUI${lambdaName}`, {
@@ -314,6 +321,7 @@ export class LambdaStack extends cdk.NestedStack {
314321
VERSION: '6',
315322
NODE_OPTIONS: '--enable-source-maps',
316323
},
324+
logRetention: logRetentionDays(props.stage),
317325
})
318326

319327
this.getUnimindLambda = new aws_lambda_nodejs.NodejsFunction(this, `GetUnimind${lambdaName}`, {
@@ -329,6 +337,7 @@ export class LambdaStack extends cdk.NestedStack {
329337
},
330338
environment: postOrderEnv,
331339
tracing: aws_lambda.Tracing.ACTIVE,
340+
logRetention: logRetentionDays(props.stage),
332341
})
333342

334343
if (props.envVars['POSTED_ORDER_DESTINATION_ARN']) {
@@ -703,6 +712,7 @@ export class LambdaStack extends cdk.NestedStack {
703712
/* cron stack */
704713
new CronStack(this, `${SERVICE_NAME}CronStack`, {
705714
lambdaRole,
715+
stage: props.stage,
706716
envVars: props.envVars,
707717
chatbotSNSArn: props.chatbotSNSArn,
708718
})

bin/stacks/step-function-stack.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Construct } from 'constructs'
77
import path from 'path'
88
import { checkDefined } from '../../lib/preconditions/preconditions'
99
import { SUPPORTED_CHAINS } from '../../lib/util/chain'
10-
import { STAGE } from '../../lib/util/stage'
10+
import { STAGE, logRetentionDays } from '../../lib/util/stage'
1111
import { SERVICE_NAME, FILTER_PATTERNS } from '../constants'
1212
import orderStatusTrackingStateMachine from '../definitions/order-tracking-sfn.json'
1313

@@ -53,6 +53,7 @@ export class StepFunctionStack extends cdk.NestedStack {
5353
...props.envVars,
5454
stage: stage,
5555
},
56+
logRetention: logRetentionDays(stage),
5657
})
5758
this.checkStatusFunction = checkStatusFunction
5859

lib/util/stage.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
import { RetentionDays } from 'aws-cdk-lib/aws-logs'
2+
13
export enum STAGE {
24
BETA = 'beta',
35
PROD = 'prod',
46
LOCAL = 'local',
57
}
8+
9+
export function logRetentionDays(stage: STAGE): RetentionDays {
10+
switch (stage) {
11+
case STAGE.PROD:
12+
return RetentionDays.THREE_MONTHS
13+
case STAGE.BETA:
14+
return RetentionDays.TWO_MONTHS
15+
default:
16+
return RetentionDays.ONE_MONTH
17+
}
18+
}

0 commit comments

Comments
 (0)