main.tf 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. # Some instance variables
  2. locals {
  3. ami_selection = "master" # master, minion, ...
  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. # Use the default EBS key
  12. data "aws_kms_key" "ebs-key" {
  13. key_id = "alias/ebs_root_encrypt_decrypt"
  14. }
  15. resource "aws_network_interface" "instance" {
  16. subnet_id = var.subnets[0]
  17. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.salt_master_security_group.id]
  18. description = var.instance_name
  19. tags = merge(local.standard_tags, var.tags, { Name = var.instance_name })
  20. }
  21. resource "aws_eip" "instance" {
  22. vpc = true
  23. tags = merge(local.standard_tags, var.tags, { Name = var.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 = var.environment == "prod" ? "t3a.xlarge" : "t3a.large"
  36. key_name = "msoc-build"
  37. monitoring = false # checkov:skip=CKV_AWS_126:Detailed monitoring not needed at this time
  38. iam_instance_profile = "salt-master-instance-profile"
  39. metadata_options {
  40. http_endpoint = "enabled"
  41. # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately; see https://github.com/saltstack/salt/issues/60668
  42. http_tokens = "optional"
  43. }
  44. ami = local.ami_map[local.ami_selection]
  45. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  46. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  47. # that could be removed.
  48. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  49. # These device definitions are optional, but added for clarity.
  50. root_block_device {
  51. volume_type = "gp3"
  52. #volume_size = "60"
  53. delete_on_termination = true
  54. encrypted = true
  55. kms_key_id = data.aws_kms_key.ebs-key.arn
  56. }
  57. ebs_block_device {
  58. # swap
  59. device_name = "/dev/xvdm"
  60. volume_type = "gp3"
  61. volume_size = 48
  62. delete_on_termination = true
  63. encrypted = true
  64. kms_key_id = data.aws_kms_key.ebs-key.arn
  65. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  66. # This may prompt replacement when the AMI is updated.
  67. # See:
  68. # https://github.com/hashicorp/terraform/issues/19958
  69. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  70. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  71. }
  72. ebs_block_device {
  73. # /home
  74. device_name = "/dev/xvdn"
  75. volume_type = "gp3"
  76. # volume_size = xx
  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/xvdn"].ebs.snapshot_id
  81. }
  82. ebs_block_device {
  83. # /var
  84. device_name = "/dev/xvdo"
  85. volume_size = 30
  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/xvdo"].ebs.snapshot_id
  91. }
  92. ebs_block_device {
  93. # /var/tmp
  94. device_name = "/dev/xvdp"
  95. volume_type = "gp3"
  96. # volume_size = xx
  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/xvdp"].ebs.snapshot_id
  101. }
  102. ebs_block_device {
  103. # /var/log
  104. device_name = "/dev/xvdq"
  105. volume_type = "gp3"
  106. # volume_size = xx
  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/xvdq"].ebs.snapshot_id
  111. }
  112. ebs_block_device {
  113. # /var/log/audit
  114. device_name = "/dev/xvdr"
  115. volume_type = "gp3"
  116. # volume_size = xx
  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/xvdr"].ebs.snapshot_id
  121. }
  122. ebs_block_device {
  123. # /tmp
  124. device_name = "/dev/xvds"
  125. volume_type = "gp3"
  126. # volume_size = xx
  127. delete_on_termination = true
  128. encrypted = true
  129. kms_key_id = data.aws_kms_key.ebs-key.arn
  130. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  131. }
  132. network_interface {
  133. device_index = 0
  134. network_interface_id = aws_network_interface.instance.id
  135. }
  136. user_data = data.template_cloudinit_config.salt_master_cloud_init_config.rendered
  137. tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = var.instance_name })
  138. volume_tags = merge(local.standard_tags, var.tags, { Name = var.instance_name })
  139. }
  140. module "private_dns_record" {
  141. source = "../../submodules/dns/private_A_record"
  142. name = var.instance_name
  143. ip_addresses = [aws_instance.instance.private_ip]
  144. dns_info = var.dns_info
  145. reverse_enabled = var.reverse_enabled
  146. providers = {
  147. aws.c2 = aws.c2
  148. }
  149. }
  150. module "public_dns_record" {
  151. source = "../../submodules/dns/public_A_record"
  152. name = var.instance_name
  153. ip_addresses = [aws_eip.instance.public_ip]
  154. dns_info = var.dns_info
  155. providers = {
  156. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  157. }
  158. }
  159. # Render a multi-part cloud-init config making use of the part
  160. # above, and other source files
  161. data "template_cloudinit_config" "salt_master_cloud_init_config" {
  162. gzip = true
  163. base64_encode = true
  164. # Main cloud-config configuration file.
  165. part {
  166. filename = "init.cfg"
  167. content_type = "text/cloud-config"
  168. content = templatefile("${path.module}/cloud-init/cloud_init_salt_master.tpl",
  169. {
  170. hostname = var.instance_name
  171. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  172. environment = var.environment
  173. salt_master = local.salt_master
  174. proxy = local.proxy
  175. aws_partition = var.aws_partition
  176. aws_partition_alias = var.aws_partition_alias
  177. aws_region = var.aws_region
  178. }
  179. )
  180. }
  181. # Additional parts as needed
  182. part {
  183. content_type = "text/x-shellscript"
  184. content = file("${path.module}/cloud-init/provision_salt_master.sh")
  185. }
  186. }
  187. resource "aws_security_group" "salt_master_security_group" {
  188. name = "salt_master_security_group"
  189. description = "Security Group for Salt Master(s)"
  190. vpc_id = var.vpc_id
  191. tags = merge(local.standard_tags, var.tags)
  192. }
  193. resource "aws_security_group_rule" "http-out" {
  194. description = "For endpoints and troubleshooting"
  195. type = "egress"
  196. from_port = 80
  197. to_port = 80
  198. protocol = "tcp"
  199. cidr_blocks = ["10.0.0.0/8"]
  200. security_group_id = aws_security_group.salt_master_security_group.id
  201. }
  202. resource "aws_security_group_rule" "https-out" {
  203. description = "For endpoints and troubleshooting"
  204. type = "egress"
  205. from_port = 443
  206. to_port = 443
  207. protocol = "tcp"
  208. cidr_blocks = ["10.0.0.0/8"]
  209. security_group_id = aws_security_group.salt_master_security_group.id
  210. }
  211. resource "aws_security_group_rule" "saltstack" {
  212. description = "SaltStack"
  213. type = "ingress"
  214. from_port = "4505"
  215. to_port = "4506"
  216. protocol = "tcp"
  217. cidr_blocks = ["10.0.0.0/8"]
  218. security_group_id = aws_security_group.salt_master_security_group.id
  219. }
  220. resource "aws_security_group_rule" "saltstack-external-ips" {
  221. # This deserves some explanation. Terraform "for_each" expects to be
  222. # getting as input a map of values to iterate over as part of the foreach.
  223. # The keys of the map are used to name each of these objects created. Looking
  224. # in the terraform plan output of a for_each you'll see things like:
  225. #
  226. # aws_security_group_rule.resource_name["key-value-from-foreach"] will be created
  227. #
  228. # Our c2_services_external_ips is a list of maps, not a map of maps. The for-expression
  229. # makes a new thing that is a map of maps, where the key value is the description with
  230. # blanks removed.
  231. #
  232. # We could have made the variable more natively-friendly to for_each but this seemed
  233. # like a better solution for what we were trying to accomplish.
  234. for_each = { for s in local.c2_services_external_ips : replace(s.description, "/\\s*/", "") => s }
  235. description = "Saltstack - ${each.value.description}"
  236. type = "ingress"
  237. from_port = "4505"
  238. to_port = "4506"
  239. protocol = "tcp"
  240. cidr_blocks = each.value.cidr_blocks # tfsec:ignore:aws-vpc-no-public-ingress-sgr Intentionally allow inbound
  241. security_group_id = aws_security_group.salt_master_security_group.id
  242. }
  243. #TODO: make this better
  244. #for now, just allow 22 outbound anywhere
  245. resource "aws_security_group_rule" "saltstack-github" {
  246. description = "SaltStack - Github Access"
  247. type = "egress"
  248. from_port = "22"
  249. to_port = "22"
  250. protocol = "tcp"
  251. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-egress-sgr direct access for github
  252. security_group_id = aws_security_group.salt_master_security_group.id
  253. }