Setting up Digital Ocean DNS Records for Github Pages using Terraform

How To Use Terraform to setup DNS Records for GitHub Pages in Digital Ocean

We’ll first create a few files we need to get started.

touch main.tf
touch variables.tf
mkdir digitalocean_githubpages
touch digitalocean_githubpages/main.tf
touch digitalocean_githubpages/variables.tf
touch digitalocean_githubpages/dns.tf
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}
provider "digitalocean" {
token = var.digitalocean_token
}
variable "digitalocean_token" {
type = string
description = "Digital Ocean Personal Access Token"
}
variable "tld" {
type = string
description = "The top level domain for the site"
}
variable "is_apex" {
type = bool
default = true
description = "Is this an apex domain? true or false"
}
variable "github_username" {
type = string
description = "The GitHub username hosting the pages: ?.github.io"
}

Defining DNS Records

With the basics setup, we’ll now define some basic variables we’ll need. In the digitalocean_githubpages/main.tf file add the following code:

locals {
github_pages_ipv4 = [
"185.199.108.153",
"185.199.109.153",
"185.199.110.153",
"185.199.111.153",
]
github_pages_ipv6 = [
"2606:50c0:8000::153",
"2606:50c0:8001::153",
"2606:50c0:8002::153",
"2606:50c0:8003::153",
]
}
resource "digitalocean_domain" "ghp_domain" {
name = var.tld
}
resource "digitalocean_record" "ghp_domain_a" {
count = length(local.github_pages_ipv4)
domain = var.tld
type = "A"
ttl = 60
name = "@"
value = local.github_pages_ipv4[count.index]
depends_on = [
digitalocean_domain.ghp_domain,
]
}
resource "digitalocean_record" "ghp_domain_aaaa" {
count = length(local.github_pages_ipv6)
domain = var.tld
type = "AAAA"
ttl = 60
name = "@"
value = local.github_pages_ipv6[count.index]
depends_on = [
digitalocean_domain.ghp_domain,
]
}
resource "digitalocean_record" "ghp_cname" {
count = "${var.is_apex == true ? 1 : 0}"
domain = var.tld
type = "CNAME"
ttl = 60
name = "www"
value = "${var.github_username}.github.io."
depends_on = [
digitalocean_domain.ghp_domain,
]
}

Implementing The Module

In the main.tf file, add the following code; in it we are defining two example sites with the module we just created.

module "example1" {
source = "./digitalocean_githubpages"
is_apex = true
tld = "example1.com"
github_username = "your_github_username"
digitalocean_token = var.digitalocean_token
}
module "example2" {
source = "./digitalocean_githubpages"
is_apex = true
tld = "example2.com"
github_username = "your_github_username"
digitalocean_token = var.digitalocean_token
}

Creating The Records

Now all that’s left to do is create our records. First, run terraform init to make sure everything is set up correctly.

terraform init
terraform plan
terraform apply

Final Thoughts

That’s it! That’s how I manage my DNS records in Digital Ocean for Github pages using terraform. Realistically, if you’re just setting up one personal site, it might make more sense for you to input the records manually. The advantages of terraform come from repeatability, and the ability to easily re-deploy everything if something goes wrong.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ryder Damen

Ryder Damen

Ryder Damen is a DevOps engineer by day, and also a DevOps engineer by night. He enjoys travelling, trying new things and making the web a stranger place.