main.tf 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. locals {
  2. first_vpc = var.vpcs[0]
  3. remaining_vpcs = [for vpc in var.vpcs : vpc if vpc != local.first_vpc]
  4. }
  5. # debug
  6. #output remaining_vpcs {
  7. # value = local.remaining_vpcs
  8. #}
  9. # Create the private zones
  10. resource "aws_route53_zone" "private" {
  11. name = var.dns_info["private"]["zone"]
  12. tags = merge(local.standard_tags, var.tags)
  13. vpc {
  14. vpc_id = local.first_vpc
  15. }
  16. # For the rationale here, see the notes at:
  17. # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_zone_association
  18. #
  19. # tldr; We can't create without an association, but we can't add associations without changing this record.
  20. # So we add one, and then we add it again and ignore any further changes.
  21. lifecycle {
  22. ignore_changes = [vpc]
  23. }
  24. }
  25. resource "aws_route53_record" "dnstest" {
  26. zone_id = aws_route53_zone.private.id
  27. name = "dnstest"
  28. type = "A"
  29. ttl = "300"
  30. # Non-routable Test IP: https://tools.ietf.org/html/rfc5737
  31. records = ["10.10.10.10"]
  32. }
  33. resource "aws_route53_zone_association" "associations" {
  34. for_each = toset(local.remaining_vpcs)
  35. zone_id = aws_route53_zone.private.zone_id
  36. vpc_id = each.value
  37. }
  38. # Create the private reverse zone
  39. resource "aws_route53_zone" "reverse" {
  40. name = var.dns_info["reverse"]["zone"]
  41. tags = merge(local.standard_tags, var.tags)
  42. vpc {
  43. vpc_id = local.first_vpc
  44. }
  45. # For the rationale here, see the notes at:
  46. # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_zone_association
  47. #
  48. # tldr; We can't create without an association, but we can't add associations without changing this record.
  49. # So we add one, and then we add it again and ignore any further changes.
  50. lifecycle {
  51. ignore_changes = [vpc]
  52. }
  53. }
  54. resource "aws_route53_record" "dnstest_reverse" {
  55. zone_id = aws_route53_zone.reverse.id
  56. name = "10.10.10"
  57. type = "PTR"
  58. ttl = "300"
  59. # Non-routable Test IP: https://tools.ietf.org/html/rfc5737
  60. records = ["dnstest.${var.dns_info["private"]["zone"]}"]
  61. }
  62. resource "aws_route53_zone_association" "reverse_associations" {
  63. for_each = toset(local.remaining_vpcs)
  64. zone_id = aws_route53_zone.reverse.zone_id
  65. vpc_id = each.value
  66. }
  67. ###################################
  68. # Resolver
  69. resource "aws_route53_resolver_endpoint" "private_resolver" {
  70. name = "xdr_private_dns_resolver"
  71. direction = "INBOUND"
  72. security_group_ids = [aws_security_group.resolver_security_group.id]
  73. dynamic "ip_address" {
  74. for_each = slice(var.subnets, 0, 2)
  75. content {
  76. subnet_id = ip_address.value
  77. }
  78. }
  79. tags = merge(local.standard_tags, var.tags)
  80. }
  81. #----------------------------------------------------------------------------
  82. # Inbound DNS Resolver Security Group
  83. #----------------------------------------------------------------------------
  84. resource "aws_security_group" "resolver_security_group" {
  85. name = "route53_resolver"
  86. description = "Allow DNS inbound traffic"
  87. vpc_id = local.first_vpc
  88. #----------------------------------------------------------------------------
  89. # INGRESS
  90. #----------------------------------------------------------------------------
  91. ingress {
  92. description = "DNS_UDP - Inbound"
  93. from_port = 53
  94. to_port = 53
  95. protocol = "udp"
  96. cidr_blocks = ["10.0.0.0/8"]
  97. }
  98. ingress {
  99. description = "DNS_TCP - Inbound"
  100. from_port = 53
  101. to_port = 53
  102. protocol = "tcp"
  103. cidr_blocks = ["10.0.0.0/8"]
  104. }
  105. #----------------------------------------------------------------------------
  106. # EGRESS
  107. #----------------------------------------------------------------------------
  108. egress {
  109. description = "DNS_UDP - Outbound"
  110. from_port = 53
  111. to_port = 53
  112. protocol = "udp"
  113. cidr_blocks = ["10.0.0.0/8"]
  114. }
  115. egress {
  116. description = "DNS_TCP - Outbound"
  117. from_port = 53
  118. to_port = 53
  119. protocol = "tcp"
  120. cidr_blocks = ["10.0.0.0/8"]
  121. }
  122. tags = merge(local.standard_tags, var.tags)
  123. }