main.tf 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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(var.standard_tags, var.tags, { Name = var.instance_name })
  20. }
  21. resource "aws_eip" "instance" {
  22. vpc = true
  23. tags = merge(var.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.instance_type
  36. key_name = "msoc-build"
  37. monitoring = false
  38. iam_instance_profile = "salt-master-instance-profile"
  39. ami = local.ami_map[local.ami_selection]
  40. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  41. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  42. # that could be removed.
  43. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  44. # These device definitions are optional, but added for clarity.
  45. root_block_device {
  46. volume_type = "gp2"
  47. #volume_size = "60"
  48. delete_on_termination = true
  49. encrypted = true
  50. kms_key_id = data.aws_kms_key.ebs-key.arn
  51. }
  52. ebs_block_device {
  53. # swap
  54. device_name = "/dev/xvdm"
  55. volume_size = 48
  56. delete_on_termination = true
  57. encrypted = true
  58. kms_key_id = data.aws_kms_key.ebs-key.arn
  59. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  60. # This may prompt replacement when the AMI is updated.
  61. # See:
  62. # https://github.com/hashicorp/terraform/issues/19958
  63. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  64. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  65. }
  66. ebs_block_device {
  67. # /home
  68. device_name = "/dev/xvdn"
  69. # volume_size = xx
  70. delete_on_termination = true
  71. encrypted = true
  72. kms_key_id = data.aws_kms_key.ebs-key.arn
  73. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  74. }
  75. ebs_block_device {
  76. # /var
  77. device_name = "/dev/xvdo"
  78. # volume_size = xx
  79. delete_on_termination = true
  80. encrypted = true
  81. kms_key_id = data.aws_kms_key.ebs-key.arn
  82. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  83. }
  84. ebs_block_device {
  85. # /var/tmp
  86. device_name = "/dev/xvdp"
  87. # volume_size = xx
  88. delete_on_termination = true
  89. encrypted = true
  90. kms_key_id = data.aws_kms_key.ebs-key.arn
  91. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  92. }
  93. ebs_block_device {
  94. # /var/log
  95. device_name = "/dev/xvdq"
  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/xvdq"].ebs.snapshot_id
  101. }
  102. ebs_block_device {
  103. # /var/log/audit
  104. device_name = "/dev/xvdr"
  105. # volume_size = xx
  106. delete_on_termination = true
  107. encrypted = true
  108. kms_key_id = data.aws_kms_key.ebs-key.arn
  109. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  110. }
  111. ebs_block_device {
  112. # /tmp
  113. device_name = "/dev/xvds"
  114. # volume_size = xx
  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.salt_master_cloud_init_config.rendered
  125. tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  126. volume_tags = merge( var.standard_tags, var.tags, { Name = var.instance_name })
  127. }
  128. module "private_dns_record" {
  129. source = "../../submodules/dns/private_A_record"
  130. name = var.instance_name
  131. ip_addresses = [ aws_instance.instance.private_ip ]
  132. dns_info = var.dns_info
  133. reverse_enabled = var.reverse_enabled
  134. providers = {
  135. aws.c2 = aws.c2
  136. }
  137. }
  138. module "public_dns_record" {
  139. source = "../../submodules/dns/public_A_record"
  140. name = var.instance_name
  141. ip_addresses = [ aws_eip.instance.public_ip ]
  142. dns_info = var.dns_info
  143. providers = {
  144. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  145. }
  146. }
  147. #The Cloud init data is to prepare the Salt Master for use.
  148. #This includes secrets from the AWS Secrets Manager, Github connectivity via SSH, and
  149. #prepopulating the salt master private key. May history judge me kindly.
  150. data "template_file" "salt_master_cloud_init" {
  151. # Should these be in a common directory? I suspect they'd be reusable
  152. template = file("${path.module}/cloud-init/cloud_init_salt_master.tpl")
  153. vars = {
  154. hostname = var.instance_name
  155. fqdn = "${var.instance_name}.${var.dns_info["private"]["zone"]}"
  156. environment = var.environment
  157. salt_master = var.salt_master
  158. proxy = var.proxy
  159. aws_partition = var.aws_partition
  160. aws_partition_alias = var.aws_partition_alias
  161. aws_region = var.aws_region
  162. }
  163. }
  164. # Render a multi-part cloud-init config making use of the part
  165. # above, and other source files
  166. data "template_cloudinit_config" "salt_master_cloud_init_config" {
  167. gzip = true
  168. base64_encode = true
  169. # Main cloud-config configuration file.
  170. part {
  171. filename = "init.cfg"
  172. content_type = "text/cloud-config"
  173. content = data.template_file.salt_master_cloud_init.rendered
  174. }
  175. # Additional parts as needed
  176. part {
  177. content_type = "text/x-shellscript"
  178. content = file("${path.module}/cloud-init/provision_salt_master.sh")
  179. }
  180. }
  181. resource "aws_security_group" "salt_master_security_group" {
  182. name = "salt_master_security_group"
  183. description = "Security Group for Salt Master(s)"
  184. vpc_id = var.vpc_id
  185. tags = merge(var.standard_tags, var.tags)
  186. }
  187. resource "aws_security_group_rule" "http-out" {
  188. description = "For endpoints and troubleshooting"
  189. type = "egress"
  190. from_port = 80
  191. to_port = 80
  192. protocol = "tcp"
  193. cidr_blocks = [ "10.0.0.0/8" ]
  194. security_group_id = aws_security_group.salt_master_security_group.id
  195. }
  196. resource "aws_security_group_rule" "https-out" {
  197. description = "For endpoints and troubleshooting"
  198. type = "egress"
  199. from_port = 443
  200. to_port = 443
  201. protocol = "tcp"
  202. cidr_blocks = [ "10.0.0.0/8" ]
  203. security_group_id = aws_security_group.salt_master_security_group.id
  204. }
  205. resource "aws_security_group_rule" "saltstack" {
  206. description = "SaltStack"
  207. type = "ingress"
  208. from_port = "4505"
  209. to_port = "4506"
  210. protocol = "tcp"
  211. cidr_blocks = [ "10.0.0.0/8" ]
  212. security_group_id = aws_security_group.salt_master_security_group.id
  213. }
  214. resource "aws_security_group_rule" "saltstack-external-ips" {
  215. # This deserves some explanation. Terraform "for_each" expects to be
  216. # getting as input a map of values to iterate over as part of the foreach.
  217. # The keys of the map are used to name each of these objects created. Looking
  218. # in the terraform plan output of a for_each you'll see things like:
  219. #
  220. # aws_security_group_rule.resource_name["key-value-from-foreach"] will be created
  221. #
  222. # Our c2_services_external_ips is a list of maps, not a map of maps. The for-expression
  223. # makes a new thing that is a map of maps, where the key value is the description with
  224. # blanks removed.
  225. #
  226. # We could have made the variable more natively-friendly to for_each but this seemed
  227. # like a better solution for what we were trying to accomplish.
  228. for_each = { for s in var.c2_services_external_ips : replace(s.description,"/\\s*/","") => s }
  229. description = "Saltstack - ${each.value.description}"
  230. type = "ingress"
  231. from_port = "4505"
  232. to_port = "4506"
  233. protocol = "tcp"
  234. cidr_blocks = each.value.cidr_blocks
  235. security_group_id = aws_security_group.salt_master_security_group.id
  236. }
  237. resource "aws_security_group_rule" "saltstack-afs-pop" {
  238. description = "SaltStack - AFS POP"
  239. type = "ingress"
  240. from_port = "4505"
  241. to_port = "4506"
  242. protocol = "tcp"
  243. cidr_blocks = var.afs_pop
  244. security_group_id = aws_security_group.salt_master_security_group.id
  245. }
  246. resource "aws_security_group_rule" "saltstack-afs-azure-pop" {
  247. description = "SaltStack - AFS Azure POP"
  248. type = "ingress"
  249. from_port = "4505"
  250. to_port = "4506"
  251. protocol = "tcp"
  252. cidr_blocks = var.afs_azure_pop
  253. security_group_id = aws_security_group.salt_master_security_group.id
  254. }
  255. resource "aws_security_group_rule" "saltstack-nga-pop" {
  256. description = "SaltStack - NGA POP"
  257. type = "ingress"
  258. from_port = "4505"
  259. to_port = "4506"
  260. protocol = "tcp"
  261. cidr_blocks = var.nga_pop
  262. security_group_id = aws_security_group.salt_master_security_group.id
  263. }
  264. resource "aws_security_group_rule" "saltstack-xdr-interconnects" {
  265. description = "SaltStack - XDR Interconnects"
  266. type = "ingress"
  267. from_port = "4505"
  268. to_port = "4506"
  269. protocol = "tcp"
  270. cidr_blocks = var.xdr_interconnect
  271. security_group_id = aws_security_group.salt_master_security_group.id
  272. }
  273. #TODO: make this better
  274. #for now, just allow 22 outbound anywhere
  275. resource "aws_security_group_rule" "saltstack-github" {
  276. description = "SaltStack - Github Access"
  277. type = "egress"
  278. from_port = "22"
  279. to_port = "22"
  280. protocol = "tcp"
  281. cidr_blocks = [ "0.0.0.0/0" ]
  282. security_group_id = aws_security_group.salt_master_security_group.id
  283. }