typescriptaws-cloudformationaws-cdkamazon-simple-email-service

CDK Cloud Formation not waiting for addDependency to complete


I am building a CDK stack and new to CDK. I am trying to create a Simple Email Service (SES) ConfigurationSet and then an EmailIdentity, which requires the ConfigurationSet to already be created. When I run them the cloud formation step for EmailIdentity fails due to the Configuration Set not completed yet. I thought if I added EmailIdentityConstructor.node.adddDependency(ConfigurationSet) that cloud formation would wait, but adding this to the CDK seemed to have no effect.

What am I doing wrong?

Logically, this should be a single stack. It's one set of SES resources I am trying to create.

    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);

    const myConfigSet = ses.ConfigurationSet.fromConfigurationSetName(this, id, 'email-bi-config-set');

    const EmailIdentityConstructor = new ses.EmailIdentity(this, 'email-identity', {
      identity: ses.Identity.domain(domain),
      configurationSet: myConfigSet,
      feedbackForwarding: true,
      mailFromDomain: domain,
    });

    EmailIdentityConstructor.applyRemovalPolicy(RemovalPolicy.DESTROY);
    EmailIdentityConstructor.node.addDependency(ConfigurationSetConstructor);

Solution

  • I'm not sure why you are creating the config set and then importing it in the same stack. If you synthesize the stack, you'll notice that CDK is not adding the DependsOn between the resources. I'm not sure why that is happening in your case.

    But you can simplify the implementation to allow CloudFormation to infer the dependencies.

        const myConfigSet = new ses.ConfigurationSet(this, 'email-bi-config-set', {
          sendingEnabled: true,
          reputationMetrics: false,
          suppressionReasons: ses.SuppressionReasons.BOUNCES_AND_COMPLAINTS,
          tlsPolicy: ses.ConfigurationSetTlsPolicy.REQUIRE,
        });
    
        myConfigSet.applyRemovalPolicy(RemovalPolicy.DESTROY);
    
        const EmailIdentityConstructor = new ses.EmailIdentity(this, 'email-identity', {
          identity: ses.Identity.domain(domain),
          configurationSet: myConfigSet,
          feedbackForwarding: true,
          mailFromDomain: domain,
        });
    
        EmailIdentityConstructor.applyRemovalPolicy(RemovalPolicy.DESTROY);
    

    With this implementation, CDK will create a Ref in the template and automatically infer the dependency between both.