aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

addDependency not working as documented

Open rigsbyt opened this issue 6 years ago • 3 comments

Per the Cloudformation documentation, APIGateway deployments must depend on any methods: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-deployment.html#aws-resource-apigateway-deployment--examples

Per the CDK documentation here, it sounds like all you should need to do to express this in CDK is deployment.node.addDependency(method): https://docs.aws.amazon.com/cdk/api/latest/docs/core-readme.html#construct-dependencies

But that call does nothing (silently; it seems to be a complete no-op). In order to express the dependency, we had to do the following: (this.restApi.latestDeployment?.node.defaultChild as apiGateway.CfnDeployment).addDependsOn(method.node)

I'm not sure whether this is a product bug or a documentation bug, but it was pretty challenging to square the behavior I was seeing with the documentation.

Reproduction Steps

const restApi = new RestApi(...)
const resource = restApi.root.addResource('resource')
const method = resources.addMethod('POST', (integration), ...)

restApi.latestDeployment?.node.addDependency(method)

Error Log

Silently, nothing happens.

Environment

  • 1.31.0
  • 1.31.0
  • Mac
  • Typescript

This is :bug: Bug Report

rigsbyt avatar Mar 26 '20 22:03 rigsbyt

Hi @rigsbyt,

The CDK will add the DependsOn to the deployment resource implicitly when adding the method:

(properties removed for clarity)

    const restApi = new apigateway.RestApi(this, 'myapi')
    const resource = restApi.root.addResource('resource')
    const method = resource.addMethod('POST')

The synthesized cloudformation template will include the bellow snippet: (removed properties for clarity)

    "myapiGET9B7CD29E": {
      "Type": "AWS::ApiGateway::Method",
     
    },
    "myapiDeploymentB7EF8EB75c091a668064a3f3a1f6d68a3fb22cf9": {
      "Type": "AWS::ApiGateway::Deployment",
      "Properties": {
       
      "DependsOn": [
        "myapiGET9B7CD29E"
      ],
    }

What do you mean by does nothing?

NetaNir avatar Mar 27 '20 20:03 NetaNir

I've tracked down the source of the issue. My setup involves a CDK library imported as a separate package. The resource dependency is added during the prepare phase. The constructs library uses instanceof cdk.Construct to determine whether to call .prepare(). Because my dependency manager has installed two separate instances of CDK 1.31.0, the instanceof evaluates to false, because they are not the exact same object.

This seems to be the same issue addressed in https://github.com/aws/aws-cdk/issues/1245, but in a different place.

I am working to try and get my dependency manager to dedupe these in a nice way, but it's a little tricky given our setup. Avoiding the instanceof call would be nice.

rigsbyt avatar Mar 29 '20 22:03 rigsbyt

@rigsbyt - using different versions of the CDK packages will cause this. This has been a source of issues for many customers and it's not easy to root cause that this is the problem.

We have a new packaging model in the works -https://github.com/aws/aws-cdk-rfcs/pull/122/files - which should address a lot of these issues.

I'm closing this issue for now.

nija-at avatar Apr 01 '20 09:04 nija-at