main.tf 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. locals {
  2. instance_name = "resolver-${var.aws_partition_alias}"
  3. }
  4. # Rather than pass in the aws security group, we just look it up. This will
  5. # probably be useful other places, as well.
  6. data "aws_security_group" "typical-host" {
  7. name = "typical-host"
  8. vpc_id = var.vpc_id
  9. }
  10. resource "aws_network_interface" "instance" {
  11. subnet_id = var.subnet_id
  12. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.dns_security_group.id ]
  13. description = local.instance_name
  14. tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  15. }
  16. resource "aws_eip" "instance" {
  17. vpc = true
  18. tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  19. }
  20. resource "aws_eip_association" "instance" {
  21. network_interface_id = aws_network_interface.instance.id
  22. allocation_id = aws_eip.instance.id
  23. }
  24. resource "aws_instance" "instance" {
  25. #availability_zone = var.azs[count.index % 2]
  26. tenancy = "default"
  27. ebs_optimized = true
  28. disable_api_termination = var.instance_termination_protection
  29. instance_initiated_shutdown_behavior = "stop"
  30. instance_type = var.resolver_instance_type
  31. key_name = var.resolver_instance_key_name
  32. monitoring = false
  33. ami = local.ami_map["minion"]
  34. lifecycle { ignore_changes = [ ami, key_name, user_data ] }
  35. root_block_device {
  36. volume_type = "gp2"
  37. #volume_size = "60"
  38. delete_on_termination = true
  39. }
  40. network_interface {
  41. device_index = 0
  42. network_interface_id = aws_network_interface.instance.id
  43. }
  44. user_data = data.template_cloudinit_config.cloud-init.rendered
  45. tags = merge( var.standard_tags, var.tags, { Name = local.instance_name })
  46. }
  47. module "private_dns_record" {
  48. source = "../../../submodules/dns/private_A_record"
  49. name = local.instance_name
  50. ip_addresses = [ aws_instance.instance.private_ip ]
  51. dns_info = var.dns_info
  52. reverse_enabled = var.reverse_enabled
  53. providers = {
  54. aws.c2 = aws.c2
  55. }
  56. }
  57. module "public_dns_record" {
  58. source = "../../../submodules/dns/public_A_record"
  59. name = local.instance_name
  60. ip_addresses = [ aws_eip.instance.public_ip ]
  61. dns_info = var.dns_info
  62. providers = {
  63. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  64. }
  65. }
  66. data "template_file" "cloud-init" {
  67. # Should these be in a common directory? I suspect they'd be reusable
  68. template = file("${path.module}/cloud-init/cloud-init.tpl")
  69. vars = {
  70. hostname = local.instance_name
  71. fqdn = "resolver-${var.aws_partition_alias}.${var.dns_info["private"]["zone"]}"
  72. environment = var.environment
  73. # can't use the DNS name like we would most places, because this is the DNS server
  74. saltmaster = var.salt_master_ip
  75. proxy = var.proxy_ip
  76. aws_partition = var.aws_partition
  77. aws_partition_alias = var.aws_partition_alias
  78. }
  79. }
  80. # Render a multi-part cloud-init config making use of the part
  81. # above, and other source files
  82. data "template_cloudinit_config" "cloud-init" {
  83. gzip = true
  84. base64_encode = true
  85. # Main cloud-config configuration file.
  86. part {
  87. filename = "init.cfg"
  88. content_type = "text/cloud-config"
  89. content = data.template_file.cloud-init.rendered
  90. }
  91. # Additional parts as needed
  92. #part {
  93. # content_type = "text/x-shellscript"
  94. # content = "ffbaz"
  95. #}
  96. }
  97. resource "aws_security_group" "dns_security_group" {
  98. name = "dns_security_group"
  99. description = "DNS Security Group"
  100. vpc_id = var.vpc_id
  101. tags = merge(var.standard_tags, var.tags)
  102. }
  103. resource "aws_security_group_rule" "dns-tcp" {
  104. type = "ingress"
  105. from_port = 53
  106. to_port = 53
  107. protocol = "tcp"
  108. cidr_blocks = [ "10.0.0.0/8" ]
  109. security_group_id = aws_security_group.dns_security_group.id
  110. }
  111. resource "aws_security_group_rule" "dns-udp" {
  112. type = "ingress"
  113. from_port = 53
  114. to_port = 53
  115. protocol = "udp"
  116. cidr_blocks = [ "10.0.0.0/8" ]
  117. security_group_id = aws_security_group.dns_security_group.id
  118. }
  119. resource "aws_security_group_rule" "dns_outbound_tcp" {
  120. type = "egress"
  121. from_port = 53
  122. to_port = 53
  123. protocol = "tcp"
  124. cidr_blocks = [ "0.0.0.0/0" ]
  125. security_group_id = aws_security_group.dns_security_group.id
  126. }
  127. resource "aws_security_group_rule" "dns_outbound_udp" {
  128. type = "egress"
  129. from_port = 53
  130. to_port = 53
  131. protocol = "udp"
  132. cidr_blocks = [ "0.0.0.0/0" ]
  133. security_group_id = aws_security_group.dns_security_group.id
  134. }