Skip to content

Commit 5fc8cab

Browse files
authored
[root-dns] Use remote state for nameservers (#56)
* Use remote state for nameservers
1 parent 8812b3b commit 5fc8cab

File tree

15 files changed

+195
-52
lines changed

15 files changed

+195
-52
lines changed

aws/accounts/audit.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ resource "aws_organizations_account" "audit" {
1919
output "audit_account_arn" {
2020
value = "${aws_organizations_account.audit.arn}"
2121
}
22+
23+
output "audit_account_id" {
24+
value = "${aws_organizations_account.audit.id}"
25+
}
26+
27+
output "audit_organization_account_access_role" {
28+
value = "arn:aws:iam::${aws_organizations_account.audit.id}:role/OrganizationAccountAccessRole"
29+
}

aws/accounts/dev.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ resource "aws_organizations_account" "dev" {
1919
output "dev_account_arn" {
2020
value = "${aws_organizations_account.dev.arn}"
2121
}
22+
23+
output "dev_account_id" {
24+
value = "${aws_organizations_account.dev.id}"
25+
}
26+
27+
output "dev_organization_account_access_role" {
28+
value = "arn:aws:iam::${aws_organizations_account.dev.id}:role/OrganizationAccountAccessRole"
29+
}

aws/accounts/prod.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ resource "aws_organizations_account" "prod" {
1919
output "prod_account_arn" {
2020
value = "${aws_organizations_account.prod.arn}"
2121
}
22+
23+
output "prod_account_id" {
24+
value = "${aws_organizations_account.prod.id}"
25+
}
26+
27+
output "prod_organization_account_access_role" {
28+
value = "arn:aws:iam::${aws_organizations_account.prod.id}:role/OrganizationAccountAccessRole"
29+
}

aws/accounts/staging.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ resource "aws_organizations_account" "staging" {
1919
output "staging_account_arn" {
2020
value = "${aws_organizations_account.staging.arn}"
2121
}
22+
23+
output "staging_account_id" {
24+
value = "${aws_organizations_account.staging.id}"
25+
}
26+
27+
output "staging_organization_account_access_role" {
28+
value = "arn:aws:iam::${aws_organizations_account.staging.id}:role/OrganizationAccountAccessRole"
29+
}

aws/accounts/testing.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,11 @@ resource "aws_organizations_account" "testing" {
1919
output "testing_account_arn" {
2020
value = "${aws_organizations_account.testing.arn}"
2121
}
22+
23+
output "testing_account_id" {
24+
value = "${aws_organizations_account.testing.id}"
25+
}
26+
27+
output "testing_organization_account_access_role" {
28+
value = "arn:aws:iam::${aws_organizations_account.testing.id}:role/OrganizationAccountAccessRole"
29+
}

aws/root-dns/main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,19 @@ variable "aws_assume_role_arn" {
88
type = "string"
99
}
1010

11+
variable "namespace" {}
12+
1113
provider "aws" {
1214
assume_role {
1315
role_arn = "${var.aws_assume_role_arn}"
1416
}
1517
}
18+
19+
data "terraform_remote_state" "root" {
20+
backend = "s3"
21+
22+
config {
23+
bucket = "${var.namespace}-root-terraform-state"
24+
key = "accounts/terraform.tfstate"
25+
}
26+
}

aws/root-dns/ns/main.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module "label" {
2+
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3"
3+
namespace = "${var.namespace}"
4+
stage = "${var.stage}"
5+
name = "${var.name}"
6+
delimiter = "${var.delimiter}"
7+
attributes = "${var.attributes}"
8+
tags = "${var.tags}"
9+
}
10+
11+
data "terraform_remote_state" "stage" {
12+
backend = "s3"
13+
14+
# This assumes stage is using a `terraform-aws-tfstate-backend`
15+
# https://github.com/cloudposse/terraform-aws-tfstate-backend
16+
config {
17+
role_arn = "${var.role_arn}"
18+
bucket = "${module.label.id}"
19+
key = "${var.key}"
20+
}
21+
}
22+
23+
locals {
24+
name_servers = "${data.terraform_remote_state.stage.name_servers}"
25+
}
26+
27+
resource "aws_route53_record" "dns_zone_ns" {
28+
count = "${signum(length(local.name_servers))}"
29+
zone_id = "${var.zone_id}"
30+
name = "${var.stage}"
31+
type = "NS"
32+
ttl = "${var.ttl}"
33+
records = ["${local.name_servers}"]
34+
}

aws/root-dns/ns/outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "stage" {
2+
description = "Name of the subaccount corresponding to the name servers"
3+
value = "${var.stage}"
4+
}
5+
6+
output "name_servers" {
7+
description = "Name servers for the account's delegated DNS zone"
8+
value = "${local.name_servers}"
9+
}

aws/root-dns/ns/variables.tf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
variable "namespace" {
2+
type = "string"
3+
description = "Namespace (e.g. `eg` or `example`)"
4+
}
5+
6+
variable "stage" {
7+
type = "string"
8+
description = "Stage (e.g. `prod`, `dev`, `staging`)"
9+
}
10+
11+
variable "name" {
12+
type = "string"
13+
default = "terraform"
14+
description = "Name (e.g. `app` or `cluster`)"
15+
}
16+
17+
variable "delimiter" {
18+
type = "string"
19+
default = "-"
20+
description = "Delimiter to be used between `namespace`, `stage`, `name`, and `attributes`"
21+
}
22+
23+
variable "attributes" {
24+
type = "list"
25+
default = ["state"]
26+
description = "Additional attributes (e.g. `state`)"
27+
}
28+
29+
variable "tags" {
30+
type = "map"
31+
default = {}
32+
description = "Additional tags (e.g. map(`BusinessUnit`,`XYZ`)"
33+
}
34+
35+
variable "role_arn" {
36+
description = "The role to be assumed in the subaccount"
37+
}
38+
39+
variable "zone_id" {
40+
description = "DNS zone to update"
41+
}
42+
43+
variable "ttl" {
44+
description = "Default TTL for the NS records"
45+
default = "30"
46+
}
47+
48+
variable "key" {
49+
description = "Object in the remote state backend containing the state of `account-dns`"
50+
default = "account-dns/terraform.tfstate"
51+
}

aws/root-dns/parent-audit-ns.tf

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
variable "audit_name_servers" {
2-
type = "list"
1+
module "audit" {
2+
source = "ns"
3+
role_arn = "${data.terraform_remote_state.root.audit_organization_account_access_role}"
4+
namespace = "${var.namespace}"
5+
stage = "audit"
6+
zone_id = "${aws_route53_zone.parent_dns_zone.zone_id}"
37
}
48

5-
resource "aws_route53_record" "audit_dns_zone_ns" {
6-
count = "${signum(length(var.audit_name_servers))}"
7-
zone_id = "${aws_route53_zone.parent_dns_zone.zone_id}"
8-
name = "audit"
9-
type = "NS"
10-
ttl = "30"
11-
records = ["${var.audit_name_servers}"]
9+
output "audit_name_servers" {
10+
value = "${module.audit.name_servers}"
1211
}

0 commit comments

Comments
 (0)