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

Python Script to Arrange Files

HearRo Using Self-Sovereign Identity to Smooth the Customer Service Experience

Explain the skills, roles, and responsibilities of a DevOps engineer?

Shield (XSH) Update #6 — Details: Titanium Airdrop

Introducing GYSR V2

Flutter Book App part 4: From demo to real app.

ZULU MODULE IN PYTHON

Complete Web Development Resources 2021 ALL IN ONE! ( ALL YOU NEED )

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

What is Terraform?

How to use Ansible AWS EC2 Dynamic Inventory Plugin

How to run docker containers on AWS?

Provisioning a Jenkins Server on AWS Using Terraform