main.tf 9.7 KB

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