diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ce3418..e9cbc4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ # Versions -* v1.0.0 -- {{ yyyy-mm-dd }} - - initial creation +* 1.0.0 -- 2023-03-24 + - initial creation with cname diff --git a/cname/availabilty_zones.tf b/cname/availabilty_zones.tf new file mode 120000 index 0000000..00a240c --- /dev/null +++ b/cname/availabilty_zones.tf @@ -0,0 +1 @@ +../common/availabilty_zones.tf \ No newline at end of file diff --git a/cname/data.tf b/cname/data.tf new file mode 120000 index 0000000..995624d --- /dev/null +++ b/cname/data.tf @@ -0,0 +1 @@ +../common/data.tf \ No newline at end of file diff --git a/cname/defaults.tf b/cname/defaults.tf new file mode 120000 index 0000000..a5556ac --- /dev/null +++ b/cname/defaults.tf @@ -0,0 +1 @@ +../common/defaults.tf \ No newline at end of file diff --git a/cname/main.tf b/cname/main.tf new file mode 100644 index 0000000..29c414f --- /dev/null +++ b/cname/main.tf @@ -0,0 +1,56 @@ +locals { + account_id = var.account_id != "" ? var.account_id : data.aws_caller_identity.current.account_id + account_environment = data.aws_arn.current.partition == "aws-us-gov" ? "gov" : "ew" + + base_tags = { + "boc:tf_module_version" = local._module_version + "boc:created_by" = "terraform" + } +} + +locals { + name_parts = split(".", var.name) + host_name = var.name_parts[0] + zone = trimprefix(var.name, format("%v.", local.host_name)) + private_zone = local.account_environment == "gov" ? true : var.private_zone + rr_type = uppercase(var.type) + default_enable_ptr = lookup(local._defaults.enable_ptr, var.type, false) + default_heritage_prefix = lookup(local._defaults.heritage_prefix, var.type, "") != "" ? format("%v.", lookup(local._defaults.heritage_prefix, var.type)) : "" + is_cname = var.type == "cname" + base_heritage_tags = [ + format("heritage=%v", local._defaults.heritage_label), + format("%v/account_id=%v", local._defaults.heritage_label, data.aws_caller_identity.current.account_id), + format("%v/region=%v", local._defaults.heritage_label, local.region), + format("%v/create_time=%d", local._defaults.heritage_label, time_static.timestamp.unix) + ] + heritage_tags = [for k, v in var.heritage_tags : format("%v/%v", local._defaults.heritage_label, k)] +} + + +# if the zone grab fails here, it either doesn't exist or it is not associated with this VPC +data "aws_route53_zone" "zone" { + name = local.zone + private_zone = local.private_zone +} + +resource "time_static" "timestamp" {} + +resource "aws_route53_record" "entry" { + count = local.is_cname ? 1 : 0 + zone_id = data.aws_route53_zone.zone.zone_id + name = var.name + type = local.rr_type + ttl = var.ttl + records = var.values +} + + +resource "aws_route53_record" "entry_heritage" { + count = var.enable_heritage ? 1 : 0 + zone_id = data.aws_route53_zone.zone.zone_id + name = format("%v%v", aws_route53_record.entry.name) + + type = "TXT" + ttl = var.ttl + records = [join(",", concat(local.base_heritage_tags, local.heritage_tags))] +} diff --git a/cname/prefixes.tf b/cname/prefixes.tf new file mode 120000 index 0000000..7e265d5 --- /dev/null +++ b/cname/prefixes.tf @@ -0,0 +1 @@ +../common/prefixes.tf \ No newline at end of file diff --git a/cname/variables.common.availability_zones.tf b/cname/variables.common.availability_zones.tf new file mode 120000 index 0000000..dca20a3 --- /dev/null +++ b/cname/variables.common.availability_zones.tf @@ -0,0 +1 @@ +../common/variables.common.availability_zones.tf \ No newline at end of file diff --git a/cname/variables.common.tf b/cname/variables.common.tf new file mode 120000 index 0000000..7439ed8 --- /dev/null +++ b/cname/variables.common.tf @@ -0,0 +1 @@ +../common/variables.common.tf \ No newline at end of file diff --git a/cname/variables.tf b/cname/variables.tf new file mode 120000 index 0000000..72202b3 --- /dev/null +++ b/cname/variables.tf @@ -0,0 +1 @@ +../common/variables.tf \ No newline at end of file diff --git a/cname/version.tf b/cname/version.tf new file mode 120000 index 0000000..b83c5b7 --- /dev/null +++ b/cname/version.tf @@ -0,0 +1 @@ +../common/version.tf \ No newline at end of file diff --git a/cname/versions.tf b/cname/versions.tf new file mode 120000 index 0000000..41bb22f --- /dev/null +++ b/cname/versions.tf @@ -0,0 +1 @@ +../common/versions.tf \ No newline at end of file diff --git a/common/defaults.tf b/common/defaults.tf index 8247df4..1c5717f 100644 --- a/common/defaults.tf +++ b/common/defaults.tf @@ -1,4 +1,21 @@ locals { _defaults = { + enable_ptr = { + cname = false + a = false + aaaa = false + txt = false + host = true + ptr = true + } + heritage_label = "terraform" + heritage_prefix = { + cname = "_txt" + a = "" + aaaa = "" + txt = "_txt" + host = "" + ptr = "" + } } } diff --git a/common/entries.tf b/common/entries.tf new file mode 100644 index 0000000..1335672 --- /dev/null +++ b/common/entries.tf @@ -0,0 +1,46 @@ +data "aws_route53_zone" "lakefront" { + name = var.vpc_domain_name + private_zone = true +} + +resource "aws_route53_record" "lakefront_cname" { + zone_id = data.aws_route53_zone.lakefront.zone_id + name = format("%v.%v", local.app_shortname, data.aws_route53_zone.lakefront.name) + type = "CNAME" + ttl = "900" + records = [format("%v.execute-api.%v.amazonaws.com", data.aws_api_gateway_rest_api.lakefront.id, local.region)] +} + +resource "time_static" "lakefront_txt" {} + +resource "aws_route53_record" "lakefront_txt" { + zone_id = data.aws_route53_zone.lakefront.zone_id + name = format("_txt.%v", aws_route53_record.lakefront_cname.name) + + type = "TXT" + ttl = "900" + records = [format("heritage=terraform,terraform/account_id=%v,terraform/region=%v,terraform/terraform/create_time=%d", + data.aws_caller_identity.current.account_id, local.region, time_static.lakefront_txt.unix)] +} + +variable "zone" { + description = "DNS Zone into which to create the record. By default, it will extract this from the name" + type = string + default = null +} + +variable "name" { + description = "FQDN DNS name to create. If the zone (everything after the first dot) does not exist or is not associated to the VPC, this creation will fail." + type = string +} + +variable "record_type" { + description = "Type of DNS record to create: cname, a, aaaa, ptr, txt, host (ptr and a and/or aaaa)" + type = string + default = "host" + + validation { + condition = contains(["cname", "a", "aaaa", "ptr", "txt", "host"], lowercase(var.record_type)) + error_message = "The record type must be one of: cname, a, aaaa, ptr, txt, or host" + } +} diff --git a/common/variables.tf b/common/variables.tf new file mode 100644 index 0000000..ba81cb9 --- /dev/null +++ b/common/variables.tf @@ -0,0 +1,56 @@ +# variable "zone" { +# description = "DNS Zone into which to create the record. By default, it will extract this from the name" +# type = string +# default = null +# } + +variable "name" { + description = "FQDN DNS name to create. If the zone (everything after the first dot) does not exist or is not associated to the VPC, this creation will fail." + type = string + + validation { + condition = length(split(".", var.name)) > 1 + error_message = "Invalid name, must use FQDN with a hostname and domain name" + } +} + +variable "record_type" { + description = "Type of DNS record to create: cname, a, aaaa, ptr, txt, host (ptr and a and/or aaaa)" + type = string + default = "host" + + validation { + condition = contains(["cname", "a", "aaaa", "ptr", "txt", "host"], var.record_type) + error_message = "The record type must be one of: cname, a, aaaa, ptr, txt, or host" + } +} + +variable "enable_ptr" { + description = "Flag to enable or disable creation of a PTR record. Used for type ptr and host" + type = bool + default = null +} + +variable "enable_heritage" { + description = "Flag to enable or disable creation a TXT record for heritage. For CNAMEs, it uses a prefix of _txt on the name" + type = bool + default = true +} + +variable "heritage_tags" { + description = "Map of key/value pairs to set into the heritage. These should be static so as not to update the heritage TXT record frequently" + type = map(string) + default = {} +} + +variable "private_zone" { + description = "Flag indicating public or private zone. Assumes private by default, and can set public only for non-gov region" + type = bool + default = true +} + +variable "ttl" { + description = "DNS RR Time To Live (ttl). Default 900s (15m)." + type = number + default = 900 +} diff --git a/common/version.tf b/common/version.tf index a0cd862..fa2705b 100644 --- a/common/version.tf +++ b/common/version.tf @@ -1,3 +1,3 @@ locals { - _module_version = "0.0.0" + _module_version = "1.0.0" } diff --git a/common/versions.tf b/common/versions.tf index 4ba10ce..fc5afe7 100644 --- a/common/versions.tf +++ b/common/versions.tf @@ -1,9 +1,9 @@ terraform { + required_version = ">= 1.0" required_providers { aws = { source = "hashicorp/aws" - version = ">= 3.66.0" + version = ">= 4" } } -# required_version = ">= 0.13" }