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.

[apigateway] defaultMethodOptions not available when using RestApi.fromRestApiAttributes()

See original GitHub issue

The new asset system and fromRestApiAttributes() updates have been great, they have solved some of my long held problems.

I came across this in the last step of splitting up my API stacks, everything looked ok until I realised the endpoints were missing their options methods.

Maybe this is intended, but I couldn’t find an explanation in the docs.

When adding methods to a restApi that was imported using RestApi.fromRestApiAttributes(), defaultCorsPreflightOptions and defaultMethodOptions don’t seem to be imported.

If this is intended, is there a way to set defaultCorsPreflightOptions and defaultMethodOptions on an imported rest api?

Thanks, Justin

Reproduction Steps

Stack 1: create a rest api and pass it to a nested stack

this.restApi = new apigateway.RestApi(this, `${platform}-api`, {
      defaultCorsPreflightOptions: {
        allowOrigins: apigateway.Cors.ALL_ORIGINS,
        allowMethods: apigateway.Cors.ALL_METHODS,
        allowHeaders: apigateway.Cors.DEFAULT_HEADERS,
        allowCredentials: true,
      },
      defaultMethodOptions: {
        authorizationType: apigateway.AuthorizationType.NONE,
        methodResponses: [
          {
            statusCode: "200",
            responseModels: {
              "application/json": new EmptyModel(),
            },
            responseParameters: {
              "method.response.header.Access-Control-Allow-Headers": true,
              "method.response.header.Access-Control-Allow-Methods": true,
              "method.response.header.Access-Control-Allow-Credentials": true,
              "method.response.header.Access-Control-Allow-Origin": true,
            },
          },
        ],
      },
    });

...

    new OrderApiStack(this, "orderApiStack",{
      ...props,
      restApiId:this.restApi.restApiId,
      rootResourceId:this.restApi.restApiRootResourceId
}
);

stack 2: import and add a method to the rest api

const restApi = RestApi.fromRestApiAttributes(this, "RestApi", {
      restApiId: props.restApiId!,
      rootResourceId: props.rootResourceId!,
    });

...

 restApi.root
      .resourceForPath("orders")
      .addMethod("GET", integration, {
        authorizer: { authorizerId: apiStack.authorizer.ref },
        authorizationType: AuthorizationType.COGNITO,
      });

Environment

  • CLI Version : 1.49.1
  • Framework Version: 1.49.1
  • Node.js Version: v12.13.1
  • OS : OSX
  • Language (Version): TypeScript

This is 🐛 Bug Report

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nija-atcommented, Aug 12, 2020

Copied from https://github.com/aws/aws-cdk/issues/9574 by @sblackstone

https://docs.aws.amazon.com/cdk/api/latest/docs/aws-apigateway-readme.html#breaking-up-methods-and-resources-across-stacks

Not sure to report this as a bug or documentation issue, but the solution presented here breaks a number of things. For example, if you have defaultCorsFlightOptions, they won’t be carried over since the api’s are looked up in each stack by ID.

The solution I finely settled on was to manually call addCorsPreflight(…opts…) on each resource and push the method into the array so the stage waits for all of the OPTIONS methods to be created as well before building the deployment. You can’t even pass the cors options into the resource’s constructor as a prop since that method will be added silently and you won’t have access to it to make the deployment dependent on the method.

0reactions
sblackstonecommented, Dec 23, 2022

FYI - I wrote a blog post about how to get ApiGatewayV2 working with CORS after a long struggle.

https://medium.com/@steveb3210/apigatewayv2-cloudfront-distribution-and-cors-via-the-cdk-9bae7f22ecf8

Maybe this will help someone.

Read more comments on GitHub >

github_iconTop Results From Across the Web

aws-cdk/aws-apigateway module - AWS Documentation
A very common practice is to use Amazon API Gateway with AWS Lambda as the backend integration. The LambdaRestApi construct makes it easy:...
Read more >
What does an API gateway do? - Red Hat
An API gateway is an application programming interface (API) management tool that sits between a client and a collection of backend services.
Read more >
APIGateway Default Integration not working - Stack Overflow
I had to create an ANY method on the parent resource ( /foo ). const foo = this.gateway.root.resourceForPath("/foo"); foo.
Read more >
REST API (API Gateway v1) - Serverless Framework
To create HTTP endpoints as Event sources for your AWS Lambda Functions, use the Serverless Framework's easy AWS API Gateway Events syntax. There...
Read more >
aws.apigateway.RestApi - Pulumi
Documentation for the aws.apigateway.RestApi resource with examples, input properties, output properties, lookup functions, and supporting types.
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