Skip to content

Instantly share code, notes, and snippets.

@panbanda

panbanda/cloudflare.tf

Last active Jan 22, 2019
Embed
What would you like to do?
Setup a domain on CloudFlare + Google MX using Terraform
variable "email" {}
variable "token" {}
variable "domain" {}
provider "cloudflare" {
email = "${var.email}"
token = "${var.token}"
}
# Setup the root domain
resource "cloudflare_record" "main" {
domain = "${var.domain}"
name = "@"
value = "192.168.0.11"
type = "A"
proxied = true
}
# Setup the www CNAME for your domain
resource "cloudflare_record" "main-www" {
domain = "${var.domain}"
name = "www"
value = "${var.domain}"
type = "CNAME"
proxied = true
}
# Setting up Google MX
resource "cloudflare_record" "google-mx-record-aspmx" {
domain = "${var.domain}"
name = "@"
value = "aspmx.l.google.com"
type = "MX"
priority = 1
}
resource "cloudflare_record" "google-mx-record-alt1" {
domain = "${var.domain}"
name = "@"
value = "alt1.aspmx.l.google.com"
type = "MX"
priority = 5
}
resource "cloudflare_record" "google-mx-record-alt2" {
domain = "${var.domain}"
name = "@"
value = "alt2.aspmx.l.google.com"
type = "MX"
priority = 5
}
resource "cloudflare_record" "google-mx-record-alt3" {
domain = "${var.domain}"
name = "@"
value = "alt3.aspmx.l.google.com"
type = "MX"
priority = 10
}
resource "cloudflare_record" "google-mx-record-alt4" {
domain = "${var.domain}"
name = "@"
value = "alt4.aspmx.l.google.com"
type = "MX"
priority = 10
}
# email = cloudflare email address
# token = cloudflare api token from profile page
# domain = the domain you're wanting to update on cloudflare
terraform apply -var email=jon@awesome.io -var token=111111111111111111111 -var domain=myawesomedomain.com
@dsifford

This comment has been minimized.

Copy link

@dsifford dsifford commented Aug 3, 2018

Google MX interpolated using count

# Mail Exchange Records: Gsuite / Gmail
resource "cloudflare_record" "mx-gsuite" {
  count    = 5
  domain   = "${var.domain}"
  name     = "@"
  type     = "MX"
  value    = "${count.index == 0 ? "" : "alt${count.index + 1}."}aspmx.l.google.com"
  priority = "${abs(element(list("1", "5", "5", "10", "10"), count.index))}"
}
@panbanda

This comment has been minimized.

Copy link
Owner Author

@panbanda panbanda commented Jan 22, 2019

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.