使用new s3notifications时的循环引用.在cdk中的LambdaDestination



我遇到了一个循环问题

props.incomingBucket.addEventNotification(EventType.OBJECT_CREATED, new s3notifications.LambdaDestination(this.incomingHandler));

这是我得到的错误:

throw new Error('${target.node.path}' depends on '${this.node.path}' (${cycle.join(', ')}). Adding this dependency (${reason}) would create a cyclic reference.);

我在堆栈A中有incomingBucket,在stackB中有incomingHandler,这个props.incomingBucket.addEventNotification(EventType.OBJECT_CREATED, new s3notifications.LambdaDestination(this.incomingHandler));在stackB中,但它遇到了这个问题。然而,props.incomingBucket.grantReadWrite(this.incomingHandler);这没有问题,所以我不完全确定发生了什么…

这是我的完整堆栈:

export interface LambdaProps {
readonly env: DeploymentEnvironment;
readonly stackName?: string;
readonly stage: string;
readonly vpc: EC2Vpc;
readonly incomingBucket: Bucket;
readonly blobsBucket: Bucket;
readonly blobsTable: Table;
readonly identExtractor: Extractor;
}
export class LambdaStack extends DeploymentStack {
public readonly apiLambda: IFunction;
public readonly dropboxLambda: IFunction;
public readonly incomingHandler: IFunction;
constructor(parent: core.App, name: string, props: LambdaProps) {
super(parent, name, {
softwareType: SoftwareType.INFRASTRUCTURE,
...props
});
this.incomingHandler = new Function(this, "IncomingHandler", {
code: .... stuff here,
handler: 'handlers.incoming_lambda',
runtime: Runtime.PYTHON_3_7,
timeout: core.Duration.seconds(30),
environment: {
.... stuff here
}
});
// // new blobs in the incoming bucket trigger lambda event
// TODO ok this is causing a circular issue, need to figure out how to fix 
props.incomingBucket.addEventNotification(EventType.OBJECT_CREATED, new s3notifications.LambdaDestination(this.incomingHandler));
// grant permissions to the incoming lambda.
props.incomingBucket.grantReadWrite(this.incomingHandler);
props.blobsBucket.grantReadWrite(this.incomingHandler);
props.blobsTable.grantReadWriteData(this.incomingHandler);
props.identExtractor.taskQueue.grantSendMessages(this.incomingHandler);
}
}

基于https://github.com/aws/aws-cdk/issues/5760存在循环依赖问题。

基于https://github.com/aws/aws-cdk/pull/10426,它以bucket依赖于lambda的方式被解析。我假设你需要同样地改变你的代码——将lambda注入桶堆栈,而不是将bucket注入lambda堆栈。

最新更新