main.tf 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(var.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_zone_association" "associations" {
  26. for_each = toset(local.remaining_vpcs)
  27. zone_id = aws_route53_zone.private.zone_id
  28. vpc_id = each.value
  29. }
  30. # Create the private reverse zone
  31. resource "aws_route53_zone" "reverse" {
  32. name = var.dns_info["reverse"]["zone"]
  33. tags = merge(var.standard_tags, var.tags)
  34. vpc {
  35. vpc_id = local.first_vpc
  36. }
  37. # For the rationale here, see the notes at:
  38. # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_zone_association
  39. #
  40. # tldr; We can't create without an association, but we can't add associations without changing this record.
  41. # So we add one, and then we add it again and ignore any further changes.
  42. lifecycle {
  43. ignore_changes = [vpc]
  44. }
  45. }
  46. resource "aws_route53_zone_association" "reverse_associations" {
  47. for_each = toset(local.remaining_vpcs)
  48. zone_id = aws_route53_zone.reverse.zone_id
  49. vpc_id = each.value
  50. }
  51. ###################################
  52. # Resolver
  53. resource "aws_route53_resolver_endpoint" "private_resolver" {
  54. name = "xdr_private_dns_resolver"
  55. direction = "INBOUND"
  56. security_group_ids = [ aws_security_group.resolver_security_group.id ]
  57. dynamic "ip_address" {
  58. for_each = slice(var.subnets, 0, 2)
  59. content {
  60. subnet_id = ip_address.value
  61. }
  62. }
  63. tags = merge(var.standard_tags, var.tags)
  64. }
  65. resource "aws_security_group" "resolver_security_group" {
  66. name = "route53_resolver"
  67. description = "Allow DNS inbound traffic"
  68. vpc_id = local.first_vpc
  69. ingress {
  70. description = "DNS_UDP"
  71. from_port = 53
  72. to_port = 53
  73. protocol = "udp"
  74. cidr_blocks = [ "10.0.0.0/8" ]
  75. }
  76. ingress {
  77. description = "DNS_TCP"
  78. from_port = 53
  79. to_port = 53
  80. protocol = "tcp"
  81. cidr_blocks = [ "10.0.0.0/8" ]
  82. }
  83. egress {
  84. description = "DNS_UDP"
  85. from_port = 53
  86. to_port = 53
  87. protocol = "udp"
  88. cidr_blocks = [ "10.0.0.0/8" ]
  89. }
  90. egress {
  91. description = "DNS_TCP"
  92. from_port = 53
  93. to_port = 53
  94. protocol = "tcp"
  95. cidr_blocks = [ "10.0.0.0/8" ]
  96. }
  97. tags = merge(var.standard_tags, var.tags)
  98. }