main.tf 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.alb_name != "" ? var.alb_name : "${ var.prefix }-splunk"
  6. }
  7. # Rather than pass in the aws security group, we just look it up. This will
  8. # probably be useful other places, as well.
  9. data "aws_security_group" "typical-host" {
  10. name = "typical-host"
  11. vpc_id = var.vpc_id
  12. }
  13. # Use the default EBS key
  14. data "aws_kms_key" "ebs-key" {
  15. key_id = "alias/ebs_root_encrypt_decrypt"
  16. }
  17. resource "aws_network_interface" "instance" {
  18. subnet_id = var.public_subnets[0]
  19. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.searchhead_security_group.id ]
  20. description = local.instance_name
  21. tags = merge(var.standard_tags, var.tags, { Name = local.instance_name })
  22. }
  23. resource "aws_instance" "instance" {
  24. #availability_zone = var.azs[count.index % 2]
  25. tenancy = "default"
  26. ebs_optimized = true
  27. disable_api_termination = var.instance_termination_protection
  28. instance_initiated_shutdown_behavior = "stop"
  29. instance_type = var.instance_type
  30. key_name = "msoc-build"
  31. monitoring = false
  32. iam_instance_profile = "msoc-default-instance-profile"
  33. ami = local.ami_map[local.ami_selection]
  34. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  35. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  36. # that could be removed.
  37. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  38. # These device definitions are optional, but added for clarity.
  39. root_block_device {
  40. volume_type = "gp3"
  41. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/"]
  42. delete_on_termination = true
  43. encrypted = true
  44. kms_key_id = data.aws_kms_key.ebs-key.arn
  45. }
  46. ebs_block_device {
  47. # /opt/splunk
  48. # Note: Not in AMI
  49. device_name = "/dev/xvdf"
  50. volume_type = "gp3"
  51. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/opt/splunk"]
  52. delete_on_termination = true
  53. encrypted = true
  54. kms_key_id = data.aws_kms_key.ebs-key.arn
  55. }
  56. ebs_block_device {
  57. # swap
  58. device_name = "/dev/xvdm"
  59. volume_type = "gp3"
  60. volume_size = var.splunk_volume_sizes["customer_searchhead"]["swap"]
  61. delete_on_termination = true
  62. encrypted = true
  63. kms_key_id = data.aws_kms_key.ebs-key.arn
  64. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  65. # This may prompt replacement when the AMI is updated.
  66. # See:
  67. # https://github.com/hashicorp/terraform/issues/19958
  68. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  69. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  70. }
  71. ebs_block_device {
  72. # /home
  73. device_name = "/dev/xvdn"
  74. volume_type = "gp3"
  75. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/home"]
  76. delete_on_termination = true
  77. encrypted = true
  78. kms_key_id = data.aws_kms_key.ebs-key.arn
  79. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  80. }
  81. ebs_block_device {
  82. # /var
  83. device_name = "/dev/xvdo"
  84. volume_type = "gp3"
  85. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/var"]
  86. delete_on_termination = true
  87. encrypted = true
  88. kms_key_id = data.aws_kms_key.ebs-key.arn
  89. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  90. }
  91. ebs_block_device {
  92. # /var/tmp
  93. device_name = "/dev/xvdp"
  94. volume_type = "gp3"
  95. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/var/tmp"]
  96. delete_on_termination = true
  97. encrypted = true
  98. kms_key_id = data.aws_kms_key.ebs-key.arn
  99. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  100. }
  101. ebs_block_device {
  102. # /var/log
  103. device_name = "/dev/xvdq"
  104. volume_type = "gp3"
  105. volume_size = var.splunk_volume_sizes["customer_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_type = "gp3"
  115. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/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_type = "gp3"
  125. volume_size = var.splunk_volume_sizes["customer_searchhead"]["/tmp"]
  126. delete_on_termination = true
  127. encrypted = true
  128. kms_key_id = data.aws_kms_key.ebs-key.arn
  129. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  130. }
  131. network_interface {
  132. device_index = 0
  133. network_interface_id = aws_network_interface.instance.id
  134. }
  135. user_data = data.template_cloudinit_config.cloud-init.rendered
  136. tags = merge( var.standard_tags, var.tags, { Name = local.instance_name })
  137. volume_tags = merge( var.standard_tags, var.tags, { Name = local.instance_name })
  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. data "template_file" "cloud-init" {
  150. # Should these be in a common directory? I suspect they'd be reusable
  151. template = file("${path.module}/cloud-init/cloud-init.tpl")
  152. vars = {
  153. hostname = local.instance_name
  154. fqdn = "${local.instance_name}.${var.dns_info["private"]["zone"]}"
  155. splunk_prefix = var.prefix
  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" "cloud-init" {
  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.cloud-init.rendered
  174. }
  175. # mount /dev/xvdf at /opt/splunk
  176. part {
  177. content_type = "text/cloud-boothook"
  178. content = file("${path.module}/cloud-init/opt_splunk.boothook")
  179. }
  180. }
  181. ## Searchhead
  182. #
  183. # Summary:
  184. # Ingress:
  185. # tcp/8000 - Splunk Web - vpc-access, Phantom
  186. # tcp/8000 - Splunk Web - Entire VPC
  187. # tcp/8089 - Splunk API - vpc-access, Phantom
  188. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC
  189. # tcp/9997-9998 - Splunk Data - Entire VPC
  190. #
  191. # Ingress:
  192. # tcp/8089 - Splunk Web - vpc-system-services (for salt inventory and portal lambda)
  193. #
  194. # Egress:
  195. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC
  196. resource "aws_security_group" "searchhead_security_group" {
  197. name = "${ var.prefix }_customer_searchhead_security_group"
  198. description = "Security Group for Splunk Customer Searchhead Instance(s)"
  199. vpc_id = var.vpc_id
  200. tags = merge(var.standard_tags, var.tags)
  201. }
  202. # Ingress
  203. resource "aws_security_group_rule" "splunk-web-in" {
  204. description = "Web access"
  205. type = "ingress"
  206. from_port = 8000
  207. to_port = 8000
  208. protocol = "tcp"
  209. cidr_blocks = toset(concat(var.cidr_map["vpc-access"],
  210. var.cidr_map["vpc-private-services"],
  211. [ var.vpc_cidr ],
  212. ))
  213. security_group_id = aws_security_group.searchhead_security_group.id
  214. }
  215. resource "aws_security_group_rule" "splunk-api-in" {
  216. description = "Splunk API"
  217. type = "ingress"
  218. from_port = 8089
  219. to_port = 8089
  220. protocol = "tcp"
  221. cidr_blocks = toset(concat(var.cidr_map["vpc-access"],
  222. var.cidr_map["vpc-private-services"],
  223. var.cidr_map["vpc-splunk"], # MC
  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" "splunk-api-out" {
  231. description = "Splunk API Outbound to talk to indexers"
  232. type = "egress"
  233. from_port = 8089
  234. to_port = 8089
  235. protocol = "tcp"
  236. cidr_blocks = [ var.vpc_cidr ]
  237. security_group_id = aws_security_group.searchhead_security_group.id
  238. }
  239. resource "aws_security_group_rule" "splunk-data-out" {
  240. description = "Splunk Data Outbound to talk to own indexers"
  241. type = "egress"
  242. from_port = 9997
  243. to_port = 9998
  244. protocol = "tcp"
  245. cidr_blocks = [ var.vpc_cidr ]
  246. security_group_id = aws_security_group.searchhead_security_group.id
  247. }