main.tf 9.0 KB

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