Infrastructure as Code with Terraform

Geographically Diverse Failover in the Cloud

Tidiness Is Next To Godliness

Usually you would split up your code in a sensible manner between multiple files, so I’ll demonstrate how to do so and then display all the code in one file for ease afterward.

The simple file in Listing 1will go at the start of my single long file.The variables defined here should be in files of their own called something like variables.tf . The file defines two variables that I can change globally. I’ll use one value for the Time To Live (TTL) values for each of the DNS records. The aws_region config in this file isn’t relevant to Route53 because it’s a global AWS service. I’ve gone for Dublin, Ireland, in this case.

Listing 1: variables.tf

variable "aws_region" {
  description = "Preferred AWS region (Route 53 doesn't need this because it's Global)."
  default     = "eu-west-1"
}
 
variable "ttl" {
  description = "TTL record"
  default = 300
}

Outing

If I want to extract information from my code once it has been executed (for informational purposes or to respond programmatically to the results), I can use a separate file called outputs.tf that I’ll include at the end of my lengthy file:

output "ip" {
  value = "${aws_route53_record.chris_A_record.records}"
}

As you can see, I substitute value with a variable name. Anybody who is familiar with Terraform has almost certainly seen the error that says the formatting of a variable should be TYPE.NAME.ATTR . If you translate aws_route53_record.chris_A_record.records  correctly, it should make sense.

Enough Foutering

Now it’s time to put these two minuscule snippets at the top and bottom of my slightly longer file, which I call main.tf . In Listing 2, you can see the creation of an A record (which could be, e.g., www or blog or indeed anything arbitrary) and the MX records, which are where email for the domain name’s DNS queries are sent. The stanzas for the MX records and the A record should be self-explanatory.

Listing 2: main.tf

variable "aws_region" {
  description = "Preferred AWS region (Route 53 doesn't need this as it's Global)."
  default     = "eu-west-1"
}
 
variable "ttl" {
  description = "TTL record"
  default = 300
}
 
provider "aws" {
  region = "${var.aws_region}"
}
 
resource "aws_route53_zone" "chris_DNS_zone" {
  name = "devsecops.cc"
}
 
resource "aws_route53_record" "chris_A_record" {
  zone_id = "${aws_route53_zone.chris_DNS_zone.zone_id}"
  name    = "www"
  type    = "A"
 
  records = ["1.2.3.4"]
 
  ttl = "${var.ttl}"
}
 
resource "aws_route53_record" "chris_MX_record" {
  zone_id = "${aws_route53_zone.chris_DNS_zone.zone_id}"
  name    = ""
  type    = "MX"
  
  records = [
    "5 mail.devsecops.cc",
    "10 mail2.devsecops.cc",
    "15 mail3.devsecops.cc",
  ]
  
  ttl = "${var.ttl}"
}
 
output "ip" {
  value = "${aws_route53_record.chris_A_record.records}"
}

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus