Skip to content

Instantly share code, notes, and snippets.

@thegedge
Last active August 29, 2015 14:27
Show Gist options
  • Save thegedge/62667ff4453727ee6585 to your computer and use it in GitHub Desktop.
Save thegedge/62667ff4453727ee6585 to your computer and use it in GitHub Desktop.
Terraform zone module
resource "terraform_remote_state" "region" {
backend = "s3"
config {
bucket = "foo"
key = "bar"
region = "us-east-1"
}
}
module "sub1_zone" {
source = "../zone"
domain = "sub1.${terraform_remote_state.region.output.domain}"
parent_zone_id = "${terraform_remote_state.region.output.zone_id}"
delegation_set_id = "${terraform_remote_state.region.output.delegation_set_id}"
name_servers = "${terraform_remote_state.region.output.name_servers}"
}
module "sub2_zone" {
source = "../zone"
domain = "sub2.${module.sub1_zone.domain}"
parent_zone_id = "${module.sub1_zone.zone_id}"
delegation_set_id = "${module.sub1_zone.delegation_set_id}"
name_servers = "${module.sub1_zone.name_servers}"
}
// Delegation set to use for subzones of this zone
resource "aws_route53_delegation_set" "sub" {
reference_name = "sub.${var.domain}"
}
// Build the zone
resource "aws_route53_zone" "main" {
delegation_set_id = "${var.delegation_set_id}"
name = "${var.domain}"
}
// Add NS records to the parent zone
resource "aws_route53_record" "parent-ns" {
zone_id = "${var.parent_zone_id}"
name = "${var.domain}"
type = "NS"
ttl = "${var.name_server_ttl}"
records = ["${split(",", var.name_servers)}"]
}
output "domain" {
value = "${var.domain}"
}
output "zone_id" {
value = "${aws_route53_zone.main.id}"
}
// Delegation set to use for subzones
output "delegation_set_id" {
value = "${aws_route53_delegation_set.sub.id}"
}
// Name servers for the subzone delegation set
output "name_servers" {
// join doesn't work with lists :(
value = "${aws_route53_delegation_set.sub.name_servers.0},${aws_route53_delegation_set.sub.name_servers.1},${aws_route53_delegation_set.sub.name_servers.2},${aws_route53_delegation_set.sub.name_servers.3}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment