简体   繁体   中英

add dependency on custom resource cdk

I have the following problem that i cannot add dependsOn on a cfnResource for a CustomResource

  const cfnRawTable = new timestream.CfnTable(this, 'MyCfnTableRaw', {
      databaseName: dbName,

      // the properties below are optional
      magneticStoreWriteProperties: {
        enableMagneticStoreWrites: true,
      },
      retentionProperties: {
        magneticStoreRetentionPeriodInDays: '1825',
        memoryStoreRetentionPeriodInHours: '8640',
      },
      tableName: rawtable,
    })

    let insertionLambda = new cdk.CustomResource(this, 'insertionlambda', {
      serviceToken:
        'arn:aws:lambda:' +
        cdk.Fn.ref('region') +
        '738234497474:function:timestreaminsertion-' +
        cdk.Fn.ref('env'),
    })

    cfnRawTable.addDependsOn(insertionLambda)

I get the error Argument of type 'CustomResource' is not assignable to parameter of type 'CfnResource'

I think you have todo it the other way around:

insertionLambda.addDependsOn(cfnRawTable);

Use CDK's Construct dependencies instead:

cfnRawTable.node.addDependency(insertionLambda);

addDependsOn is a low-level CloudFormation feature that is only available in L1 CloudFormation resources. The high-level .node.addDependency is available with all Constructs.

In this case, though, it does seem from the naming that your insertion lambda depends on the table, not the other way around, like @Martin Muller pointed out. You can still use the above, just swap them around. This guess an totally be incorrect, of course, maybe your lambda is not inserting into this particular table.

If you make any reference to the resource from another, it will automatically add the dependency.

As an example, adding the cfnRawTable.attrArn to the properties argument for CustomResource will cause the dependency relationship to be created.

cdk.CustomResource(
  ..., 
  {
    properties: {'tablearn': cfnRawTable.attrArn}
  },
)

Alternatively, you can declare a dependency without needing to make any reference using .node.addDependency

insertionLambda.node.addDependency(CfnRawTable)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM