main.tf 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. locals {
  2. # For reverse dns:
  3. # 0) Only take the first address
  4. first_address = var.ip_addresses[0]
  5. # 1) Split the ip addresses into 4 octets
  6. octets = regex("^(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)$", local.first_address)
  7. # 2) Reverse them, but only if the first octet is 10 (maybe handle 192.168 and 172.16 later), and if enabled
  8. # NOTE: This used to ignore any that don't start with 10, but that causes headaches because:
  9. # > The "for_each" value depends on resource attributes that cannot be determined
  10. # > until apply, so Terraform cannot predict how many instances will be created.
  11. # > To work around this, use the -target argument to first apply only the
  12. # > resources that the for_each depends on.
  13. # So, we create the reverse no matter what, which means you should pass in 'reverse_disabled' if you're not
  14. # assignign out of the 10. network.
  15. reverse_address = join(".", reverse(slice(local.octets, 1, 4)))
  16. }
  17. resource "aws_route53_record" "dns" {
  18. count = var.enabled ? 1 : 0
  19. name = var.name
  20. type = "A"
  21. ttl = 300
  22. zone_id = var.dns_info["private"]["zone_id"]
  23. records = var.ip_addresses #checkov:skip=CKV2_AWS_23:IP Address Passed in from external resources
  24. provider = aws.c2
  25. }
  26. resource "aws_route53_record" "reverse_dns" {
  27. count = var.enabled && var.reverse_enabled ? 1 : 0
  28. name = local.reverse_address
  29. type = "PTR"
  30. ttl = 300
  31. zone_id = var.dns_info["reverse"]["zone_id"]
  32. records = ["${var.name}.${var.dns_info["private"]["zone"]}"]
  33. provider = aws.c2
  34. }