From 4dfa4bf7215fa05c386a01591f01e81e511c47ae Mon Sep 17 00:00:00 2001 From: Kit Sunde Date: Mon, 9 Nov 2020 20:38:37 +0800 Subject: [PATCH] Update to use terraform 0.13 syntax, add an only A example. --- CHANGELOG.md | 12 ++++++++++-- README.md | 4 ++-- aws-route53-zone-alias/input.tf | 33 ++++++++++++++++++++++++++++++++ aws-route53-zone-alias/main.tf | 18 +++++++++++++++++ examples/cloudfront/README.md | 4 ++-- examples/cloudfront/main.tf | 34 ++++++++------------------------- examples/only-ipv4/README.md | 19 ++++++++++++++++++ examples/only-ipv4/main.tf | 24 +++++++++++++++++++++++ input.tf | 11 ++--------- main.tf | 33 ++++++++------------------------ versions.tf | 8 ++++++++ 11 files changed, 134 insertions(+), 66 deletions(-) create mode 100644 aws-route53-zone-alias/input.tf create mode 100644 aws-route53-zone-alias/main.tf create mode 100644 examples/only-ipv4/README.md create mode 100644 examples/only-ipv4/main.tf create mode 100644 versions.tf diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ad32ae..a4ef0a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index c1579b4..bb38cdd 100644 --- a/README.md +++ b/README.md @@ -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 } ``` diff --git a/aws-route53-zone-alias/input.tf b/aws-route53-zone-alias/input.tf new file mode 100644 index 0000000..ca4d30c --- /dev/null +++ b/aws-route53-zone-alias/input.tf @@ -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 + } + ] + ]) +} diff --git a/aws-route53-zone-alias/main.tf b/aws-route53-zone-alias/main.tf new file mode 100644 index 0000000..8d2c774 --- /dev/null +++ b/aws-route53-zone-alias/main.tf @@ -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 + } +} diff --git a/examples/cloudfront/README.md b/examples/cloudfront/README.md index cea27eb..bd69aea 100644 --- a/examples/cloudfront/README.md +++ b/examples/cloudfront/README.md @@ -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." = [ diff --git a/examples/cloudfront/main.tf b/examples/cloudfront/main.tf index 612b6ff..f6f3e0e 100644 --- a/examples/cloudfront/main.tf +++ b/examples/cloudfront/main.tf @@ -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 { @@ -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", @@ -50,10 +32,10 @@ resource "aws_cloudfront_distribution" "redirect" { "GET", ] - "forwarded_values" { + forwarded_values { query_string = false - "cookies" { + cookies { forward = "none" } } @@ -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." = [ diff --git a/examples/only-ipv4/README.md b/examples/only-ipv4/README.md new file mode 100644 index 0000000..b3e51ba --- /dev/null +++ b/examples/only-ipv4/README.md @@ -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", + ] + } +} +``` diff --git a/examples/only-ipv4/main.tf b/examples/only-ipv4/main.tf new file mode 100644 index 0000000..6572a8e --- /dev/null +++ b/examples/only-ipv4/main.tf @@ -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", + ] + } +} diff --git a/input.tf b/input.tf index bfd2076..6846fbf 100644 --- a/input.tf +++ b/input.tf @@ -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." } @@ -12,7 +12,7 @@ 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 = [ @@ -20,10 +20,3 @@ variable "record_types" { "AAAA", ] } - -locals { - zones = "${keys(var.domains)}" - records = "${keys(transpose(var.domains))}" - record_map = "${transpose(var.domains)}" - type_count = "${length(var.record_types)}" -} diff --git a/main.tf b/main.tf index e3ab3a2..e29bce4 100644 --- a/main.tf +++ b/main.tf @@ -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 } diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..1d23dfe --- /dev/null +++ b/versions.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + } + } + required_version = ">= 0.13" +}