Skip to content

Commit f0485cb

Browse files
committed
Keep Lambda execution role independent from API Gateway
1 parent 25a9bd3 commit f0485cb

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

packages/cdk/resources/RestApiGateway/LambdaEndpoint.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {HttpMethod} from "aws-cdk-lib/aws-lambda"
44
import {IRole} from "aws-cdk-lib/aws-iam"
55
import {LambdaFunction} from "../LambdaFunction"
66

7+
// Props for constructing an API Gateway endpoint backed by a Lambda function.
78
export interface LambdaEndpointProps {
89
readonly parentResource: IResource
910
readonly resourceName: string
@@ -12,20 +13,23 @@ export interface LambdaEndpointProps {
1213
readonly lambdaFunction: LambdaFunction
1314
}
1415

16+
// Creates an API Gateway resource and method integrated with a Lambda function.
1517
export class LambdaEndpoint extends Construct {
1618
public readonly resource: IResource
1719

1820
constructor(scope: Construct, id: string, props: LambdaEndpointProps) {
1921
super(scope, id)
2022

23+
// Add a new resource to the parent resource
2124
const resource = props.parentResource.addResource(props.resourceName)
2225

23-
resource.addMethod(props.method, new LambdaIntegration(props.lambdaFunction.function, {
24-
credentialsRole: props.restApiGatewayRole
25-
}))
26+
// Add a method to the resource, integrated with the Lambda function.
27+
resource.addMethod(props.method, new LambdaIntegration(props.lambdaFunction.function))
2628

27-
props.restApiGatewayRole.addManagedPolicy(props.lambdaFunction.executionPolicy)
29+
// Grant API Gateway's role permission to invoke the Lambda function.
30+
props.lambdaFunction.function.grantInvoke(props.restApiGatewayRole)
2831

32+
// Expose the resource for potential further use.
2933
this.resource = resource
3034
}
3135
}

0 commit comments

Comments
 (0)