main.tf 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. instance_name = var.instance_name != "" ? var.instance_name : "${var.prefix}-splunk-sh"
  5. alb_name = var.alb_name != "" ? var.alb_name : "${var.prefix}-splunk"
  6. is_moose = length(regexall("moose", var.prefix)) > 0 ? true : false
  7. is_monitoring_console = var.alb_name == "splunk-mc" ? true : false
  8. is_fm_searchhead = var.alb_name == "fm-shared-search" ? true : false
  9. }
  10. # Rather than pass in the aws security group, we just look it up. This will
  11. # probably be useful other places, as well.
  12. data "aws_security_group" "typical-host" {
  13. name = "typical-host"
  14. vpc_id = var.vpc_id
  15. }
  16. # Use the default EBS key
  17. data "aws_kms_key" "ebs-key" {
  18. key_id = "alias/ebs_root_encrypt_decrypt"
  19. }
  20. resource "aws_network_interface" "instance" {
  21. subnet_id = var.subnets[0]
  22. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.searchhead_security_group.id]
  23. description = local.instance_name
  24. tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  25. }
  26. resource "aws_instance" "instance" {
  27. #availability_zone = var.azs[count.index % 2]
  28. tenancy = "default"
  29. ebs_optimized = true
  30. disable_api_termination = var.instance_termination_protection
  31. instance_initiated_shutdown_behavior = "stop"
  32. instance_type = var.instance_type
  33. key_name = "msoc-build"
  34. monitoring = false
  35. iam_instance_profile = local.is_moose ? module.moose_instance_profile[0].profile_id : "splunk-sh-instance-profile"
  36. metadata_options {
  37. http_tokens = "optional" # tfsec:ignore:aws-ec2-enforce-http-token-imds Splunk uses v1 by default. MSOCI-2150
  38. }
  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 = var.splunk_volume_sizes["searchhead"]["/"]
  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. # /opt/splunk
  54. # Note: Not in AMI
  55. device_name = "/dev/xvdf"
  56. volume_size = var.splunk_volume_sizes["searchhead"]["/opt/splunk"]
  57. delete_on_termination = true
  58. encrypted = true
  59. kms_key_id = data.aws_kms_key.ebs-key.arn
  60. }
  61. ebs_block_device {
  62. # swap
  63. device_name = "/dev/xvdm"
  64. volume_size = var.splunk_volume_sizes["searchhead"]["swap"]
  65. delete_on_termination = true
  66. encrypted = true
  67. kms_key_id = data.aws_kms_key.ebs-key.arn
  68. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  69. # This may prompt replacement when the AMI is updated.
  70. # See:
  71. # https://github.com/hashicorp/terraform/issues/19958
  72. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  73. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  74. }
  75. ebs_block_device {
  76. # /home
  77. device_name = "/dev/xvdn"
  78. volume_size = var.splunk_volume_sizes["searchhead"]["/home"]
  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/xvdn"].ebs.snapshot_id
  83. }
  84. ebs_block_device {
  85. # /var
  86. device_name = "/dev/xvdo"
  87. volume_size = var.splunk_volume_sizes["searchhead"]["/var"]
  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/xvdo"].ebs.snapshot_id
  92. }
  93. ebs_block_device {
  94. # /var/tmp
  95. device_name = "/dev/xvdp"
  96. volume_size = var.splunk_volume_sizes["searchhead"]["/var/tmp"]
  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_size = var.splunk_volume_sizes["searchhead"]["/var/log"]
  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/xvdq"].ebs.snapshot_id
  110. }
  111. ebs_block_device {
  112. # /var/log/audit
  113. device_name = "/dev/xvdr"
  114. volume_size = var.splunk_volume_sizes["searchhead"]["/var/log/audit"]
  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/xvdr"].ebs.snapshot_id
  119. }
  120. ebs_block_device {
  121. # /tmp
  122. device_name = "/dev/xvds"
  123. volume_size = var.splunk_volume_sizes["searchhead"]["/tmp"]
  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(var.standard_tags, var.tags, var.instance_tags, { Name = local.instance_name })
  135. volume_tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  136. depends_on = [module.moose_instance_profile, module.instance_profile]
  137. }
  138. module "private_dns_record" {
  139. source = "../../../submodules/dns/private_A_record"
  140. name = local.instance_name
  141. ip_addresses = [aws_instance.instance.private_ip]
  142. dns_info = var.dns_info
  143. reverse_enabled = var.reverse_enabled
  144. providers = {
  145. aws.c2 = aws.c2
  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. splunk_prefix = var.prefix
  162. environment = var.environment
  163. salt_master = var.salt_master
  164. proxy = var.proxy
  165. aws_partition = var.aws_partition
  166. aws_partition_alias = var.aws_partition_alias
  167. aws_region = var.aws_region
  168. }
  169. )
  170. }
  171. # mount /dev/xvdf at /opt/splunk
  172. part {
  173. content_type = "text/cloud-boothook"
  174. content = file("${path.module}/cloud-init/opt_splunk.boothook")
  175. }
  176. }
  177. ## Searchhead
  178. #
  179. # Summary:
  180. # Ingress:
  181. # tcp/8000 - Splunk Web - vpc-access, legacy openvpn, legacy bastion, Phantom
  182. # tcp/8000 - Splunk Web - Entire VPC + var.splunk_legacy_cidr
  183. # tcp/8089 - Splunk API - vpc-access, legacy openvpn, legacy bastion, Phantom
  184. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC + var.splunk_legacy_cidr
  185. # tcp/9997-9998 - Splunk Data - Entire VPC + var.splunk_legacy_cidr
  186. #
  187. # Ingress:
  188. # tcp/8000 - Splunk Web - vpc-system-services (for salt inventory (moose only))
  189. # tcp/8089 - Splunk Web - vpc-system-services (for salt inventory and portal lambda)
  190. #
  191. # Egress:
  192. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC + var.splunk_legacy_cidr
  193. resource "aws_security_group" "searchhead_security_group" {
  194. name = "${var.prefix}_searchhead_security_group"
  195. description = "Security Group for Splunk Searchhead Instance(s)"
  196. vpc_id = var.vpc_id
  197. tags = merge(var.standard_tags, var.tags)
  198. }
  199. # Ingress
  200. resource "aws_security_group_rule" "splunk-web-in" {
  201. description = "Web access"
  202. type = "ingress"
  203. from_port = 8000
  204. to_port = 8000
  205. protocol = "tcp"
  206. cidr_blocks = toset(concat(var.cidr_map["vpc-access"],
  207. var.cidr_map["vpc-private-services"],
  208. var.splunk_legacy_cidr,
  209. [var.vpc_cidr],
  210. local.is_moose ? var.cidr_map["vpc-system-services"] : [], # for salt inventory
  211. ))
  212. security_group_id = aws_security_group.searchhead_security_group.id
  213. }
  214. resource "aws_security_group_rule" "splunk-api-in" {
  215. description = "Splunk API"
  216. type = "ingress"
  217. from_port = 8089
  218. to_port = 8089
  219. protocol = "tcp"
  220. cidr_blocks = toset(concat(var.cidr_map["vpc-access"],
  221. var.cidr_map["vpc-private-services"],
  222. var.cidr_map["vpc-splunk"], # MC
  223. var.splunk_legacy_cidr,
  224. [var.vpc_cidr],
  225. var.cidr_map["vpc-system-services"], # for salt inventory and Portal lambda
  226. ))
  227. security_group_id = aws_security_group.searchhead_security_group.id
  228. }
  229. # Egress
  230. resource "aws_security_group_rule" "ssh-out" {
  231. count = length(var.splunk_legacy_cidr) > 0 ? 1 : 0
  232. description = "SSH to legacy splunk"
  233. type = "egress"
  234. from_port = 22
  235. to_port = 22
  236. protocol = "tcp"
  237. cidr_blocks = var.splunk_legacy_cidr
  238. security_group_id = aws_security_group.searchhead_security_group.id
  239. }
  240. resource "aws_security_group_rule" "splunk-api-out" {
  241. description = "Splunk API Outbound to talk to indexers"
  242. type = "egress"
  243. from_port = 8089
  244. to_port = 8089
  245. protocol = "tcp"
  246. cidr_blocks = toset(concat([var.vpc_cidr], var.splunk_legacy_cidr))
  247. security_group_id = aws_security_group.searchhead_security_group.id
  248. }
  249. resource "aws_security_group_rule" "splunk-api-out-to-all" {
  250. count = local.is_monitoring_console || local.is_fm_searchhead ? 1 : 0
  251. description = "Splunk API Outbound to talk to Other Segments"
  252. type = "egress"
  253. from_port = 8089
  254. to_port = 8089
  255. protocol = "tcp"
  256. cidr_blocks = ["10.0.0.0/8"]
  257. security_group_id = aws_security_group.searchhead_security_group.id
  258. }
  259. resource "aws_security_group_rule" "splunk-data-out" {
  260. description = "Splunk Data Outbound to talk to own indexers"
  261. type = "egress"
  262. from_port = 9997
  263. to_port = 9998
  264. protocol = "tcp"
  265. cidr_blocks = toset(concat([var.vpc_cidr], var.splunk_legacy_cidr))
  266. security_group_id = aws_security_group.searchhead_security_group.id
  267. }