question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(aws-apigateway): Allow other stacks to add resource-paths to existing gateway resource-paths, and update Stage to latest Deployment

See original GitHub issue
  1. We have a CDK for our ApiGateway which has a base resource-path /v1. We have many lambda functions: we want Lambda-cdk’s to add resource paths to existing resource paths, whether they are created by this lambda’s CDK stack, or another lambda’s CDK stack.

  2. If another Stack creates an Api Deployment, the ApiGateway Stage should point to these new deployments

Use Case

CdkApiGateway

  • adds resource path /v1
  • deploy : true
  • stage: DEV

CdkLambda-1

const apiGateway = apiGateway.RestApi.fromRestApiAttributes(this, `api`, {
             restApiId: <restapi id from cf export>,
             rootResourceId: <root resource id from cf export>,
        });

apiGateway.root.getResource('/v1')
                           .addResource('lambda1Path')
                           .addMethod("GET")

CdkLambda-2

const apiGateway = apiGateway.RestApi.fromRestApiAttributes(this, `api`, {
             restApiId: <restapi id from cf export>,
             rootResourceId: <root resource id from cf export>,
        });

apiGateway.root.getResource('/v1')
                           .getResource('lambda1Path')
                           .addResource('lambda2Path')

Proposed Solution

A: currently “.getResource()” returns an undefined object, so we can’t handle existing resources in CDK.

  • Should return a defined resource to add resources, or add methods to, AND allow another stack to add resource-paths to the existing resource

B: Currently .resourceForPath("/v1") throws an error

CREATE_FAILED | AWS::ApiGateway::Resource | api-dev/Default/v1 (apidevv1795DEFC9) Another resource with the same parent already has this name: v1 (Service: AmazonApiGateway; Status Code: 409; Error Code: ConflictException; Request ID: 16d1f261-3a68-4771-98c8-2a6b7398e6d0; Proxy: null)

  • Should remove constraint, and allow other stacks to add resources to existing paths.
`.resourceForPath()` should allows us to Get an existing path, or create the path

Other

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jsonpj3wtcommented, Aug 16, 2022

@nija-at I also have this error.

Here is an example that “works” however, the lambda gets the wrong target path:


const gtwy = apigateway.RestApi.fromRestApiAttributes(this, 'apiAddressGtwy', {
    restApiId: process.env.CDK_API_GATEWAY_REST_API_ID,
    rootResourceId: process.env.CDK_API_V1_RESOURCE_ID,
});
const addressResource = new apigateway.Resource(this, 'addressResource', {
    parent: gtwy.root,
    pathPart: 'address',
});

const gtwyAddressMethod = addressResource?.addMethod(...)

This approach does create the proper methods in the right spot, however, the lambda target ArnLike is .../address instead of the expected full path of .../v1/address.

Let’s say I start at the root (/):


const gtwy = apigateway.RestApi.fromRestApiAttributes(this, 'apiAddressGtwy', {
    restApiId: process.env.CDK_API_GATEWAY_REST_API_ID,
    rootResourceId: process.env.CDK_API_ROOT_RESOURCE_ID,
});
const addressResource = new apigateway.Resource(this, 'addressResource', {
    parent: gtwy.root.getResource('/v1'), // Returns undefined
    pathPart: 'address',
});

const gtwyAddressMethod = addressResource?.addMethod(...)

Will fail because the getResource('/v1') is undefined. If I change it to have gtwy.root.resourceForPath('/v1'), the proper target path for the lambda is set, but will not complete with the following error: Another resource with the same parent already has this name.

I understand the idea behind creating another stack, but, we are moving over to CDK and is currently using serverless for the root path. I will attempt to import that from its CF stack, but, seems odd this cannot be done.

1reaction
tmclaughcommented, Mar 3, 2021

Okay, wanted to call out changing the Deployment LogicalId.

Read more comments on GitHub >

github_iconTop Results From Across the Web

aws-cdk/aws-apigateway module - AWS Documentation
Create an API to access data, business logic, or functionality from your back-end services, such as applications running on Amazon Elastic Compute Cloud...
Read more >
Serverless Import Apigateway
See Serverless for an example of sharing a single API Gateway between multiple stacks. This plugin allows you to specify the name and...
Read more >
aws api-gateway - cdk stage deployment setup - Stack Overflow
To create the Stage construct in the cdk stack, it required a Deployment , so I defined this in the cdk stack. deployment...
Read more >
Amazon API Gateway Deployment with CloudFormation
4.4 AWS::ApiGateway::VpcLink. 5 Idiosyncrasy. 5.1 Add Deployments and Stages to the Template; 5.2 Dynamically Change The Stack Template and ...
Read more >
The Missing Guide to AWS API Gateway Access Logs
Access logging fields (Or: What should I log?) General request info; Integration info; Authorizer info; Caller info; Other fields; Summary / TL; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found