繁体   English   中英

添加对自定义资源 cdk 的依赖

[英]add dependency on custom resource cdk

我有以下问题,我无法在 cfnResource 上为 CustomResource 添加 dependsOn

  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)

我收到错误Argument of type 'CustomResource' is not assignable to parameter of type 'CfnResource'

我认为你必须反过来做:

insertionLambda.addDependsOn(cfnRawTable);

改为使用 CDK 的 Construct 依赖项:

cfnRawTable.node.addDependency(insertionLambda);

addDependsOn是一种低级别的 CloudFormation 功能,仅在 L1 CloudFormation 资源中可用。 高级.node.addDependency可用于所有构造。

但是,在这种情况下,从命名上看,您的插入 lambda 确实取决于表格,而不是相反,就像@Martin Muller 指出的那样。 你仍然可以使用上面的,只是交换它们。 这个猜测完全不正确,当然,也许你的 lambda 没有插入到这个特定的表中。

如果你从另一个引用资源,它会自动添加依赖关系。

例如,将cfnRawTable.attrArn添加到CustomResourceproperties参数将导致创建依赖关系。

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

或者,您可以声明依赖关系而无需使用.node.addDependency进行任何引用

insertionLambda.node.addDependency(CfnRawTable)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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