main.tf 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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(local.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 = local.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_endpoint = "enabled"
  38. http_tokens = "optional" # tfsec:ignore:aws-ec2-enforce-http-token-imds Splunk uses v1 by default. MSOCI-2150
  39. }
  40. ami = local.ami_map[local.ami_selection]
  41. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  42. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  43. # that could be removed.
  44. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  45. # These device definitions are optional, but added for clarity.
  46. root_block_device {
  47. volume_type = "gp2"
  48. volume_size = local.volume_sizes["/"]
  49. delete_on_termination = true
  50. encrypted = true
  51. kms_key_id = data.aws_kms_key.ebs-key.arn
  52. }
  53. ebs_block_device {
  54. # /opt/splunk
  55. # Note: Not in AMI
  56. device_name = "/dev/xvdf"
  57. volume_size = local.volume_sizes["/opt/splunk"]
  58. delete_on_termination = true
  59. encrypted = true
  60. kms_key_id = data.aws_kms_key.ebs-key.arn
  61. }
  62. ebs_block_device {
  63. # swap
  64. device_name = "/dev/xvdm"
  65. volume_size = local.volume_sizes["swap"]
  66. delete_on_termination = true
  67. encrypted = true
  68. kms_key_id = data.aws_kms_key.ebs-key.arn
  69. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  70. # This may prompt replacement when the AMI is updated.
  71. # See:
  72. # https://github.com/hashicorp/terraform/issues/19958
  73. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  74. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  75. }
  76. ebs_block_device {
  77. # /home
  78. device_name = "/dev/xvdn"
  79. volume_size = local.volume_sizes["/home"]
  80. delete_on_termination = true
  81. encrypted = true
  82. kms_key_id = data.aws_kms_key.ebs-key.arn
  83. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  84. }
  85. ebs_block_device {
  86. # /var
  87. device_name = "/dev/xvdo"
  88. volume_size = local.volume_sizes["/var"]
  89. delete_on_termination = true
  90. encrypted = true
  91. kms_key_id = data.aws_kms_key.ebs-key.arn
  92. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  93. }
  94. ebs_block_device {
  95. # /var/tmp
  96. device_name = "/dev/xvdp"
  97. volume_size = local.volume_sizes["/var/tmp"]
  98. delete_on_termination = true
  99. encrypted = true
  100. kms_key_id = data.aws_kms_key.ebs-key.arn
  101. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  102. }
  103. ebs_block_device {
  104. # /var/log
  105. device_name = "/dev/xvdq"
  106. volume_size = local.volume_sizes["/var/log"]
  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_size = local.volume_sizes["/var/log/audit"]
  116. delete_on_termination = true
  117. encrypted = true
  118. kms_key_id = data.aws_kms_key.ebs-key.arn
  119. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  120. }
  121. ebs_block_device {
  122. # /tmp
  123. device_name = "/dev/xvds"
  124. volume_size = local.volume_sizes["/tmp"]
  125. delete_on_termination = true
  126. encrypted = true
  127. kms_key_id = data.aws_kms_key.ebs-key.arn
  128. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  129. }
  130. network_interface {
  131. device_index = 0
  132. network_interface_id = aws_network_interface.instance.id
  133. }
  134. user_data = data.template_cloudinit_config.cloud-init.rendered
  135. tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = local.instance_name })
  136. volume_tags = merge(local.standard_tags, var.tags, { Name = local.instance_name })
  137. depends_on = [module.moose_instance_profile, module.instance_profile]
  138. }
  139. module "private_dns_record" {
  140. source = "../../../submodules/dns/private_A_record"
  141. name = local.instance_name
  142. ip_addresses = [aws_instance.instance.private_ip]
  143. dns_info = var.dns_info
  144. reverse_enabled = var.reverse_enabled
  145. providers = {
  146. aws.c2 = aws.c2
  147. }
  148. }
  149. # Render a multi-part cloud-init config making use of the part
  150. # above, and other source files
  151. data "template_cloudinit_config" "cloud-init" {
  152. gzip = true
  153. base64_encode = true
  154. # Main cloud-config configuration file.
  155. part {
  156. filename = "init.cfg"
  157. content_type = "text/cloud-config"
  158. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  159. {
  160. hostname = local.instance_name
  161. fqdn = "${local.instance_name}.${var.dns_info["private"]["zone"]}"
  162. splunk_prefix = var.prefix
  163. environment = var.environment
  164. salt_master = local.salt_master
  165. proxy = local.proxy
  166. aws_partition = var.aws_partition
  167. aws_partition_alias = var.aws_partition_alias
  168. aws_region = var.aws_region
  169. }
  170. )
  171. }
  172. # mount /dev/xvdf at /opt/splunk
  173. part {
  174. content_type = "text/cloud-boothook"
  175. content = file("${path.module}/cloud-init/opt_splunk.boothook")
  176. }
  177. }
  178. ## Searchhead
  179. #
  180. # Summary:
  181. # Ingress:
  182. # tcp/8000 - Splunk Web - vpc-access, legacy openvpn, legacy bastion, Phantom
  183. # tcp/8000 - Splunk Web - Entire VPC + local.splunk_legacy_cidr
  184. # tcp/8089 - Splunk API - vpc-access, legacy openvpn, legacy bastion, Phantom
  185. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC + local.splunk_legacy_cidr
  186. # tcp/9997-9998 - Splunk Data - Entire VPC + local.splunk_legacy_cidr
  187. #
  188. # Ingress:
  189. # tcp/8000 - Splunk Web - vpc-system-services (for salt inventory (moose only))
  190. # tcp/8089 - Splunk Web - vpc-system-services (for salt inventory and portal lambda)
  191. #
  192. # Egress:
  193. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC + local.splunk_legacy_cidr
  194. resource "aws_security_group" "searchhead_security_group" {
  195. name = "${var.prefix}_searchhead_security_group"
  196. description = "Security Group for Splunk Searchhead Instance(s)"
  197. vpc_id = var.vpc_id
  198. tags = merge(local.standard_tags, var.tags)
  199. }
  200. #----------------------------------------------------------------------------
  201. # INGRESS
  202. #----------------------------------------------------------------------------
  203. resource "aws_security_group_rule" "splunk-web-in" {
  204. type = "ingress"
  205. description = "Splunk Web - Inbound access"
  206. from_port = 8000
  207. to_port = 8000
  208. protocol = "tcp"
  209. cidr_blocks = toset(concat(local.cidr_map["vpc-access"],
  210. local.cidr_map["vpc-private-services"],
  211. local.splunk_legacy_cidr,
  212. [var.vpc_cidr],
  213. local.is_moose ? local.cidr_map["vpc-system-services"] : [], # for salt inventory
  214. ))
  215. security_group_id = aws_security_group.searchhead_security_group.id
  216. }
  217. resource "aws_security_group_rule" "splunk-api-in" {
  218. type = "ingress"
  219. description = "Splunk API - Inbound access"
  220. from_port = 8089
  221. to_port = 8089
  222. protocol = "tcp"
  223. cidr_blocks = toset(concat(local.cidr_map["vpc-access"],
  224. local.cidr_map["vpc-private-services"],
  225. local.cidr_map["vpc-splunk"], # MC
  226. local.splunk_legacy_cidr,
  227. [var.vpc_cidr],
  228. local.cidr_map["vpc-system-services"], # for salt inventory and Portal lambda
  229. ))
  230. security_group_id = aws_security_group.searchhead_security_group.id
  231. }
  232. #----------------------------------------------------------------------------
  233. # EGRESS
  234. #----------------------------------------------------------------------------
  235. resource "aws_security_group_rule" "ssh-out" {
  236. count = length(local.splunk_legacy_cidr) > 0 ? 1 : 0
  237. type = "egress"
  238. description = "SSH to Legacy Splunk"
  239. from_port = 22
  240. to_port = 22
  241. protocol = "tcp"
  242. cidr_blocks = local.splunk_legacy_cidr
  243. security_group_id = aws_security_group.searchhead_security_group.id
  244. }
  245. resource "aws_security_group_rule" "splunk-api-out" {
  246. type = "egress"
  247. description = "Splunk API - Outbound to talk to indexers"
  248. from_port = 8089
  249. to_port = 8089
  250. protocol = "tcp"
  251. cidr_blocks = toset(concat([var.vpc_cidr], local.splunk_legacy_cidr))
  252. security_group_id = aws_security_group.searchhead_security_group.id
  253. }
  254. resource "aws_security_group_rule" "splunk-api-out-to-all" {
  255. count = local.is_monitoring_console || local.is_fm_searchhead ? 1 : 0
  256. type = "egress"
  257. description = "Splunk API - Outbound to talk to Other Segments"
  258. from_port = 8089
  259. to_port = 8089
  260. protocol = "tcp"
  261. cidr_blocks = ["10.0.0.0/8"]
  262. security_group_id = aws_security_group.searchhead_security_group.id
  263. }
  264. resource "aws_security_group_rule" "splunk-data-out" {
  265. type = "egress"
  266. description = "Splunk Data - Outbound to talk to own indexers"
  267. from_port = 9997
  268. to_port = 9998
  269. protocol = "tcp"
  270. cidr_blocks = toset(concat([var.vpc_cidr], local.splunk_legacy_cidr))
  271. security_group_id = aws_security_group.searchhead_security_group.id
  272. }