Aws-cdk: Defining Resources on imported RestApi Error on LambdaIntegration

Created on 22 Jun 2020  路  4Comments  路  Source: aws/aws-cdk

When we try to use the imported RestApi Gateway to define resources across stacks, it is not allowing to add new LambdaIntegration on the imported Gateway. However it work with MockIntegration as expected.
The following error is thrown on synth or deployment:
RestApi is not available on Resource not connected to an instance of RestApi. Useapiinstead

This works:

 const api = RestApi.fromRestApiAttributes(this, 'RestApi', {
      restApiId: props.restApiId,
      rootResourceId: props.rootResourceId
    });

    const method = api.root.addResource('pets').addMethod(
      'GET',
      new MockIntegration({
        integrationResponses: [
          {
            statusCode: '200'
          }
        ],
        passthroughBehavior: PassthroughBehavior.NEVER,
        requestTemplates: {
          'application/json': '{ "statusCode": 200 }'
        }
      }),
      {
        methodResponses: [{ statusCode: '200' }]
      }
    );

This doesn't:

const api = RestApi.fromRestApiAttributes(this, 'RestApi', {
      restApiId: props.restApiId,
      rootResourceId: props.rootResourceId
    });

    const method = api.root.addResource('pets').addMethod(
      'GET',
      new LambdaIntegration(
        new lambda.Function(this, 'MyLambda', {
          code: new lambda.AssetCode('src'),

          handler: 'index.main',
          runtime: lambda.Runtime.PYTHON_3_6
        })
      ),
      {
        methodResponses: [{ statusCode: '200' }]
      }
    );

PS: From the repo code it is clear that "restApi throws an error on imported while api returns correctly".
When we use LambdaIntegration, it intrinsically calls ${method.restApi.node.uniqueId} which will throw an error for imported gateway. Workarounds?

@aws-cdaws-apigateway bug p1

Most helpful comment

I've posted a fix for this here - https://github.com/aws/aws-cdk/pull/8785

All 4 comments

@nija-at Can you please take a look? ^

Thanks for reporting this bug. I don't think there are any workarounds for this.
Marking this as a p1 bug since there are no workarounds.

Thanks @nija-at . Counting on you for this one.

I've posted a fix for this here - https://github.com/aws/aws-cdk/pull/8785

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abelmokadem picture abelmokadem  路  3Comments

vgribok picture vgribok  路  3Comments

pepastach picture pepastach  路  3Comments

cybergoof picture cybergoof  路  3Comments

peterdeme picture peterdeme  路  3Comments