main.tf 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. locals {
  2. first_vpc = var.vpcs[0]
  3. remaining_vpcs = [ for vpc in var.vpcs: vpc if vpc != local.first_vpc ]
  4. other_partition = var.aws_partition == "aws-us-gov" ? "aws" : "aws-us-gov"
  5. }
  6. # debug
  7. #output remaining_vpcs {
  8. # value = local.remaining_vpcs
  9. #}
  10. # Create the private zones
  11. resource "aws_route53_zone" "private" {
  12. name = var.private_dns[var.aws_partition].name
  13. tags = merge(var.standard_tags, var.tags)
  14. vpc {
  15. vpc_id = local.first_vpc
  16. }
  17. # For the rationale here, see the notes at:
  18. # https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_zone_association
  19. #
  20. # tldr; We can't create without an association, but we can't add associations without changing this record.
  21. # So we add one, and then we add it again and ignore any further changes.
  22. lifecycle {
  23. ignore_changes = [vpc]
  24. }
  25. }
  26. resource "aws_route53_zone_association" "associations" {
  27. for_each = toset(local.remaining_vpcs)
  28. zone_id = aws_route53_zone.private.zone_id
  29. vpc_id = each.value
  30. }
  31. output "zone_id" {
  32. value = aws_route53_zone.private.id
  33. }
  34. ###################################
  35. # Resolver
  36. resource "aws_route53_resolver_endpoint" "private_resolver" {
  37. name = "xdr_private_dns_resolver"
  38. direction = "INBOUND"
  39. security_group_ids = [ aws_security_group.resolver_security_group.id ]
  40. dynamic "ip_address" {
  41. for_each = var.subnets
  42. content {
  43. subnet_id = ip_address.value
  44. }
  45. }
  46. tags = merge(var.standard_tags, var.tags)
  47. }
  48. output dns_servers {
  49. value = [ for ipblock in aws_route53_resolver_endpoint.private_resolver.ip_address: ipblock["ip"] ]
  50. }
  51. resource "aws_security_group" "resolver_security_group" {
  52. name = "route53_resolver"
  53. description = "Allow DNS inbound traffic"
  54. vpc_id = local.first_vpc
  55. ingress {
  56. description = "DNS_UDP"
  57. from_port = 53
  58. to_port = 53
  59. protocol = "udp"
  60. cidr_blocks = [ "10.0.0.0/8" ]
  61. }
  62. ingress {
  63. description = "DNS_TCP"
  64. from_port = 53
  65. to_port = 53
  66. protocol = "tcp"
  67. cidr_blocks = [ "10.0.0.0/8" ]
  68. }
  69. egress {
  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. egress {
  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. tags = merge(var.standard_tags, var.tags)
  84. }