Skip to content

Commit 78f138f

Browse files
committed
Reorder creation of dependencies
1 parent 1de00bd commit 78f138f

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

packages/cdk/resources/LambdaFunction.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export interface LambdaFunctionProps {
3232
const insightsLayerArn = "arn:aws:lambda:eu-west-2:580247275435:layer:LambdaInsightsExtension:55"
3333

3434
export class LambdaFunction extends Construct {
35-
public readonly function: lambda.Function
3635
public readonly executionPolicy: ManagedPolicy
36+
public readonly function: lambda.Function
3737

3838
public constructor(scope: Construct, id: string, props: LambdaFunctionProps) {
3939
super(scope, id)
@@ -138,9 +138,9 @@ export class LambdaFunction extends Construct {
138138
}
139139
}
140140

141-
// Create an execution policy for external use
142-
this.executionPolicy = new ManagedPolicy(this, "ExecutionPolicy", {
143-
description: `Allow invoking ${props.functionName}`,
141+
// Policy to allow invoking this Lambda
142+
const executionManagedPolicy = new ManagedPolicy(this, "ExecuteLambdaManagedPolicy", {
143+
description: `execute lambda ${props.functionName}`,
144144
statements: [
145145
new PolicyStatement({
146146
actions: ["lambda:InvokeFunction"],
@@ -151,5 +151,6 @@ export class LambdaFunction extends Construct {
151151

152152
// Outputs
153153
this.function = lambdaFunction
154+
this.executionPolicy = executionManagedPolicy
154155
}
155156
}

packages/cdk/resources/RestApiGateway/LambdaEndpoint.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
import {Construct} from "constructs"
22
import {IResource, LambdaIntegration} from "aws-cdk-lib/aws-apigateway"
33
import {HttpMethod} from "aws-cdk-lib/aws-lambda"
4+
import {IRole} from "aws-cdk-lib/aws-iam"
45
import {LambdaFunction} from "../LambdaFunction"
56

67
export interface LambdaEndpointProps {
78
readonly parentResource: IResource
89
readonly resourceName: string
910
readonly method: HttpMethod
11+
readonly restApiGatewayRole: IRole
1012
readonly lambdaFunction: LambdaFunction
1113
}
1214

13-
// Creates an API Gateway resource and method integrated with a Lambda function
1415
export class LambdaEndpoint extends Construct {
1516
public readonly resource: IResource
1617

1718
constructor(scope: Construct, id: string, props: LambdaEndpointProps) {
1819
super(scope, id)
1920

20-
// Add a new resource to the parent resource
2121
const resource = props.parentResource.addResource(props.resourceName)
2222

23-
// Let CDK/APIGateway manage the Lambda invoke permission automatically
24-
resource.addMethod(props.method, new LambdaIntegration(props.lambdaFunction.function))
23+
resource.addMethod(props.method, new LambdaIntegration(props.lambdaFunction.function, {
24+
credentialsRole: props.restApiGatewayRole
25+
}))
26+
27+
props.restApiGatewayRole.addManagedPolicy(props.lambdaFunction.executionPolicy)
2528

2629
this.resource = resource
2730
}

packages/cdk/stacks/EpsAssistMeStack.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import {
2222
} from "aws-cdk-lib/aws-bedrock"
2323
import {RestApiGateway} from "../resources/RestApiGateway"
2424
import {LambdaFunction} from "../resources/LambdaFunction"
25-
import {nagSuppressions} from "../nagSuppressions"
2625
import {LambdaIntegration} from "aws-cdk-lib/aws-apigateway"
2726
import * as iam from "aws-cdk-lib/aws-iam"
2827
import * as ops from "aws-cdk-lib/aws-opensearchserverless"
29-
import * as ssm from "aws-cdk-lib/aws-ssm"
3028
import * as cr from "aws-cdk-lib/custom-resources"
29+
import * as ssm from "aws-cdk-lib/aws-ssm"
30+
import {nagSuppressions} from "../nagSuppressions"
3131

3232
export interface EpsAssistMeStackProps extends StackProps {
3333
readonly stackName: string
@@ -367,6 +367,15 @@ export class EpsAssistMeStack extends Stack {
367367
})
368368
kbDataSource.node.addDependency(kb)
369369

370+
// ==== IAM Policy for Lambda to read SSM parameters ====
371+
const slackLambdaSSMPolicy = new PolicyStatement({
372+
actions: ["ssm:GetParameter", "ssm:GetParameters", "ssm:GetParameterHistory"],
373+
resources: [
374+
slackBotTokenParameter.parameterArn,
375+
slackSigningSecretParameter.parameterArn
376+
]
377+
})
378+
370379
// ==== Lambda environment variables ====
371380
const lambdaEnv: {[key: string]: string} = {
372381
RAG_MODEL_ID: "anthropic.claude-3-sonnet-20240229-v1:0",
@@ -396,15 +405,6 @@ export class EpsAssistMeStack extends Stack {
396405
additionalPolicies: []
397406
})
398407

399-
// ==== IAM Policy for Lambda to read SSM parameters ====
400-
const slackLambdaSSMPolicy = new PolicyStatement({
401-
actions: ["ssm:GetParameter", "ssm:GetParameters", "ssm:GetParameterHistory"],
402-
resources: [
403-
slackBotTokenParameter.parameterArn,
404-
slackSigningSecretParameter.parameterArn
405-
]
406-
})
407-
408408
// ==== Lambda self-invoke policy (needed for Slack Bolt lazy handlers) ====
409409
const slackLambdaSelfInvokePolicy = new PolicyStatement({
410410
actions: ["lambda:InvokeFunction"],
@@ -446,18 +446,12 @@ export class EpsAssistMeStack extends Stack {
446446
trustStoreKey: "unused",
447447
truststoreVersion: "unused"
448448
})
449-
450-
// Grant the API Gateway role permission to invoke the Lambda function
451-
apiGateway.role.addManagedPolicy(slackBotLambda.executionPolicy)
452-
453-
// Create API resources directly to avoid circular dependencies
454-
const slackResource = apiGateway.api.root.addResource("slack")
455-
const askEpsResource = slackResource.addResource("ask-eps")
456-
457-
// Add the method with Lambda integration and explicit role
458-
askEpsResource.addMethod("POST", new LambdaIntegration(slackBotLambda.function, {
449+
// Add SlackBot Lambda to API Gateway
450+
const slackRoute = apiGateway.api.root.addResource("slack").addResource("ask-eps")
451+
slackRoute.addMethod("POST", new LambdaIntegration(slackBotLambda.function, {
459452
credentialsRole: apiGateway.role
460453
}))
454+
apiGateway.role.addManagedPolicy(slackBotLambda.executionPolicy)
461455

462456
// ==== Output: SlackBot Endpoint ====
463457
new CfnOutput(this, "SlackBotEndpoint", {

0 commit comments

Comments
 (0)