main.tf 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. locals {
  2. ami_selection = "minion" # master, minion, ...
  3. instance_name = "${var.instance_prefix}-${var.instance_number}"
  4. }
  5. # Rather than pass in the aws security group, we just look it up. This will
  6. # probably be useful other places, as well.
  7. data "aws_security_group" "typical-host" {
  8. name = "typical-host"
  9. vpc_id = var.vpc_id
  10. }
  11. data "aws_kms_key" "ebs-key" {
  12. key_id = "alias/ebs_root_encrypt_decrypt"
  13. }
  14. resource "aws_network_interface" "instance" {
  15. subnet_id = var.subnet_id
  16. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.dns_security_group.id]
  17. description = local.instance_name
  18. tags = merge(local.standard_tags, var.tags, { Name = local.instance_name })
  19. }
  20. resource "aws_eip" "instance" {
  21. vpc = true
  22. tags = merge(local.standard_tags, var.tags, { Name = local.instance_name })
  23. }
  24. resource "aws_eip_association" "instance" {
  25. network_interface_id = aws_network_interface.instance.id
  26. allocation_id = aws_eip.instance.id
  27. }
  28. resource "aws_instance" "instance" {
  29. #availability_zone = var.azs[count.index % 2]
  30. tenancy = "default"
  31. ebs_optimized = true
  32. disable_api_termination = var.instance_termination_protection
  33. instance_initiated_shutdown_behavior = "stop"
  34. instance_type = "t3a.xlarge"
  35. key_name = var.resolver_instance_key_name
  36. monitoring = false
  37. iam_instance_profile = "msoc-default-instance-profile"
  38. ami = local.ami_map["minion"]
  39. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  40. root_block_device {
  41. volume_type = "gp3"
  42. volume_size = 10
  43. delete_on_termination = true
  44. encrypted = true
  45. kms_key_id = data.aws_kms_key.ebs-key.arn
  46. }
  47. ebs_block_device {
  48. # swap
  49. device_name = "/dev/xvdm"
  50. volume_size = 8
  51. volume_type = "gp3"
  52. delete_on_termination = true
  53. encrypted = true
  54. kms_key_id = data.aws_kms_key.ebs-key.arn
  55. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  56. # This may prompt replacement when the AMI is updated.
  57. # See:
  58. # https://github.com/hashicorp/terraform/issues/19958
  59. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  60. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  61. }
  62. ebs_block_device {
  63. # /home
  64. device_name = "/dev/xvdn"
  65. volume_size = 8
  66. volume_type = "gp3"
  67. delete_on_termination = true
  68. encrypted = true
  69. kms_key_id = data.aws_kms_key.ebs-key.arn
  70. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  71. }
  72. ebs_block_device {
  73. # /var
  74. device_name = "/dev/xvdo"
  75. volume_size = 4
  76. volume_type = "gp3"
  77. delete_on_termination = true
  78. encrypted = true
  79. kms_key_id = data.aws_kms_key.ebs-key.arn
  80. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  81. }
  82. ebs_block_device {
  83. # /var/tmp
  84. device_name = "/dev/xvdp"
  85. volume_size = 4
  86. volume_type = "gp3"
  87. delete_on_termination = true
  88. encrypted = true
  89. kms_key_id = data.aws_kms_key.ebs-key.arn
  90. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  91. }
  92. ebs_block_device {
  93. # /var/log
  94. device_name = "/dev/xvdq"
  95. volume_size = 8
  96. volume_type = "gp3"
  97. delete_on_termination = true
  98. encrypted = true
  99. kms_key_id = data.aws_kms_key.ebs-key.arn
  100. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  101. }
  102. ebs_block_device {
  103. # /var/log/audit
  104. device_name = "/dev/xvdr"
  105. volume_size = 8
  106. volume_type = "gp3"
  107. delete_on_termination = true
  108. encrypted = true
  109. kms_key_id = data.aws_kms_key.ebs-key.arn
  110. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  111. }
  112. ebs_block_device {
  113. # /tmp
  114. device_name = "/dev/xvds"
  115. volume_size = 4
  116. volume_type = "gp3"
  117. delete_on_termination = true
  118. encrypted = true
  119. kms_key_id = data.aws_kms_key.ebs-key.arn
  120. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  121. }
  122. network_interface {
  123. device_index = 0
  124. network_interface_id = aws_network_interface.instance.id
  125. }
  126. user_data = data.template_cloudinit_config.cloud-init.rendered
  127. tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = local.instance_name })
  128. }
  129. module "private_dns_record" {
  130. source = "../../../submodules/dns/private_A_record"
  131. name = local.instance_name
  132. ip_addresses = [aws_instance.instance.private_ip]
  133. dns_info = var.dns_info
  134. reverse_enabled = var.reverse_enabled
  135. providers = {
  136. aws.c2 = aws.c2
  137. }
  138. }
  139. module "public_dns_record" {
  140. source = "../../../submodules/dns/public_A_record"
  141. name = local.instance_name
  142. ip_addresses = [aws_eip.instance.public_ip]
  143. dns_info = var.dns_info
  144. providers = {
  145. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  146. }
  147. }
  148. # Render a multi-part cloud-init config making use of the part
  149. # above, and other source files
  150. data "template_cloudinit_config" "cloud-init" {
  151. gzip = true
  152. base64_encode = true
  153. # Main cloud-config configuration file.
  154. part {
  155. filename = "init.cfg"
  156. content_type = "text/cloud-config"
  157. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  158. {
  159. hostname = local.instance_name
  160. fqdn = "${local.instance_name}.${var.dns_info["private"]["zone"]}"
  161. environment = var.environment
  162. # can't use the DNS name like we would most places, because this is the DNS server
  163. saltmaster = var.environment == "test" ? "10.20.2.32" : "10.40.2.106"
  164. proxy = local.proxy_ip
  165. aws_partition = var.aws_partition
  166. aws_partition_alias = var.aws_partition_alias
  167. aws_region = var.aws_region
  168. }
  169. )
  170. }
  171. # Additional parts as needed
  172. #part {
  173. # content_type = "text/x-shellscript"
  174. # content = "ffbaz"
  175. #}
  176. }
  177. #----------------------------------------------------------------------------
  178. # DNS Security Group
  179. #----------------------------------------------------------------------------
  180. resource "aws_security_group" "dns_security_group" {
  181. name = "dns_security_group_${var.instance_number}"
  182. description = "DNS Security Group"
  183. vpc_id = var.vpc_id
  184. tags = merge(local.standard_tags, var.tags)
  185. }
  186. #----------------------------------------------------------------------------
  187. # INGRESS
  188. #----------------------------------------------------------------------------
  189. resource "aws_security_group_rule" "dns-tcp" {
  190. type = "ingress"
  191. description = "DNS - Inbound TCP"
  192. from_port = 53
  193. to_port = 53
  194. protocol = "tcp"
  195. cidr_blocks = ["10.0.0.0/8"]
  196. security_group_id = aws_security_group.dns_security_group.id
  197. }
  198. resource "aws_security_group_rule" "dns-udp" {
  199. type = "ingress"
  200. description = "DNS - Inbound UDP"
  201. from_port = 53
  202. to_port = 53
  203. protocol = "udp"
  204. cidr_blocks = ["10.0.0.0/8"]
  205. security_group_id = aws_security_group.dns_security_group.id
  206. }
  207. #----------------------------------------------------------------------------
  208. # EGRESS
  209. #----------------------------------------------------------------------------
  210. resource "aws_security_group_rule" "dns_outbound_tcp" {
  211. type = "egress"
  212. description = "DNS - Outbound TCP"
  213. from_port = 53
  214. to_port = 53
  215. protocol = "tcp"
  216. cidr_blocks = ["0.0.0.0/0"] #tfsec:ignore:aws-vpc-no-public-egress-sgr
  217. security_group_id = aws_security_group.dns_security_group.id
  218. }
  219. resource "aws_security_group_rule" "dns_outbound_udp" {
  220. type = "egress"
  221. description = "DNS - Outbound UDP"
  222. from_port = 53
  223. to_port = 53
  224. protocol = "udp"
  225. cidr_blocks = ["0.0.0.0/0"] #tfsec:ignore:aws-vpc-no-public-egress-sgr
  226. security_group_id = aws_security_group.dns_security_group.id
  227. }