Skip to content

Commit 331b3a2

Browse files
committed
Add Lambda self-invoke policy and allow API Gateway to invoke the Lambda
1 parent ee1d31d commit 331b3a2

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

packages/cdk/stacks/EpsAssistMeStack.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as iam from "aws-cdk-lib/aws-iam"
2727
import * as ops from "aws-cdk-lib/aws-opensearchserverless"
2828
import * as cr from "aws-cdk-lib/custom-resources"
2929
import * as ssm from "aws-cdk-lib/aws-ssm"
30+
import * as lambda from "aws-cdk-lib/aws-lambda"
3031
import {nagSuppressions} from "../nagSuppressions"
3132

3233
export interface EpsAssistMeStackProps extends StackProps {
@@ -377,12 +378,12 @@ export class EpsAssistMeStack extends Stack {
377378
})
378379

379380
// ==== Lambda self-invoke policy (needed for Slack Bolt lazy handlers) ====
380-
// const slackLambdaSelfInvokePolicy = new PolicyStatement({
381-
// actions: ["lambda:InvokeFunction"],
382-
// resources: [
383-
// slackBotLambda.function.functionArn
384-
// ]
385-
// })
381+
const slackLambdaSelfInvokePolicy = new PolicyStatement({
382+
actions: ["lambda:InvokeFunction"],
383+
resources: [
384+
`arn:aws:lambda:${this.region}:${this.account}:function:*`
385+
]
386+
})
386387

387388
// ==== Lambda environment variables ====
388389
const lambdaEnv: {[key: string]: string} = {
@@ -415,7 +416,7 @@ export class EpsAssistMeStack extends Stack {
415416

416417
// ==== Attach all policies to SlackBot Lambda role ====
417418
slackBotLambda.function.addToRolePolicy(slackLambdaSSMPolicy)
418-
// slackBotLambda.function.addToRolePolicy(slackLambdaSelfInvokePolicy)
419+
slackBotLambda.function.addToRolePolicy(slackLambdaSelfInvokePolicy)
419420

420421
// ==== API Gateway & Slack Route ====
421422
const apiGateway = new RestApiGateway(this, "EpsAssistApiGateway", {
@@ -425,12 +426,18 @@ export class EpsAssistMeStack extends Stack {
425426
trustStoreKey: "unused",
426427
truststoreVersion: "unused"
427428
})
428-
// Add SlackBot Lambda to API Gateway
429429
const slackRoute = apiGateway.api.root.addResource("slack").addResource("ask-eps")
430430
slackRoute.addMethod("POST", new LambdaIntegration(slackBotLambda.function, {
431431
credentialsRole: apiGateway.role
432432
}))
433-
apiGateway.role.addManagedPolicy(slackBotLambda.executionPolicy)
433+
434+
// ==== Allow API Gateway to invoke the Lambda ====
435+
new lambda.CfnPermission(this, "ApiGatewayInvokeSlackBotLambda", {
436+
action: "lambda:InvokeFunction",
437+
functionName: slackBotLambda.function.functionName,
438+
principal: "apigateway.amazonaws.com",
439+
sourceArn: `arn:aws:execute-api:${this.region}:${this.account}:${apiGateway.api.restApiId}/*/POST/slack/ask-eps`
440+
})
434441

435442
// ==== Output: SlackBot Endpoint ====
436443
new CfnOutput(this, "SlackBotEndpoint", {

0 commit comments

Comments
 (0)