How do you handle domains, certificates and DNS with CDK?

Hello everyone, I’ve recently fell in love with CDK and have had more and more exposure to infra/ devops work. But what I cannot for the love of me figure out is how what to do with domains, (this is my own startup that I’ll be talking about)

We have several moving parts, some of which should have their own domain, and alongside that, we also have several environments. So ideally I’d love to have `staging-analytics.company.com` or some variation of that. But what I found to be cumbersome is the following, 1) having stuff related to acm or route53 seems to be tricky in CDK since it can sometimes hang indefinitely until something is propagated or if something is misconfigured, and 2), we use the company emails for AWS accounts, so I cannot move the domain to AWS or there’s a risk of getting locked out of our accounts.

So with all of this in mind, how is this usually approached? I would love to have the domains be handled via the cdk as well, as I love having everything in code


Related discussions:

There are some things that the CDK doesn’t handle well. Try using the SDK instead.

Can you elaborate a bit?

The CDK doesn’t support OAC with CloudFront, for example:

https://github.com/aws/aws-cdk/issues/21771

There are some things that need to be handled imperatively, for which the SDK is better suited. If you want to create a certificate with ACM, for example, you could use the RequestCertificateCommand, and you would also need to validate the domain through your registrar’s DNS settings.

If you are willing to add one more degree of subdomain (i.e. analytics.staging.companyname.com) what you can do is domain delegation from your registar for the subdomain staging.companyname.com to Route53 and at that point all the automated DNS validation constructs can be used. Then for certain prod ones such as the public facing site you can do a manual delegation or write a one off script.

This delegation seems interesting, I’ll look into that, thanks

This is what we do for our internal stuff.

I would try and use level 1 cloud formation constructs first if possible, but sounds like even that isn’t possible yet

I use cdk for Terraform instead. My domain is on Cloudflare and it has a Terraform provider to manage it. Also Terraform works faster and more reliable than cloudformation.

Wildcard certificate for each env like *.dev.company.com, set the ARN of the certificate as an SSM variable or in cdk.json, import and use. Route53 shouldn’t have any additional complications. You can manage that shared certificate in its own stack, via clickops, or however you like.

Do you purchase a wildcard cert for each env, or is there a service that provides them?

Just note that the OAC issue can be worked around with L1 constructs.

One in each env, each env a different account. Each env has its own zone in those accounts.