diff --git a/README.md b/README.md index a5dd089..e79618b 100644 --- a/README.md +++ b/README.md @@ -1 +1,55 @@ -# terraform-aws-route53 \ No newline at end of file +# terraform-aws-route53 +Terraform module to create route 53 hosted zones. + +## Usages + +### Example for public zone: + +```hcl +module "public_zone_name" { + source = "git@github.com:nimbux911/terraform-aws-route53/tags/v1.0" + zone_name = "name." + + record_set = [ { + record_name = "name." + type = "A" // Valid values are A, AAAA, CAA, CNAME, MX, NAPTR, NS, PTR, SOA, SPF, SRV and TXT + ttl = 300 + records = ["IP"] + } ] + + tags = { + Environment = var.environment + Purpose = "Api endpoint" + } +} +``` + +### Example for private zone: + +```hcl +module "private_zone_name" { + source = "git@github.com:nimbux911/terraform-aws-route53/tags/1.0" + zone_name = "name." + vpc_id = "vpc-id" + record_set = [ { + record_name = "name." + type = "A" // Valid values are A, AAAA, CAA, CNAME, MX, NAPTR, NS, PTR, SOA, SPF, SRV and TXT + ttl = 300 + records = ["IP"] + } ] + + tags = { + Environment = var.environment + Purpouse = "Api endpoint" + } +} +``` + + +## Outputs + +| Name | Description | +|------|-------------| +| zone_id | The ID of Hosted Zone | +| name_servers | A list of name servers in associated (or default) delegation set | +| zone_name | Route53 Hosted Zone domain name | \ No newline at end of file diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..08fa360 --- /dev/null +++ b/main.tf @@ -0,0 +1,56 @@ +locals { + zone_id = concat( + aws_route53_zone.private.*.zone_id, + aws_route53_zone.public.*.zone_id, + )[0] + name_servers = concat( + aws_route53_zone.private.*.name_servers, + aws_route53_zone.public.*.name_servers, + )[0] + # convert from list to map with unique keys + record_set = { for rs in var.record_set : join(" ", compact(["${rs.name} ${rs.type}", lookup(rs, "set_identifier", "")])) => rs } +} + +resource "aws_route53_zone" "public" { + count = var.vpc_id == "" ? 1 : 0 + name = var.zone_name + comment = var.comment + force_destroy = var.force_destroy + tags = var.tags +} + +resource "aws_route53_zone" "private" { + count = var.vpc_id != "" ? 1 : 0 + name = var.zone_name + comment = var.comment + force_destroy = var.force_destroy + vpc { + vpc_id = var.vpc_id + } + tags = var.tags +} + +# resource "aws_route53_zone_association" "secondary" { +# count = var.vpc_id != "" ? 1 : 0 +# zone_id = aws_route53_zone.private[count.index].zone_id +# vpc_id = var.vpc_id +# } + +resource "aws_route53_record" "record_set" { + for_each = local.record_set + zone_id = local.zone_id + name = each.value.name + type = each.value.type + ttl = lookup(each.value, "ttl", null) + records = lookup(each.value, "records", null) + + dynamic "alias" { + for_each = length(keys(lookup(each.value, "alias", {}))) == 0 ? [] : [true] + + content { + name = each.value.alias.dns_name + zone_id = each.value.alias.zone_id + evaluate_target_health = lookup(each.value.alias, "evaluate_target_health", false) + } + } +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..1d35479 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,12 @@ +output "zone_id" { + description = "Private zone ID created" + value = local.zone_id +} +output "name_servers" { + description = "A list of name servers in associated (or default) delegation set" + value = local.name_servers +} +output "zone_name" { + description = "Route53 Hosted Zone domain name" + value = var.zone_name +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..a64f829 --- /dev/null +++ b/variables.tf @@ -0,0 +1,36 @@ +variable "zone_name" { + description = "This is the name of the hosted zone" + type = string + default = "" +} + +variable "vpc_id" { + description = "ID of the VPC to associate" + type = string + default = "" +} + +variable "tags" { + description = "A map of tags to add to IAM role resources" + type = map(string) + default = {} +} + +variable "record_set" { + description = "List of maps of DNS records" + type = any + default = [] +} + +variable "comment" { + description = "A comment for the hosted zone" + type = string + default = "" +} + +variable "force_destroy" { + description = "Whether to destroy all records (possibly managed outside of Terraform) in the zone when destroying the zone" + type = bool + default = false +} + diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..2606a5a --- /dev/null +++ b/versions.tf @@ -0,0 +1,4 @@ + +terraform { + required_version = ">= 0.13" +}