Skip to content

Commit

Permalink
Update to use terraform 0.13 syntax, add an only A example.
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsunde committed Nov 9, 2020
1 parent e3844bb commit 4dfa4bf
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 66 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# 1.1
# 1.2.0

This role now requires terraform 0.13 syntax. It has also been updated to internally use a module per zone which will cause records to be recreated.

BREAKING:

* Now requires terraform => 0.13 to function.

# 1.1.0

BREAKING

* `domains` replaces `zone_name` and `zone_records` enabling us to set records across many zones.

# 1.0
# 1.0.0

Initial Release
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module "route53-alias" {
"mediapop.sg." = ["mediapop.sg", "www.mediapop.sg"]
}
alias_hosted_zone_id = "${aws_cloudfront_distribution.cloudfront.hosted_zone_id}"
alias_domain_name = "${aws_cloudfront_distribution.cloudfront.domain_name}"
alias_hosted_zone_id = aws_cloudfront_distribution.cloudfront.hosted_zone_id
alias_domain_name = aws_cloudfront_distribution.cloudfront.domain_name
}
```

Expand Down
33 changes: 33 additions & 0 deletions aws-route53-zone-alias/input.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
variable "zone" {
type = string
description = "A zone."
}

variable "hosts" {
type = set(string)
description = "A set of hosts."
}

variable "alias_hosted_zone_id" {
description = "The hosted_zone_id to alias"
}

variable "alias_domain_name" {
description = "The domain_name on the hosted_zone_id to alias"
}

variable "record_types" {
type = list(string)
description = "The types of records to set. Default is A and AAAA"
}

locals {
mappings = flatten([
for record_type in var.record_types : [
for host in var.hosts : {
type = record_type,
host = host
}
]
])
}
18 changes: 18 additions & 0 deletions aws-route53-zone-alias/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
data "aws_route53_zone" "zone" {
name = var.zone
}

resource "aws_route53_record" "record" {
count = length(local.mappings)

zone_id = data.aws_route53_zone.zone.id

name = local.mappings[count.index]["host"]
type = local.mappings[count.index]["type"]

alias {
name = var.alias_domain_name
zone_id = var.alias_hosted_zone_id
evaluate_target_health = false
}
}
4 changes: 2 additions & 2 deletions examples/cloudfront/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ This example just shows how to bind a group of domains to a CloudFront distribut
```hcl
module "alias" {
source = "mediapop/route53-alias/aws"
alias_hosted_zone_id = "${aws_cloudfront_distribution.redirect.hosted_zone_id}"
alias_domain_name = "${aws_cloudfront_distribution.redirect.domain_name}"
alias_hosted_zone_id = aws_cloudfront_distribution.redirect.hosted_zone_id
alias_domain_name = aws_cloudfront_distribution.redirect.domain_name
domains = {
"uatdomains.com." = [
Expand Down
34 changes: 8 additions & 26 deletions examples/cloudfront/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,9 @@ provider "aws" {
region = "ap-southeast-1"
}

resource "random_string" "redirect-bucket" {
length = 16
special = false
}

resource "aws_s3_bucket" "301" {
bucket = "${lower(random_string.redirect-bucket.result)}"
region = "us-east-1"

website {
redirect_all_requests_to = "https://mediapop.co"
}
}

resource "aws_cloudfront_distribution" "redirect" {
"origin" {
domain_name = "${aws_s3_bucket.301.website_endpoint}"
origin {
domain_name = "terraform-aws-route53-alias.uatdomains.com"
origin_id = "website"

custom_origin_config {
Expand All @@ -35,11 +21,7 @@ resource "aws_cloudfront_distribution" "redirect" {
enabled = true
is_ipv6_enabled = true

aliases = [
"terraform-aws-route53-alias.uatdomains.com",
]

"default_cache_behavior" {
default_cache_behavior {
allowed_methods = [
"HEAD",
"GET",
Expand All @@ -50,10 +32,10 @@ resource "aws_cloudfront_distribution" "redirect" {
"GET",
]

"forwarded_values" {
forwarded_values {
query_string = false

"cookies" {
cookies {
forward = "none"
}
}
Expand All @@ -71,15 +53,15 @@ resource "aws_cloudfront_distribution" "redirect" {
}
}

"viewer_certificate" {
viewer_certificate {
cloudfront_default_certificate = true
}
}

module "alias" {
source = "../../"
alias_hosted_zone_id = "${aws_cloudfront_distribution.redirect.hosted_zone_id}"
alias_domain_name = "${aws_cloudfront_distribution.redirect.domain_name}"
alias_hosted_zone_id = aws_cloudfront_distribution.redirect.hosted_zone_id
alias_domain_name = aws_cloudfront_distribution.redirect.domain_name

domains = {
"uatdomains.com." = [
Expand Down
19 changes: 19 additions & 0 deletions examples/only-ipv4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Only IPv4 Example

This example shows overriding record_types and only setting A records pointing to an S3 bucket.

```hcl
module "alias" {
source = "mediapop/route53-alias/aws"
alias_hosted_zone_id = aws_s3_bucket.bucket.hosted_zone_id
alias_domain_name = aws_s3_bucket.bucket.bucket_domain_name
record_types = ["A"]
domains = {
"uatdomains.com." = [
"terraform-aws-route53-alias-ipv4.uatdomains.com",
]
}
}
```
24 changes: 24 additions & 0 deletions examples/only-ipv4/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
provider "aws" {
region = "ap-southeast-1"
}

resource "aws_s3_bucket" "bucket" {
# TODO Why can't I use the bucket_regional_domain_name as the alias_domain_name?
website {
index_document = "index.html"
}
}

module "alias" {
source = "../../"
alias_hosted_zone_id = aws_s3_bucket.bucket.hosted_zone_id
alias_domain_name = aws_s3_bucket.bucket.website_domain

record_types = ["A"]

domains = {
"uatdomains.com." = [
"terraform-aws-route53-alias-ipv4.uatdomains.com",
]
}
}
11 changes: 2 additions & 9 deletions input.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
variable "domains" {
type = "map"
type = map(list(string))
description = "A map {\"zone.com.\" = [\"zone.com\",\"www.zone.com\"],\"foo.com\" = [\"foo.com\"] } of domains."
}

Expand All @@ -12,18 +12,11 @@ variable "alias_domain_name" {
}

variable "record_types" {
type = "list"
type = list(string)
description = "The types of records to set. Default is A and AAAA"

default = [
"A",
"AAAA",
]
}

locals {
zones = "${keys(var.domains)}"
records = "${keys(transpose(var.domains))}"
record_map = "${transpose(var.domains)}"
type_count = "${length(var.record_types)}"
}
33 changes: 8 additions & 25 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
data "aws_route53_zone" "zone" {
count = "${length(local.zones)}"
name = "${local.zones[count.index]}"
}

resource "aws_route53_record" "record" {
count = "${length(local.records) * length(var.record_types)}"

// We lookup the zone name from the record, then the zone_id from zone_name
zone_id = "${
element(matchkeys(
data.aws_route53_zone.zone.*.id,
data.aws_route53_zone.zone.*.name,
local.record_map[element(local.records, floor(count.index / local.type_count))]
), 0)
}"

name = "${element(local.records, floor(count.index / local.type_count))}"
type = "${element(var.record_types, count.index % local.type_count)}"

alias {
name = "${var.alias_domain_name}"
zone_id = "${var.alias_hosted_zone_id}"
evaluate_target_health = false
}
module "zone_alias" {
for_each = var.domains
source = "./aws-route53-zone-alias"
alias_domain_name = var.alias_domain_name
alias_hosted_zone_id = var.alias_hosted_zone_id
zone = each.key
hosts = each.value
record_types = var.record_types
}
8 changes: 8 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
required_version = ">= 0.13"
}

0 comments on commit 4dfa4bf

Please sign in to comment.