Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RestApi: CDK is duplicating methods that added to a proxy by adding them to the root resource #25849

Open
alantos opened this issue Jun 5, 2023 · 6 comments
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort p3 package/tools Related to AWS CDK Tools or CLI

Comments

@alantos
Copy link

alantos commented Jun 5, 2023

Describe the bug

Having RestApi

const restApi = new RestApi(this, 'rest-api', {
    restApiName: `rest-api`,
    deploy: true,
    deployOptions: { ... },
    cloudWatchRole: true,
    endpointConfiguration: {
        types: [EndpointType.REGIONAL],
    },
});

When you add proxy resource to it like so and then you add a method(example shows OPTIONS but it happens with other methods too) to the created proxy

const proxy = restApi.root.addProxy({
    anyMethod: false,
});

proxy.addMethod('OPTIONS');

Cloud formation template that is created has this method created twice, one attached to a proxy and one more attached to the root of the RestApi. That when deployed creates 2 methods instead of 1, as per template.

"publicgatewaypublicgatewayapiproxyOPTIONS130DF52D": {
 "Type": "AWS::ApiGateway::Method",
 "Properties": {
  "HttpMethod": "OPTIONS",
  "ResourceId": {
   "Ref": "publicgatewaypublicgatewayapiproxy91EF2EEC"
  },
  "RestApiId": {
   "Ref": "publicgatewaypublicgatewayapi060C6F16"
  },
  "AuthorizationType": "NONE",
  "Integration": {
   "Type": "MOCK"
  }
 },
 "Metadata": {
  "aws:cdk:path": "dev-eu-west-1-main-adopt-routing/public-gateway/public-gateway-api/Default/{proxy+}/OPTIONS/Resource"
 }
},
"publicgatewaypublicgatewayapiOPTIONS68011C19": {
 "Type": "AWS::ApiGateway::Method",
 "Properties": {
  "HttpMethod": "OPTIONS",
  "ResourceId": {
   "Fn::GetAtt": [
    "publicgatewaypublicgatewayapi060C6F16",
    "RootResourceId"
   ]
  },
  "RestApiId": {
   "Ref": "publicgatewaypublicgatewayapi060C6F16"
  },
  "AuthorizationType": "NONE",
  "Integration": {
   "Type": "MOCK"
  }
 },
 "Metadata": {
  "aws:cdk:path": "dev-eu-west-1-main-adopt-routing/public-gateway/public-gateway-api/Default/OPTIONS/Resource"
 }
},

When you would add some other path resource before proxy i.e.

const res = restApi.root.addResource('test');

const proxy = res.addProxy({
    anyMethod: false,
});

proxy.addMethod('OPTIONS');

It would create only one method as expected.

Same issue occurs when you import existing resource i.e.

const res = Resource.fromResourceAttributes(this, 'resource', {
    restApi: restApiResource,
    resourceId: importedResourceId,
    path: '/',
});

It could point at root or even further down the path. And if you add proxy directly to it and some method

const proxy = res.root.addProxy({
    anyMethod: false,
});

proxy.addMethod('OPTIONS');

It is going to duplicate that method also to the root of the restApi.
And again if you add some additional path before adding proxy it will only create one method, as expected.

Expected Behavior

Cloud formation template should only have one method added, to the proxy resource and not to the root of the RestApi.

Current Behavior

Cloud formation template that is created has this method created twice, one attached to a proxy and one more attached to the root of the RestApi.

Reproduction Steps

Added to the description of the bug

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.82.0 (build 3a8648a)

Framework Version

No response

Node.js Version

v14.19.0

OS

macOS 12.6 (M1 Pro)

Language

Typescript

Language Version

Typescript (4.9.5)

Other information

No response

@alantos alantos added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 5, 2023
@github-actions github-actions bot added the package/tools Related to AWS CDK Tools or CLI label Jun 5, 2023
@pahud pahud self-assigned this Jun 5, 2023
@pahud
Copy link
Contributor

pahud commented Jun 5, 2023

I guess instead of

const proxy = restApi.root.addProxy({
    anyMethod: false,
});

proxy.addMethod('OPTIONS');

Maybe you should try this.

const proxy = restApi.root.addProxy({
    anyMethod: false,
});

restApi.root.addMethod('OPTIONS');

As you are adding an method to the root resource of the restApi. Does it work for you?

@pahud pahud added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 5, 2023
@pahud pahud removed their assignment Jun 5, 2023
@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jun 5, 2023
@alantos
Copy link
Author

alantos commented Jun 6, 2023

Yes, if I would like to add that method to the root of the API this is what I would do, but the problem is I want to add it only to the proxy resource.
And when I am doing this

const proxy = restApi.root.addProxy({
    anyMethod: false,
});

proxy.addMethod('OPTIONS');

CDK adds that method to proxy and root resources and it should only add it to the proxy.

So like I mentioned above, code above results in CloudFormation template that has 2 methods (AWS::ApiGateway::Method), one referencing proxy resource and one root of the RestAPI.

So when you deploy that code your API Gateway will look like this
image

and it should look like this
image

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 6, 2023
@potkae
Copy link

potkae commented Nov 17, 2023

Is there any workarounds to this? I'm facing the same issue.

@runtime
Copy link

runtime commented Apr 29, 2024

You can try with anyMethod: true, Instead of :

`const proxy = restApi.root.addProxy({
    anyMethod: false,
});

proxy.addMethod('OPTIONS');`

Or you can try adding a resource under the root and add your proxy to that
try items...

const rootMethod = restAPI.root.addMethod(
        'ANY',
        //lambdaFunctionIntegration <-- if you are using lambda handler
 );
 
 addCorsOptions(restAPI.root)
 
const items = restAPI.root.addResource('items');
items.addMethod('ANY', lambdaFunctionIntegration);
addCorsOptions(items);

 
 const proxy = items.addProxy({
      anyMethod: true,
      defaultMethodOptions: {
        authorizationType: api.AuthorizationType.NONE,
        requestParameters: {
          'method.request.path.proxy': true
        }
      }
    });
    proxy.addMethod('GET', lambdaFunctionIntegration);
    addCorsOptions(proxy);
    

to add cors Options you could write a function like the one below that uses IResource to pass all your resources and functions and assign the appropriate methods and responses

import { IResource, LambdaIntegration, MockIntegration, PassthroughBehavior, 
     RestApi } from 'aws-cdk-lib/aws-apigateway';
`    
  function addCorsOptions(resource: IResource) {
       resource.addMethod('OPTIONS, new MockIntegration({
       // all of your the method and integration responses would go here.
  })
}

Hope this is helpful.

@ashishdhingra
Copy link
Contributor

@alantos Good afternoon. Could you please confirm if this is still an issue or if it could be closed?

@ashishdhingra ashishdhingra added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 6, 2024
Copy link

github-actions bot commented Jun 9, 2024

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Jun 9, 2024
@pahud pahud added p3 and removed p2 closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. effort/medium Medium work item – several days of effort p3 package/tools Related to AWS CDK Tools or CLI
Projects
None yet
Development

No branches or pull requests

5 participants