Better application deployment with DigitalOcean, Terraform, Ansible and Docker. DNS Records

As we are creating our instances on the fly, using Terraform declarative scripts, we can also need to create dns domains and records. Let’s say, you have two environments: dev and prod, and you you want to have your backend application stored under dev.api.myapp.com and prod.api.myapp.com. Luckily, terraform allows to do such things

Required steps

Digital Ocean Domain

Add additional declaration to your terraform file:

resource "digitalocean_domain" "default" {
   name = "dev.api.myapp.com"
   ip_address = "${digitalocean_droplet.app-rest-api.ipv4_address}"

DigitalOcean Record

If you want your app to be accessible through dev.api.myapp.com, without wwwm you should add this lines:

resource "digitalocean_record" "CNAME-www" {
  domain = "${digitalocean_domain.default.name}"
  type = "CNAME"
  name = "www"
  value = "@"

Final Check

You should see your domain name in the list of networking domains here - https://cloud.digitalocean.com/networking/domains

On the next part we will see how to start working with cloud instances using ansible, and how to connect ansible with terraform.