main.tf 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. resource "aws_security_group" "resolver_security_group" {
  82. name = "route53_resolver"
  83. description = "Allow DNS inbound traffic"
  84. vpc_id = local.first_vpc
  85. ingress {
  86. description = "DNS_UDP"
  87. from_port = 53
  88. to_port = 53
  89. protocol = "udp"
  90. cidr_blocks = ["10.0.0.0/8"]
  91. }
  92. ingress {
  93. description = "DNS_TCP"
  94. from_port = 53
  95. to_port = 53
  96. protocol = "tcp"
  97. cidr_blocks = ["10.0.0.0/8"]
  98. }
  99. egress {
  100. description = "DNS_UDP"
  101. from_port = 53
  102. to_port = 53
  103. protocol = "udp"
  104. cidr_blocks = ["10.0.0.0/8"]
  105. }
  106. egress {
  107. description = "DNS_TCP"
  108. from_port = 53
  109. to_port = 53
  110. protocol = "tcp"
  111. cidr_blocks = ["10.0.0.0/8"]
  112. }
  113. tags = merge(local.standard_tags, var.tags)
  114. }