Skip to content

Commit 81500ec

Browse files
committed
Refactor EpsAssistMeStack and LambdaFunction modules
1 parent bb20f48 commit 81500ec

File tree

3 files changed

+225
-103
lines changed

3 files changed

+225
-103
lines changed

packages/cdk/constructs/LambdaFunction.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface LambdaFunctionProps {
2525
readonly entryPoint: string
2626
readonly environmentVariables: {[key: string]: string}
2727
readonly additionalPolicies?: Array<IManagedPolicy>
28+
readonly role?: Role
2829
readonly logRetentionInDays: number
2930
readonly logLevel: string
3031
}
@@ -99,15 +100,27 @@ export class LambdaFunction extends Construct {
99100
]
100101
})
101102

102-
const role = new Role(this, "LambdaRole", {
103-
assumedBy: new ServicePrincipal("lambda.amazonaws.com"),
104-
managedPolicies: [
105-
putLogsManagedPolicy,
106-
lambdaInsightsLogGroupPolicy,
107-
cloudwatchEncryptionKMSPolicy,
108-
...(props.additionalPolicies ?? [])
109-
]
110-
})
103+
// Role/Policy Aggregation
104+
const requiredPolicies: Array<IManagedPolicy> = [
105+
putLogsManagedPolicy,
106+
lambdaInsightsLogGroupPolicy,
107+
cloudwatchEncryptionKMSPolicy,
108+
...(props.additionalPolicies ?? [])
109+
]
110+
111+
let role: Role
112+
if (props.role) {
113+
role = props.role
114+
// Attach any missing managed policies to the provided role
115+
for (const policy of requiredPolicies) {
116+
role.addManagedPolicy(policy)
117+
}
118+
} else {
119+
role = new Role(this, "LambdaRole", {
120+
assumedBy: new ServicePrincipal("lambda.amazonaws.com"),
121+
managedPolicies: requiredPolicies
122+
})
123+
}
111124

112125
// Define the Lambda function
113126
const lambdaFunction = new lambda.Function(this, props.functionName, {

packages/cdk/nagSuppressions.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ export const nagSuppressions = (stack: Stack) => {
204204
id: "AwsSolutions-IAM5",
205205
reason: "SlackBot Lambda needs access to all guardrails to apply content filtering.",
206206
appliesTo: [
207-
"Resource::arn:aws:bedrock:eu-west-2:591291862413:guardrail/*"
207+
"Resource::arn:aws:bedrock:eu-west-2:591291862413:guardrail/*",
208+
"Resource::arn:aws:bedrock:eu-west-2:123456789012:guardrail/*"
208209
]
209210
}
210211
]
@@ -219,11 +220,35 @@ export const nagSuppressions = (stack: Stack) => {
219220
id: "AwsSolutions-IAM5",
220221
reason: "Lambda needs to invoke itself for Slack Bolt lazy handlers.",
221222
appliesTo: [
222-
"Resource::arn:aws:lambda:eu-west-2:591291862413:function:*"
223+
"Resource::arn:aws:lambda:eu-west-2:591291862413:function:*",
224+
"Resource::arn:aws:lambda:eu-west-2:123456789012:function:AmazonBedrock*"
223225
]
224226
}
225227
]
226228
)
229+
230+
// Suppress secrets without rotation
231+
safeAddNagSuppression(
232+
stack,
233+
"/EpsAssistMeStack/SlackBotTokenSecret/Resource",
234+
[
235+
{
236+
id: "AwsSolutions-SMG4",
237+
reason: "Slack bot token rotation is handled manually as part of the Slack app configuration process."
238+
}
239+
]
240+
)
241+
242+
safeAddNagSuppression(
243+
stack,
244+
"/EpsAssistMeStack/SlackBotSigningSecret/Resource",
245+
[
246+
{
247+
id: "AwsSolutions-SMG4",
248+
reason: "Slack signing secret rotation is handled manually as part of the Slack app configuration process."
249+
}
250+
]
251+
)
227252
}
228253

229254
const safeAddNagSuppression = (stack: Stack, path: string, suppressions: Array<NagPackSuppression>) => {

0 commit comments

Comments
 (0)