Search code examples
amazon-web-servicesaws-cloudformationaws-api-gatewayaws-cdk

How to access AWS::Lambda::Permissions resource logical ID from CDK API Gateway


I need to override the logical id of some HttpLambdaIntegration CDK resources (context is beyond this post, but yes, I know this isn't recommended).

In my CloudFormation stack under Resources, I see the node tree looks like this:

| Logical Id                                                | Physical ID| Type 
| ----------------------------------------------------------| -----------| -----------------------------
| MyFrontendHttpApi                                         |            |
|   - Default Route                                         |            |
|     - HttpIntegration-123                                 |            |
|       |- MyFrontendHttpAPIDefaultRouteHttpIntegration123  | abcde      | AWS::ApiGatewayV2::Integration
|     - Resource                                            |            |
|       |- MyFrontendHttpApiDefaultRouteDDEEFF              | fghij      | AWS::ApiGatewayV2::Route
|     |- MyFrontendHttpApiDefaultRouteMyFronte...Permission | klmnop     | AWS::Lambda::Permission
  

To override the HttpIntegration logical id, I'm using this code (as recommended here)

const defaultRoute = httpApi.node.findChild('DefaultRoute');
const myInt = defaultRoute.node.findChild('apiGatewayLambdaIntegration'); // apiGatewayLambdaIntegration is an ID that I specify in my code
const resource = myInt.node.findChild('Resource') as CfnIntegration;
resource.overrideLogicalId(myDesiredLogicalId);

However, how can I access the last one (Lambda permission)?

It doesn't show up as the default child, and I can't seem to find an ID to use in findChild() either. I'm not original writer of this code, but it's not explicitly created anywhere in the code: I imagine it seems to be an auto-generated permission. But it is in this node tree in Cloudformation, so I assume it must be accessible somehow...


Solution

  • The CfnPermission resource is a child of the route. The Permission's construct ID is the name of your integration followed by -Permission.

    const cfnPermission = defaultRoute.node.findChild("apiGatewayLambdaIntegration-Permission") as lambda.CfnPermission;
    cfnPermission.overrideLogicalId("OveriddenPermissionId")