Letsencrypt is nowadays very popular certificates authority.
It is standard de-facto for most of situations when you need green sealed certificate on your environment. New version of the API (v2) provides very nice way to issue wildcard certificates using DNS validation.
Although it is not recommended to put read/write dns credentials on a such environment, there might be exception that forces you to do so on a temporary basis.
Workaround below provides way to limit write scope of the credentials, when your domain is served by AWS Route53.
Let’s assume we want to create wildcard certificate for our staging environment
Create another public hosted zone for the domain
_acme-challenge.staging.yourdomain.com I know, you are probably thinking "but that's not a domain!", but in the relevant sense, it actually is still a domain, just four level one.
Route 53 will assign 4 new nameservers to this new hosted zone. Make a note of those servers.
You will get something like
Return to your original hosted zone, and create a record for
_acme-challenge.staging.yourdomain.com of type NS. As a value you will use to create this record use those 4 nameservers that Route 53 assigned to the new hosted zone, one per line.
Important! Do not change any of the existing NS records in either of the zones.
What we have implemented by this step is called a delegation — i.e. you are delegating authority for
staging.yourdomain.com subdomain to a different hosted zone, which you will notice was automatically assigned a completely different set of 4 Route 53 servers from those that handle your parent domain.
You can now create a new record in the root of the new hosted zone, and when you do a DNS query for _acme-challenge.staging.yourdomain.com, the answer returned will be the answer from the new hosted zone.
Now, you can give your script permission only to modify records in the new zone, and it will be unable to modify anything in the parent zone, because you gave it no permissions, there. So we approaching to the next step
Now we can prepare AWS policy with fine grained permissions
and link this policy to appropriate IAM user, and get his access credentials.
Corresponding terraform script might be as following (always check to most recent documentation https://certbot-dns-route53.readthedocs.io/en/stable/ )
Example: generating certificate using certbot
We now need to put the AWS credentials on the server so the plugin can use them. I run all my certbot commands out of the default user’s home folder, your setup might be different.
In the home folder create an .aws folder and inside that create a text file with the name
credentials with the following contents.
Replace the placeholders with the access key and secret access key that you just saved from AWS and fill them in.
Once created, check it was configured properly
Next step would be, naturally, generating the certificate
Install tool of your choice, for example, classic
certbot (examples below are given for ubuntu family)
or using their up-to-date script
If you see output like below, you are done:
Now let’s ensure, that our domain will be prolonged automatically
If you want to setup cron, and you have specified custom log, config, work dirs — make sure to specify full path in crontab.
Your certificates will be located under config dir
Example: generating certificate using acme.sh
My own preference is pure shell acme.sh
Acme.sh https://github.com/Neilpang/acme.sh — it is another extra cool tool written purely in shell. It supports number of dns providers in form of shell(!) plugins https://github.com/Neilpang/acme.sh/tree/master/dnsapi
Each plugin has comprehensive documentation on configuring https://github.com/Neilpang/acme.sh/wiki/dnsapi
For Route53, you will need to export your credentials
With acme.sh generating certificate is simple
Renewing is also straightforward — just create cron similar to
Next: use certificates in your webserver
Terraform snippets might be found at https://github.com/Voronenko/route53-letsencrypt-policy/ ; Ansible role assisting you with install on target server can be found here: https://github.com/softasap/sa-acme-sh