Skip to content

Commit 13ac879

Browse files
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>
1 parent d80193e commit 13ac879

5 files changed

Lines changed: 31 additions & 16 deletions

File tree

bin/stacks/api-stack.ts

Lines changed: 2 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'
@@ -75,7 +75,7 @@ export class APIStack extends cdk.Stack {
7575
})
7676

7777
const accessLogGroup = new aws_logs.LogGroup(this, `${SERVICE_NAME}APIGAccessLogs`, {
78-
retention: aws_logs.RetentionDays.ONE_MONTH,
78+
retention: logRetentionDays(stage as STAGE),
7979
})
8080

8181
const api = new aws_apigateway.RestApi(this, `${SERVICE_NAME}`, {

bin/stacks/cron-stack.ts

Lines changed: 3 additions & 1 deletion
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,7 +37,7 @@ export class CronStack extends cdk.NestedStack {
3537
environment: {
3638
NODE_OPTIONS: '--enable-source-maps',
3739
},
38-
logRetention: aws_logs.RetentionDays.ONE_MONTH,
40+
logRetention: logRetentionDays(props.stage),
3941
})
4042

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

bin/stacks/lambda-stack.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import { CfnEIP, NatProvider, Vpc } from 'aws-cdk-lib/aws-ec2'
66
import * as aws_iam from 'aws-cdk-lib/aws-iam'
77
import * as aws_kms from 'aws-cdk-lib/aws-kms'
88
import * as aws_lambda from 'aws-cdk-lib/aws-lambda'
9-
import * as aws_logs from 'aws-cdk-lib/aws-logs'
109
import { DynamoEventSource, SqsDlq } from 'aws-cdk-lib/aws-lambda-event-sources'
1110
import * as aws_lambda_nodejs from 'aws-cdk-lib/aws-lambda-nodejs'
1211
import { Queue } from 'aws-cdk-lib/aws-sqs'
1312
import { Construct } from 'constructs'
1413
import * as path from 'path'
1514
import { SUPPORTED_CHAINS } from '../../lib/util/chain'
16-
import { STAGE } from '../../lib/util/stage'
15+
import { STAGE, logRetentionDays } from '../../lib/util/stage'
1716
import { SERVICE_NAME, FILTER_PATTERNS } from '../constants'
1817
import { CronStack } from './cron-stack'
1918
import { DynamoStack, IndexCapacityConfig, TableCapacityConfig } from './dynamo-stack'
@@ -147,7 +146,7 @@ export class LambdaStack extends cdk.NestedStack {
147146
},
148147
environment: getOrdersEnv,
149148
tracing: aws_lambda.Tracing.ACTIVE,
150-
logRetention: aws_logs.RetentionDays.ONE_MONTH,
149+
logRetention: logRetentionDays(props.stage),
151150
})
152151

153152
this.orderNotificationLambda = new aws_lambda_nodejs.NodejsFunction(this, `OrderNotification${lambdaName}`, {
@@ -173,7 +172,7 @@ export class LambdaStack extends cdk.NestedStack {
173172
vpcSubnets: {
174173
subnets: [...vpc.privateSubnets],
175174
},
176-
logRetention: aws_logs.RetentionDays.ONE_MONTH,
175+
logRetention: logRetentionDays(props.stage),
177176
})
178177

179178
const notificationConfig = {
@@ -229,7 +228,7 @@ export class LambdaStack extends cdk.NestedStack {
229228
},
230229
environment: postOrderEnv,
231230
tracing: aws_lambda.Tracing.ACTIVE,
232-
logRetention: aws_logs.RetentionDays.ONE_MONTH,
231+
logRetention: logRetentionDays(props.stage),
233232
})
234233

235234
this.postLimitOrderLambda = new aws_lambda_nodejs.NodejsFunction(this, `PostLimitOrder${lambdaName}`, {
@@ -245,7 +244,7 @@ export class LambdaStack extends cdk.NestedStack {
245244
},
246245
environment: postOrderEnv,
247246
tracing: aws_lambda.Tracing.ACTIVE,
248-
logRetention: aws_logs.RetentionDays.ONE_MONTH,
247+
logRetention: logRetentionDays(props.stage),
249248
})
250249

251250
this.getLimitOrdersLambda = new aws_lambda_nodejs.NodejsFunction(this, `GetLimitOrders${lambdaName}`, {
@@ -260,7 +259,7 @@ export class LambdaStack extends cdk.NestedStack {
260259
sourceMap: true,
261260
},
262261
environment: getOrdersEnv,
263-
logRetention: aws_logs.RetentionDays.ONE_MONTH,
262+
logRetention: logRetentionDays(props.stage),
264263
})
265264

266265
this.getNonceLambda = new aws_lambda_nodejs.NodejsFunction(this, `GetNonce${lambdaName}`, {
@@ -282,7 +281,7 @@ export class LambdaStack extends cdk.NestedStack {
282281
NODE_OPTIONS: '--enable-source-maps',
283282
},
284283
tracing: aws_lambda.Tracing.ACTIVE,
285-
logRetention: aws_logs.RetentionDays.ONE_MONTH,
284+
logRetention: logRetentionDays(props.stage),
286285
})
287286

288287
this.getDocsLambda = new aws_lambda_nodejs.NodejsFunction(this, `GetDocs${lambdaName}`, {
@@ -302,7 +301,7 @@ export class LambdaStack extends cdk.NestedStack {
302301
VERSION: '5',
303302
NODE_OPTIONS: '--enable-source-maps',
304303
},
305-
logRetention: aws_logs.RetentionDays.ONE_MONTH,
304+
logRetention: logRetentionDays(props.stage),
306305
})
307306

308307
this.getDocsUILambda = new aws_lambda_nodejs.NodejsFunction(this, `GetDocsUI${lambdaName}`, {
@@ -322,7 +321,7 @@ export class LambdaStack extends cdk.NestedStack {
322321
VERSION: '5',
323322
NODE_OPTIONS: '--enable-source-maps',
324323
},
325-
logRetention: aws_logs.RetentionDays.ONE_MONTH,
324+
logRetention: logRetentionDays(props.stage),
326325
})
327326

328327
this.getUnimindLambda = new aws_lambda_nodejs.NodejsFunction(this, `GetUnimind${lambdaName}`, {
@@ -338,7 +337,7 @@ export class LambdaStack extends cdk.NestedStack {
338337
},
339338
environment: postOrderEnv,
340339
tracing: aws_lambda.Tracing.ACTIVE,
341-
logRetention: aws_logs.RetentionDays.ONE_MONTH,
340+
logRetention: logRetentionDays(props.stage),
342341
})
343342

344343
if (props.envVars['POSTED_ORDER_DESTINATION_ARN']) {
@@ -642,6 +641,7 @@ export class LambdaStack extends cdk.NestedStack {
642641
/* cron stack */
643642
new CronStack(this, `${SERVICE_NAME}CronStack`, {
644643
lambdaRole,
644+
stage: props.stage,
645645
envVars: props.envVars,
646646
chatbotSNSArn: props.chatbotSNSArn,
647647
})

bin/stacks/step-function-stack.ts

Lines changed: 2 additions & 2 deletions
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,7 +53,7 @@ export class StepFunctionStack extends cdk.NestedStack {
5353
...props.envVars,
5454
stage: stage,
5555
},
56-
logRetention: aws_logs.RetentionDays.ONE_MONTH,
56+
logRetention: logRetentionDays(stage),
5757
})
5858
this.checkStatusFunction = checkStatusFunction
5959

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)