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

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

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

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

terraform init
terraform plan
terraform apply

Final Thoughts

--

--

--

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.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Practical Guide to Surviving AWS SAM

Best Hosting apex hosting in 2021 for all Minecraft servers

Best Hosting apex hosting in 2021 for all Minecraft servers

Double down on Red Pill Analytics at Collaborate 2018

Enabling Integration, Collaboration, and Innovation with the Strava API

How to approach a Backtracking Problem — Sudoku example

Backtracking paradigm on a high level

The subtle Art of Debugging

Kubernetes, Java and GC walks into a bar

Deploying Dash to Google Cloud Run in 5 minutes

Cloud run and plotly dash logo

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.

More from Medium

Get Cloudflare IP Address Ranges in Terraform Without Authentication

Ubiquiti EdgeOS Terraform Provider

My Cloud Native Hackathon Experience from enforcing policy using Datree tool to configuring CIVO…

DevOps Girls 2021 in Review