我想在 AWS CDK 中执行以下操作。
const a = new RandomObject(this, 'randomValue1', 'randomValue2').lambdaFunction;
const b = new RandomObject(this, 'randomValue111', 'randomValue2222').lambdaFunction;
然而,CDK 抱怨“InfraStack 中已经存在一个名为‘RandomObject’的构造”
有办法实现我想要做的事情吗?
Context :我有一个 stepFunction,我想通过传递 2 个变量“randomValue1”和“randomValue2”来动态创建它。 randomValue1 将是步骤函数的名称,randomValue2 将是其中 lambda 函数的触发器。保持其他一切不变。 如果可以的话,我想避免为同一个状态机创建多个构造。
可能会发生此问题,因为由
RandomObject
创建的构造没有传递唯一的构造作为其作用域。当您创建自己的构造子类时,它们应该从 Construct 继承,然后将 this
作为范围传递给它们的子类。 CDK 将通过这种方式确保所有 Cloudformation 逻辑 ID 都是唯一的。
class MyBucket extends Construct {
constructor(scope, id, props = {}) {
super(scope, id);
// It is very important that `this` is passed to any child construct instead of `scope.
// The `bucket` logical ID can be static.
const bucket = new s3.Bucket(this, 'bucket');
}
}
稍后您可以在堆栈中像这样使用它:
new MyBucket(this, 'bucket1');
new MyBucket(this, 'bucket2');