Skip to content

Commit

Permalink
add cname
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Mar 24, 2023
1 parent a20b257 commit aa055bb
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 5 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Versions

* v1.0.0 -- {{ yyyy-mm-dd }}
- initial creation
* 1.0.0 -- 2023-03-24
- initial creation with cname

1 change: 1 addition & 0 deletions cname/availabilty_zones.tf
1 change: 1 addition & 0 deletions cname/data.tf
1 change: 1 addition & 0 deletions cname/defaults.tf
56 changes: 56 additions & 0 deletions cname/main.tf
Original file line number Diff line number Diff line change
@@ -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))]
}
1 change: 1 addition & 0 deletions cname/prefixes.tf
1 change: 1 addition & 0 deletions cname/variables.common.availability_zones.tf
1 change: 1 addition & 0 deletions cname/variables.common.tf
1 change: 1 addition & 0 deletions cname/variables.tf
1 change: 1 addition & 0 deletions cname/version.tf
1 change: 1 addition & 0 deletions cname/versions.tf
17 changes: 17 additions & 0 deletions common/defaults.tf
Original file line number Diff line number Diff line change
@@ -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 = ""
}
}
}
46 changes: 46 additions & 0 deletions common/entries.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}
56 changes: 56 additions & 0 deletions common/variables.tf
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion common/version.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
locals {
_module_version = "0.0.0"
_module_version = "1.0.0"
}
4 changes: 2 additions & 2 deletions common/versions.tf
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit aa055bb

Please sign in to comment.