CDK中添加addDependency后CloudFormation仍未等待,SES资源创建失败求助
问题根因
你代码里的核心错误是:创建完ConfigurationSetConstructor后,又用ses.ConfigurationSet.fromConfigurationSetName生成了myConfigSet,并把这个引用对象传给了EmailIdentity。fromConfigurationSetName是用来引用已存在的配置集(比如跨栈资源或手动创建的),它不会和你当前栈里刚创建的ConfigurationSetConstructor建立依赖关联——哪怕名字一模一样也没用。
你手动给EmailIdentityConstructor加的依赖是指向ConfigurationSetConstructor,但EmailIdentity实际绑定的是myConfigSet,而myConfigSet和ConfigurationSetConstructor之间没有任何依赖关系,所以CloudFormation不会等真正的配置集创建完成就开始创建EmailIdentity,导致报错。
修复方案
直接跳过中间的myConfigSet,把你创建的ConfigurationSetConstructor直接传给EmailIdentity的configurationSet参数。CDK会自动识别这种资源引用,帮你在CloudFormation里生成正确的依赖关系,不需要手动调用addDependency。
修正后的代码
const ConfigurationSetConstructor = new ses.ConfigurationSet(this, 'email-bi-config-set', { sendingEnabled: true, reputationMetrics: false, suppressionReasons: ses.SuppressionReasons.BOUNCES_AND_COMPLAINTS, tlsPolicy: ses.ConfigurationSetTlsPolicy.REQUIRE, }); ConfigurationSetConstructor.applyRemovalPolicy(RemovalPolicy.DESTROY); // 直接使用上面创建的配置集对象,不要用fromConfigurationSetName const EmailIdentityConstructor = new ses.EmailIdentity(this, 'email-identity', { identity: ses.Identity.domain(domain), configurationSet: ConfigurationSetConstructor, feedbackForwarding: true, mailFromDomain: domain, }); EmailIdentityConstructor.applyRemovalPolicy(RemovalPolicy.DESTROY); // 这里不需要手动添加依赖了,CDK已经自动处理
额外说明
当你在CDK中直接将一个资源对象传给另一个资源的参数时,CDK会自动在底层CloudFormation模板中生成DependsOn关系,确保资源按顺序创建。只有当你遇到一些特殊场景(比如间接依赖)时,才需要手动调用addDependency。
内容的提问来源于stack exchange,提问作者George Bentz

