main.tf 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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(var.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 = var.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. ami = local.ami_map[local.ami_selection]
  37. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  38. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  39. # that could be removed.
  40. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  41. # These device definitions are optional, but added for clarity.
  42. root_block_device {
  43. volume_type = "gp2"
  44. volume_size = var.splunk_volume_sizes["searchhead"]["/"]
  45. delete_on_termination = true
  46. encrypted = true
  47. kms_key_id = data.aws_kms_key.ebs-key.arn
  48. }
  49. ebs_block_device {
  50. # /opt/splunk
  51. # Note: Not in AMI
  52. device_name = "/dev/xvdf"
  53. volume_size = var.splunk_volume_sizes["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_size = var.splunk_volume_sizes["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_size = var.splunk_volume_sizes["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_size = var.splunk_volume_sizes["searchhead"]["/var"]
  85. delete_on_termination = true
  86. encrypted = true
  87. kms_key_id = data.aws_kms_key.ebs-key.arn
  88. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  89. }
  90. ebs_block_device {
  91. # /var/tmp
  92. device_name = "/dev/xvdp"
  93. volume_size = var.splunk_volume_sizes["searchhead"]["/var/tmp"]
  94. delete_on_termination = true
  95. encrypted = true
  96. kms_key_id = data.aws_kms_key.ebs-key.arn
  97. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  98. }
  99. ebs_block_device {
  100. # /var/log
  101. device_name = "/dev/xvdq"
  102. volume_size = var.splunk_volume_sizes["searchhead"]["/var/log"]
  103. delete_on_termination = true
  104. encrypted = true
  105. kms_key_id = data.aws_kms_key.ebs-key.arn
  106. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  107. }
  108. ebs_block_device {
  109. # /var/log/audit
  110. device_name = "/dev/xvdr"
  111. volume_size = var.splunk_volume_sizes["searchhead"]["/var/log/audit"]
  112. delete_on_termination = true
  113. encrypted = true
  114. kms_key_id = data.aws_kms_key.ebs-key.arn
  115. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  116. }
  117. ebs_block_device {
  118. # /tmp
  119. device_name = "/dev/xvds"
  120. volume_size = var.splunk_volume_sizes["searchhead"]["/tmp"]
  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/xvds"].ebs.snapshot_id
  125. }
  126. network_interface {
  127. device_index = 0
  128. network_interface_id = aws_network_interface.instance.id
  129. }
  130. user_data = data.template_cloudinit_config.cloud-init.rendered
  131. tags = merge( var.standard_tags, var.tags, var.instance_tags, { Name = local.instance_name })
  132. volume_tags = merge( var.standard_tags, var.tags, { Name = local.instance_name })
  133. depends_on = [ module.moose_instance_profile, module.instance_profile ]
  134. }
  135. module "private_dns_record" {
  136. source = "../../../submodules/dns/private_A_record"
  137. name = local.instance_name
  138. ip_addresses = [ aws_instance.instance.private_ip ]
  139. dns_info = var.dns_info
  140. reverse_enabled = var.reverse_enabled
  141. providers = {
  142. aws.c2 = aws.c2
  143. }
  144. }
  145. # Render a multi-part cloud-init config making use of the part
  146. # above, and other source files
  147. data "template_cloudinit_config" "cloud-init" {
  148. gzip = true
  149. base64_encode = true
  150. # Main cloud-config configuration file.
  151. part {
  152. filename = "init.cfg"
  153. content_type = "text/cloud-config"
  154. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  155. {
  156. hostname = local.instance_name
  157. fqdn = "${local.instance_name}.${var.dns_info["private"]["zone"]}"
  158. splunk_prefix = var.prefix
  159. environment = var.environment
  160. salt_master = var.salt_master
  161. proxy = var.proxy
  162. aws_partition = var.aws_partition
  163. aws_partition_alias = var.aws_partition_alias
  164. aws_region = var.aws_region
  165. }
  166. )
  167. }
  168. # mount /dev/xvdf at /opt/splunk
  169. part {
  170. content_type = "text/cloud-boothook"
  171. content = file("${path.module}/cloud-init/opt_splunk.boothook")
  172. }
  173. }
  174. ## Searchhead
  175. #
  176. # Summary:
  177. # Ingress:
  178. # tcp/8000 - Splunk Web - vpc-access, legacy openvpn, legacy bastion, Phantom
  179. # tcp/8000 - Splunk Web - Entire VPC + var.splunk_legacy_cidr
  180. # tcp/8089 - Splunk API - vpc-access, legacy openvpn, legacy bastion, Phantom
  181. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC + var.splunk_legacy_cidr
  182. # tcp/9997-9998 - Splunk Data - Entire VPC + var.splunk_legacy_cidr
  183. #
  184. # Ingress:
  185. # tcp/8000 - Splunk Web - vpc-system-services (for salt inventory (moose only))
  186. # tcp/8089 - Splunk Web - vpc-system-services (for salt inventory and portal lambda)
  187. #
  188. # Egress:
  189. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC + var.splunk_legacy_cidr
  190. resource "aws_security_group" "searchhead_security_group" {
  191. name = "${ var.prefix }_searchhead_security_group"
  192. description = "Security Group for Splunk Searchhead Instance(s)"
  193. vpc_id = var.vpc_id
  194. tags = merge(var.standard_tags, var.tags)
  195. }
  196. # Ingress
  197. resource "aws_security_group_rule" "splunk-web-in" {
  198. description = "Web access"
  199. type = "ingress"
  200. from_port = 8000
  201. to_port = 8000
  202. protocol = "tcp"
  203. cidr_blocks = toset(concat(var.cidr_map["vpc-access"],
  204. var.cidr_map["vpc-private-services"],
  205. var.splunk_legacy_cidr,
  206. [ var.vpc_cidr ],
  207. local.is_moose ? var.cidr_map["vpc-system-services"] : [], # for salt inventory
  208. ))
  209. security_group_id = aws_security_group.searchhead_security_group.id
  210. }
  211. resource "aws_security_group_rule" "splunk-api-in" {
  212. description = "Splunk API"
  213. type = "ingress"
  214. from_port = 8089
  215. to_port = 8089
  216. protocol = "tcp"
  217. cidr_blocks = toset(concat(var.cidr_map["vpc-access"],
  218. var.cidr_map["vpc-private-services"],
  219. var.cidr_map["vpc-splunk"], # MC
  220. var.splunk_legacy_cidr,
  221. [ var.vpc_cidr ],
  222. var.cidr_map["vpc-system-services"], # for salt inventory and Portal lambda
  223. ))
  224. security_group_id = aws_security_group.searchhead_security_group.id
  225. }
  226. # Egress
  227. resource "aws_security_group_rule" "ssh-out" {
  228. count = length(var.splunk_legacy_cidr) > 0 ? 1 : 0
  229. description = "SSH to legacy splunk"
  230. type = "egress"
  231. from_port = 22
  232. to_port = 22
  233. protocol = "tcp"
  234. cidr_blocks = var.splunk_legacy_cidr
  235. security_group_id = aws_security_group.searchhead_security_group.id
  236. }
  237. resource "aws_security_group_rule" "splunk-api-out" {
  238. description = "Splunk API Outbound to talk to indexers"
  239. type = "egress"
  240. from_port = 8089
  241. to_port = 8089
  242. protocol = "tcp"
  243. cidr_blocks = toset(concat([ var.vpc_cidr ], var.splunk_legacy_cidr))
  244. security_group_id = aws_security_group.searchhead_security_group.id
  245. }
  246. resource "aws_security_group_rule" "splunk-api-out-to-all" {
  247. count = local.is_monitoring_console || local.is_fm_searchhead ? 1 : 0
  248. description = "Splunk API Outbound to talk to Other Segments"
  249. type = "egress"
  250. from_port = 8089
  251. to_port = 8089
  252. protocol = "tcp"
  253. cidr_blocks = [ "10.0.0.0/8" ]
  254. security_group_id = aws_security_group.searchhead_security_group.id
  255. }
  256. resource "aws_security_group_rule" "splunk-data-out" {
  257. description = "Splunk Data Outbound to talk to own indexers"
  258. type = "egress"
  259. from_port = 9997
  260. to_port = 9998
  261. protocol = "tcp"
  262. cidr_blocks = toset(concat([ var.vpc_cidr ], var.splunk_legacy_cidr))
  263. security_group_id = aws_security_group.searchhead_security_group.id
  264. }