main.tf 9.3 KB

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