Skip to content

CDK AwsCustomResource: Howto know when there are no more ResponseFields?

0

What I'm trying to accomplish:

I want to call the SES verifyDomainDkim API method via AwsCustomResource. From the documentation I know that it returns a response as follows:

{
    DkimTokens: [
       "EXAMPLEq76owjnks3lnluwg65scbemvw", 
       "EXAMPLEi3dnsj67hstzaj673klariwx2", 
       "EXAMPLEwfbtcukvimehexktmdtaz6naj"
    ]
}

Now I want to use those DkimTokens to create the required CNAME records in Route53. How do I know how many entries the DkimTokensarray has? I currently have hardcoded looping over 3 records like the following:

const verifyDomainDkim = new AwsCustomResource(...);
for (let i = 0; i < 3; i++) {
      new route53.CnameRecord(this, `ses-dkim-${i}`, {
        zone: props.hostedZone,
        recordName: `${Token.asString(verifyDomainDkim.getResponseFieldReference(`DkimTokens.${i}`))}._domainkey.${domain}`,
        domainName: `${Token.asString(verifyDomainDkim.getResponseFieldReference(`DkimTokens.${i}`))}.dkim.amazonses.com`,
      }).node.addDependency(verifyDomainDkim);
    }

However what if at some point there are more than three DKIM tokens? Can I somehow make this dynamic?

AWS

asked 6 years ago1.3K views

1 Answer
0
Accepted Answer

You'll need to think about this slightly differently, as your CDK code is only executing BEFORE CloudFormation creates the stack. Your references to the response values from your custom resource will be rendered into some combination of GetAtt and Select intrinsic functions in the template which is output, and so you're back to your already-identified problem of knowing how many results there will be.

I think the way to handle this may well be another custom resource (or maybe the same one, if you want) which takes the DkimTokens list and can execute the logic to create the Route 53 records as required. So you won't have AWS::Route53::RecordSets in your stack, you'll have a Custom::CreateRecords (or whatever) which "owns" all the records, but those records are opaque to CloudFormation.

AWS
EXPERT

answered 6 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.