main.tf 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. instance_name = var.instance_name != "" ? var.instance_name : "${var.prefix}-splunk-cust-sh"
  5. alb_name = "${var.prefix}-splunk-cust-sh"
  6. dns_short_name = "search.${var.prefix}"
  7. auth_short_name = "search-auth.${var.prefix}"
  8. }
  9. # Rather than pass in the aws security group, we just look it up. This will
  10. # probably be useful other places, as well.
  11. data "aws_security_group" "typical-host" {
  12. name = "typical-host"
  13. vpc_id = var.vpc_id
  14. }
  15. # Use the default EBS key
  16. data "aws_kms_key" "ebs-key" {
  17. key_id = "alias/ebs_root_encrypt_decrypt"
  18. }
  19. resource "aws_network_interface" "instance" {
  20. subnet_id = var.public_subnets[0]
  21. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.searchhead_security_group.id]
  22. description = local.instance_name
  23. tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  24. }
  25. resource "aws_instance" "instance" {
  26. #availability_zone = var.azs[count.index % 2]
  27. tenancy = "default"
  28. ebs_optimized = true
  29. disable_api_termination = var.instance_termination_protection
  30. instance_initiated_shutdown_behavior = "stop"
  31. instance_type = var.instance_type
  32. key_name = "msoc-build"
  33. monitoring = false
  34. iam_instance_profile = module.instance_profile.profile_id
  35. metadata_options {
  36. http_tokens = "optional" # tfsec:ignore:aws-ec2-enforce-http-token-imds Splunk uses v1 by default. MSOCI-2150
  37. }
  38. ami = local.ami_map[local.ami_selection]
  39. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  40. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  41. # that could be removed.
  42. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  43. # These device definitions are optional, but added for clarity.
  44. root_block_device {
  45. volume_type = "gp3"
  46. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/"]
  47. delete_on_termination = true
  48. encrypted = true
  49. kms_key_id = data.aws_kms_key.ebs-key.arn
  50. }
  51. ebs_block_device {
  52. # /opt/splunk
  53. # Note: Not in AMI
  54. device_name = "/dev/xvdf"
  55. volume_type = "gp3"
  56. volume_size = var.splunk_volume_sizes["customer_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_type = "gp3"
  65. volume_size = var.splunk_volume_sizes["customer_searchhead"]["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_type = "gp3"
  80. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/home"]
  81. delete_on_termination = true
  82. encrypted = true
  83. kms_key_id = data.aws_kms_key.ebs-key.arn
  84. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  85. }
  86. ebs_block_device {
  87. # /var
  88. device_name = "/dev/xvdo"
  89. volume_type = "gp3"
  90. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/var"]
  91. delete_on_termination = true
  92. encrypted = true
  93. kms_key_id = data.aws_kms_key.ebs-key.arn
  94. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  95. }
  96. ebs_block_device {
  97. # /var/tmp
  98. device_name = "/dev/xvdp"
  99. volume_type = "gp3"
  100. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/var/tmp"]
  101. delete_on_termination = true
  102. encrypted = true
  103. kms_key_id = data.aws_kms_key.ebs-key.arn
  104. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  105. }
  106. ebs_block_device {
  107. # /var/log
  108. device_name = "/dev/xvdq"
  109. volume_type = "gp3"
  110. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/var/log"]
  111. delete_on_termination = true
  112. encrypted = true
  113. kms_key_id = data.aws_kms_key.ebs-key.arn
  114. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  115. }
  116. ebs_block_device {
  117. # /var/log/audit
  118. device_name = "/dev/xvdr"
  119. volume_type = "gp3"
  120. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/var/log/audit"]
  121. delete_on_termination = true
  122. encrypted = true
  123. kms_key_id = data.aws_kms_key.ebs-key.arn
  124. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  125. }
  126. ebs_block_device {
  127. # /tmp
  128. device_name = "/dev/xvds"
  129. volume_type = "gp3"
  130. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/tmp"]
  131. delete_on_termination = true
  132. encrypted = true
  133. kms_key_id = data.aws_kms_key.ebs-key.arn
  134. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  135. }
  136. network_interface {
  137. device_index = 0
  138. network_interface_id = aws_network_interface.instance.id
  139. }
  140. user_data = data.template_cloudinit_config.cloud-init.rendered
  141. tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  142. volume_tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  143. }
  144. module "private_dns_record" {
  145. source = "../../../submodules/dns/private_A_record"
  146. name = local.instance_name
  147. ip_addresses = [aws_instance.instance.private_ip]
  148. dns_info = var.dns_info
  149. reverse_enabled = var.reverse_enabled
  150. providers = {
  151. aws.c2 = aws.c2
  152. }
  153. }
  154. # Render a multi-part cloud-init config making use of the part
  155. # above, and other source files
  156. data "template_cloudinit_config" "cloud-init" {
  157. gzip = true
  158. base64_encode = true
  159. # Main cloud-config configuration file.
  160. part {
  161. filename = "init.cfg"
  162. content_type = "text/cloud-config"
  163. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  164. {
  165. hostname = local.instance_name
  166. fqdn = "${local.instance_name}.${var.dns_info["private"]["zone"]}"
  167. splunk_prefix = var.prefix
  168. environment = var.environment
  169. salt_master = var.salt_master
  170. proxy = var.proxy
  171. aws_partition = var.aws_partition
  172. aws_partition_alias = var.aws_partition_alias
  173. aws_region = var.aws_region
  174. }
  175. )
  176. }
  177. # mount /dev/xvdf at /opt/splunk
  178. part {
  179. content_type = "text/cloud-boothook"
  180. content = file("${path.module}/cloud-init/opt_splunk.boothook")
  181. }
  182. }
  183. ## Searchhead
  184. #
  185. # Summary:
  186. # Ingress:
  187. # tcp/8000 - Splunk Web - vpc-access, Phantom
  188. # tcp/8000 - Splunk Web - Entire VPC
  189. # tcp/8089 - Splunk API - vpc-access, Phantom
  190. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC
  191. # tcp/9997-9998 - Splunk Data - Entire VPC
  192. #
  193. # Ingress:
  194. # tcp/8089 - Splunk Web - vpc-system-services (for salt inventory and portal lambda)
  195. #
  196. # Egress:
  197. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC
  198. resource "aws_security_group" "searchhead_security_group" {
  199. name = "${var.prefix}_customer_searchhead_security_group"
  200. description = "Security Group for Splunk Customer Searchhead Instance(s)"
  201. vpc_id = var.vpc_id
  202. tags = merge(var.standard_tags, var.tags)
  203. }
  204. # Ingress
  205. resource "aws_security_group_rule" "splunk-web-in" {
  206. description = "Web access"
  207. type = "ingress"
  208. from_port = 8000
  209. to_port = 8000
  210. protocol = "tcp"
  211. cidr_blocks = toset(concat(var.cidr_map["vpc-access"],
  212. var.cidr_map["vpc-private-services"],
  213. [var.vpc_cidr],
  214. ))
  215. security_group_id = aws_security_group.searchhead_security_group.id
  216. }
  217. resource "aws_security_group_rule" "splunk-auth-in" {
  218. description = "Web access"
  219. type = "ingress"
  220. from_port = 10000
  221. to_port = 10000
  222. protocol = "tcp"
  223. cidr_blocks = toset(concat(var.cidr_map["vpc-access"],
  224. var.cidr_map["vpc-private-services"],
  225. [var.vpc_cidr],
  226. ))
  227. security_group_id = aws_security_group.searchhead_security_group.id
  228. }
  229. resource "aws_security_group_rule" "splunk-api-in" {
  230. description = "Splunk API"
  231. type = "ingress"
  232. from_port = 8089
  233. to_port = 8089
  234. protocol = "tcp"
  235. cidr_blocks = toset(concat(var.cidr_map["vpc-access"],
  236. var.cidr_map["vpc-private-services"],
  237. var.cidr_map["vpc-splunk"], # MC
  238. [var.vpc_cidr],
  239. var.cidr_map["vpc-system-services"], # for salt inventory and Portal lambda
  240. ))
  241. security_group_id = aws_security_group.searchhead_security_group.id
  242. }
  243. # Egress
  244. resource "aws_security_group_rule" "splunk-api-out" {
  245. description = "Splunk API Outbound to talk to indexers"
  246. type = "egress"
  247. from_port = 8089
  248. to_port = 8089
  249. protocol = "tcp"
  250. cidr_blocks = [var.vpc_cidr]
  251. security_group_id = aws_security_group.searchhead_security_group.id
  252. }
  253. resource "aws_security_group_rule" "splunk-data-out" {
  254. description = "Splunk Data Outbound to talk to own indexers"
  255. type = "egress"
  256. from_port = 9997
  257. to_port = 9998
  258. protocol = "tcp"
  259. cidr_blocks = [var.vpc_cidr]
  260. security_group_id = aws_security_group.searchhead_security_group.id
  261. }