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

API Gateway CORS preflight causes synth error #5232

Closed
lukehedger opened this issue Nov 27, 2019 · 4 comments · Fixed by #5249
Closed

API Gateway CORS preflight causes synth error #5232

lukehedger opened this issue Nov 27, 2019 · 4 comments · Fixed by #5249
Assignees
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway bug This issue is a bug. p1

Comments

@lukehedger
Copy link
Contributor

Running cdk synth on a stack that uses the API Gateway Construct (LambdaRestApi) with CORS preflight enabled throws an error.

Reproduction Steps

new LambdaRestApi(this, "SomeEndpoint", {
  defaultCorsPreflightOptions: {
    allowOrigins: Cors.ALL_ORIGINS,
    allowMethods: Cors.ALL_METHODS
  },
  handler: someLambda
});

Error Log

$ cdk synth
some-stack/node_modules/@aws-cdk/core/lib/construct.js:395
  throw new Error(`There is already a Construct with name '${childName}' in ${typeName}${name.length > 0 ? ' [' + name + ']' : ''}`);

Error: There is already a Construct with name 'OPTIONS' in RootResource [Default]
  at ConstructNode.addChild (some-stack/node_modules/@aws-cdk/core/lib/construct.js:395:19)
  at new ConstructNode (some-stack/node_modules/@aws-cdk/core/lib/construct.js:32:24)
  at new Construct (some-stack/node_modules/@aws-cdk/core/lib/construct.js:435:21)
  at new Resource (some-stack/node_modules/@aws-cdk/core/lib/resource.js:13:9)
  at new Method (some-stack/node_modules/@aws-cdk/aws-apigateway/lib/method.js:10:9)
  at RootResource.addMethod (some-stack/node_modules/@aws-cdk/aws-apigateway/lib/resource.js:17:16)
  at ProxyResource.addMethod (some-stack/node_modules/@aws-cdk/aws-apigateway/lib/resource.js:216:33)
  at ProxyResource.addCorsPreflight (some-stack/node_modules/@aws-cdk/aws-apigateway/lib/resource.js:93:21)
  at new Resource (some-stack/node_modules/@aws-cdk/aws-apigateway/lib/resource.js:190:18)
  at new ProxyResource (some-stack/node_modules/@aws-cdk/aws-apigateway/lib/resource.js:201:9)

Environment

  • CLI Version : 1.18.0 (build bc924bc)
  • Framework Version: 1.18.0
  • OS : macOS
  • Language : TypeScript

This is 🐛 Bug Report

@lukehedger lukehedger added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Nov 27, 2019
@SomayaB SomayaB added the @aws-cdk/aws-apigateway Related to Amazon API Gateway label Nov 28, 2019
@eladb
Copy link
Contributor

eladb commented Nov 28, 2019

Seems like the CORS OPTIONS conflicts with another construct named "OPTIONS"

@nija-at nija-at added p1 and removed needs-triage This issue or PR still needs to be triaged. labels Nov 28, 2019
eladb pushed a commit that referenced this issue Dec 16, 2019
…stApi (#5249)

If a proxy resource is added to the root, it will reflect any `addMethod` calls to the root resource, which causes two OPTIONS methods to be created when CORS is specified (one directly against the root resource and one by the proxy).

This uncovered a more general bug where if the proxy resource was added to the root, and the root already included a method of a certain kind, we will get a duplicate.

The fix is to avoid the proxy `addMethod` reflection on the root in case there is already a method by that type on the root.

This, indirectly, also fixes #5232, where CORS cannot be used with `LambdaRestApi`, which is basically a root proxy.
@halvardssm
Copy link

Hi! Sorry to reawaken such an old issue, but I am facing the exact same problem on 1.149.0. Should I create a new issue for this?

@amirmishani
Copy link

I'm also getting the same error on 2.21.0.

@hhimanshu
Copy link

Any resolutions to this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway bug This issue is a bug. p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants