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 and Lambdas in separate stacks fails

See original GitHub issue

🐛 Bug Report

What is the problem?

I have what I would expect a very typical scenario: APIGateway that exposes several of my lambdas. I frequently have to delete the whole stack and would like to avoid destroying the APIGateway (so as not to change the URL and apikeys). So I wanted to split them to two stacks, one for the APIGateway and one for Lambdas. I first tried with CDK 1.1.0. This caused cyclic dependency errors (which I mentioned here. In hopes of it being fixed, I upgraded to 1.4.0. Now with the same setup I get:

cfn-reference.ts:108
      throw new Error(`Cross-stack reference (${context.scope.node.path} -> ${this.target.node.path}) has not been assigned a value--call prepare() first`);
            ^
Error: Resolution error: Resolution error: Resolution error: Cross-stack reference (ipk-PostiKioskiBackendStack-development -> ipk-PostiKioskiLambdaStack-development/ipk-ConfigLambda-development/Resource) has not been assigned a value--call prepare() first.
Object creation stack:
  at new Intrinsic (/Users/vertti/dev/posti/itsepalvelu-posti/posti-kioski-backend/node_modules/@aws-cdk/core/lib/private/intrinsic.ts:28:26)
  at new Reference (/Users/vertti/dev/posti/itsepalvelu-posti/posti-kioski-backend/node_modules/@aws-cdk/core/lib/reference.ts:21:5)

Reproduction Steps

Backend defined with:

const app = new core.App()
const stackLambda = new PostiKioskiLambdaStack(app, `ipk-PostiKioskiLambdaStack-${environment}`)
const stackBE = new PostiKioskiBackendStack(app, `ipk-PostiKioskiBackendStack-${environment}`, { configLambda: stackLambda.configLambda })

where PostiKioskiLambdaStack creates a lambda to a public configLambda property which is given in the props to stackBE.

The lambda in question is put inside a specific vpc:

    this.configLambda = new awsLambda.Function(this, `ipk-ConfigLambda-${environment}`, {
      role,
      vpc,

      code: awsLambda.Code.asset('resources'),
      handler: 'config_lambda.handler',
      runtime: awsLambda.Runtime.NODEJS_10_X,
    })

Verbose Log

https://gist.github.com/vertti/4883548b587774d1f1d2a75e78b640a1

Environment

  • CDK CLI Version: 1.4.0 (build 175471f)
  • OS: OSX Mojave
  • Language: TypeScript

Other information

There’s been lots of fixes done for similar cases where Lambdas are in one stack and for example SNS is in another, all of those seemed to cause cyclic dependency errors.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nija-atcommented, Sep 2, 2019

Update: Identified that the bug lies with API Gateway specific implementation of the Prepare phase, specifically within the LatestDeploymentResource.

The bug is here - https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-apigateway/lib/deployment.ts#L131.

The prepare phase, among other things, identifies intra-stack and cross-stack dependencies and registers them, for future resolution. It can be fully resolved only after the prepare phase is complete.

1reaction
nija-atcommented, Aug 23, 2019

Able to reproduce this with this CDK code -

#!/usr/bin/env node
import cdk = require('@aws-cdk/core');
import lambda = require('@aws-cdk/aws-lambda');
import apig = require('@aws-cdk/aws-apigateway');

class FirstStack extends cdk.Stack {
    public readonly firstLambda: lambda.Function;

    constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
      super(scope, id, props);

      this.firstLambda = new lambda.Function(this, 'firstLambda', {
        code: lambda.Code.asset('resources'),
        handler: 'index.handler',
        runtime: lambda.Runtime.NODEJS_10_X,
      });
    }
}

interface SecondStackProps extends cdk.StackProps {
  readonly lambda: lambda.Function;
}

class SecondStack extends cdk.Stack{
  constructor(scope: cdk.Construct, id: string, props: SecondStackProps) {
    super(scope, id, props);

    const api = new apig.RestApi(this, 'BooksApi');
    api.root.addMethod('ANY');
    const booksApi = api.root.addResource('books');
    const lambdaIntegration = new apig.LambdaIntegration(props.lambda);
    booksApi.addMethod('GET', lambdaIntegration);
  }
}
  
const app = new cdk.App();
const first = new FirstStack(app, 'FirstStack');
new SecondStack(app, 'SecondStack', { lambda: first.firstLambda });
app.synth();

followed by running cdk synth

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Nested Stack to separate API gateway error with ouse on ...
To refer to the output from a nested stack, you should use GetAtt : SubStacklambdaA: Type: 'AWS::CloudFormation::Stack' Properties: ...
Read more >
AWS::ApiGateway::Authorizer - AWS CloudFormation
The AWS::ApiGateway::Authorizer resource creates an authorization layer that API Gateway activates for methods that have authorization enabled.
Read more >
REST API (API Gateway v1) - Serverless Framework
To create HTTP endpoints as Event sources for your AWS Lambda Functions, ... Gateway must exist before deploying the stack, otherwise deployment will...
Read more >
Splitting your Serverless Framework API on AWS - GorillaStack
When combined with the AWS Lambda + API Gateway model for API development, ... to this problem is to split up your APIs...
Read more >
“Deployment” in Amazon API Gateway | by Jaewoo Ahn
A quick remediation/diagnose on this issue is deploying your API manually. If a problem is solved by doing it, then it means your...
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