main.tf 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. }
  45. ebs_block_device {
  46. # swap
  47. device_name = "/dev/xvdm"
  48. volume_size = 8
  49. volume_type = "gp3"
  50. delete_on_termination = true
  51. encrypted = true
  52. kms_key_id = data.aws_kms_key.ebs-key.arn
  53. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  54. # This may prompt replacement when the AMI is updated.
  55. # See:
  56. # https://github.com/hashicorp/terraform/issues/19958
  57. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  58. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  59. }
  60. ebs_block_device {
  61. # /home
  62. device_name = "/dev/xvdn"
  63. volume_size = 8
  64. volume_type = "gp3"
  65. delete_on_termination = true
  66. encrypted = true
  67. kms_key_id = data.aws_kms_key.ebs-key.arn
  68. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  69. }
  70. ebs_block_device {
  71. # /var
  72. device_name = "/dev/xvdo"
  73. volume_size = 4
  74. volume_type = "gp3"
  75. delete_on_termination = true
  76. encrypted = true
  77. kms_key_id = data.aws_kms_key.ebs-key.arn
  78. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  79. }
  80. ebs_block_device {
  81. # /var/tmp
  82. device_name = "/dev/xvdp"
  83. volume_size = 4
  84. volume_type = "gp3"
  85. delete_on_termination = true
  86. encrypted = true
  87. kms_key_id = data.aws_kms_key.ebs-key.arn
  88. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  89. }
  90. ebs_block_device {
  91. # /var/log
  92. device_name = "/dev/xvdq"
  93. volume_size = 8
  94. volume_type = "gp3"
  95. delete_on_termination = true
  96. encrypted = true
  97. kms_key_id = data.aws_kms_key.ebs-key.arn
  98. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  99. }
  100. ebs_block_device {
  101. # /var/log/audit
  102. device_name = "/dev/xvdr"
  103. volume_size = 8
  104. volume_type = "gp3"
  105. delete_on_termination = true
  106. encrypted = true
  107. kms_key_id = data.aws_kms_key.ebs-key.arn
  108. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  109. }
  110. ebs_block_device {
  111. # /tmp
  112. device_name = "/dev/xvds"
  113. volume_size = 4
  114. volume_type = "gp3"
  115. delete_on_termination = true
  116. encrypted = true
  117. kms_key_id = data.aws_kms_key.ebs-key.arn
  118. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  119. }
  120. network_interface {
  121. device_index = 0
  122. network_interface_id = aws_network_interface.instance.id
  123. }
  124. user_data = data.template_cloudinit_config.cloud-init.rendered
  125. tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = local.instance_name })
  126. }
  127. module "private_dns_record" {
  128. source = "../../../submodules/dns/private_A_record"
  129. name = local.instance_name
  130. ip_addresses = [aws_instance.instance.private_ip]
  131. dns_info = var.dns_info
  132. reverse_enabled = var.reverse_enabled
  133. providers = {
  134. aws.c2 = aws.c2
  135. }
  136. }
  137. module "public_dns_record" {
  138. source = "../../../submodules/dns/public_A_record"
  139. name = local.instance_name
  140. ip_addresses = [aws_eip.instance.public_ip]
  141. dns_info = var.dns_info
  142. providers = {
  143. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  144. }
  145. }
  146. # Render a multi-part cloud-init config making use of the part
  147. # above, and other source files
  148. data "template_cloudinit_config" "cloud-init" {
  149. gzip = true
  150. base64_encode = true
  151. # Main cloud-config configuration file.
  152. part {
  153. filename = "init.cfg"
  154. content_type = "text/cloud-config"
  155. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  156. {
  157. hostname = local.instance_name
  158. fqdn = "${local.instance_name}.${var.dns_info["private"]["zone"]}"
  159. environment = var.environment
  160. # can't use the DNS name like we would most places, because this is the DNS server
  161. saltmaster = var.environment == "test" ? "10.20.2.32" : "10.40.2.106"
  162. proxy = local.proxy_ip
  163. aws_partition = var.aws_partition
  164. aws_partition_alias = var.aws_partition_alias
  165. aws_region = var.aws_region
  166. }
  167. )
  168. }
  169. # Additional parts as needed
  170. #part {
  171. # content_type = "text/x-shellscript"
  172. # content = "ffbaz"
  173. #}
  174. }
  175. resource "aws_security_group" "dns_security_group" {
  176. name = "dns_security_group_${var.instance_number}"
  177. description = "DNS Security Group"
  178. vpc_id = var.vpc_id
  179. tags = merge(local.standard_tags, var.tags)
  180. }
  181. resource "aws_security_group_rule" "dns-tcp" {
  182. type = "ingress"
  183. from_port = 53
  184. to_port = 53
  185. protocol = "tcp"
  186. cidr_blocks = ["10.0.0.0/8"]
  187. security_group_id = aws_security_group.dns_security_group.id
  188. }
  189. resource "aws_security_group_rule" "dns-udp" {
  190. type = "ingress"
  191. from_port = 53
  192. to_port = 53
  193. protocol = "udp"
  194. cidr_blocks = ["10.0.0.0/8"]
  195. security_group_id = aws_security_group.dns_security_group.id
  196. }
  197. resource "aws_security_group_rule" "dns_outbound_tcp" {
  198. type = "egress"
  199. from_port = 53
  200. to_port = 53
  201. protocol = "tcp"
  202. cidr_blocks = ["0.0.0.0/0"] #tfsec:ignore:aws-vpc-no-public-egress-sgr
  203. security_group_id = aws_security_group.dns_security_group.id
  204. }
  205. resource "aws_security_group_rule" "dns_outbound_udp" {
  206. type = "egress"
  207. from_port = 53
  208. to_port = 53
  209. protocol = "udp"
  210. cidr_blocks = ["0.0.0.0/0"] #tfsec:ignore:aws-vpc-no-public-egress-sgr
  211. security_group_id = aws_security_group.dns_security_group.id
  212. }