main.tf 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. instance_name = "${ var.prefix }-splunk-cm"
  5. is_moose = length(regexall("moose", var.prefix)) > 0 ? true : false
  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.subnets[0]
  19. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.cluster_master_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 = module.instance_profile.profile_id
  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 = "gp2"
  41. volume_size = var.splunk_volume_sizes["cluster_master"]["/"]
  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_size = var.splunk_volume_sizes["cluster_master"]["/opt/splunk"]
  51. delete_on_termination = true
  52. encrypted = true
  53. kms_key_id = data.aws_kms_key.ebs-key.arn
  54. }
  55. ebs_block_device {
  56. # swap
  57. device_name = "/dev/xvdm"
  58. volume_size = var.splunk_volume_sizes["cluster_master"]["swap"]
  59. delete_on_termination = true
  60. encrypted = true
  61. kms_key_id = data.aws_kms_key.ebs-key.arn
  62. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  63. # This may prompt replacement when the AMI is updated.
  64. # See:
  65. # https://github.com/hashicorp/terraform/issues/19958
  66. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  67. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  68. }
  69. ebs_block_device {
  70. # /home
  71. device_name = "/dev/xvdn"
  72. volume_size = var.splunk_volume_sizes["cluster_master"]["/home"]
  73. delete_on_termination = true
  74. encrypted = true
  75. kms_key_id = data.aws_kms_key.ebs-key.arn
  76. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  77. }
  78. ebs_block_device {
  79. # /var
  80. device_name = "/dev/xvdo"
  81. volume_size = var.splunk_volume_sizes["cluster_master"]["/var"]
  82. delete_on_termination = true
  83. encrypted = true
  84. kms_key_id = data.aws_kms_key.ebs-key.arn
  85. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  86. }
  87. ebs_block_device {
  88. # /var/tmp
  89. device_name = "/dev/xvdp"
  90. volume_size = var.splunk_volume_sizes["cluster_master"]["/var/tmp"]
  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/xvdp"].ebs.snapshot_id
  95. }
  96. ebs_block_device {
  97. # /var/log
  98. device_name = "/dev/xvdq"
  99. volume_size = var.splunk_volume_sizes["cluster_master"]["/var/log"]
  100. delete_on_termination = true
  101. encrypted = true
  102. kms_key_id = data.aws_kms_key.ebs-key.arn
  103. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  104. }
  105. ebs_block_device {
  106. # /var/log/audit
  107. device_name = "/dev/xvdr"
  108. volume_size = var.splunk_volume_sizes["cluster_master"]["/var/log/audit"]
  109. delete_on_termination = true
  110. encrypted = true
  111. kms_key_id = data.aws_kms_key.ebs-key.arn
  112. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  113. }
  114. ebs_block_device {
  115. # /tmp
  116. device_name = "/dev/xvds"
  117. volume_size = var.splunk_volume_sizes["cluster_master"]["/tmp"]
  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/xvds"].ebs.snapshot_id
  122. }
  123. network_interface {
  124. device_index = 0
  125. network_interface_id = aws_network_interface.instance.id
  126. }
  127. user_data = data.template_cloudinit_config.cloud-init.rendered
  128. tags = merge( var.standard_tags, var.tags, { Name = local.instance_name })
  129. volume_tags = merge( var.standard_tags, var.tags, var.instance_tags, { Name = local.instance_name })
  130. }
  131. module "private_dns_record" {
  132. source = "../../../submodules/dns/private_A_record"
  133. name = local.instance_name
  134. ip_addresses = [ aws_instance.instance.private_ip ]
  135. dns_info = var.dns_info
  136. reverse_enabled = var.reverse_enabled
  137. providers = {
  138. aws.c2 = aws.c2
  139. }
  140. }
  141. # Render a multi-part cloud-init config making use of the part
  142. # above, and other source files
  143. data "template_cloudinit_config" "cloud-init" {
  144. gzip = true
  145. base64_encode = true
  146. # Main cloud-config configuration file.
  147. part {
  148. filename = "init.cfg"
  149. content_type = "text/cloud-config"
  150. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  151. {
  152. hostname = local.instance_name
  153. fqdn = "${local.instance_name}.${var.dns_info["private"]["zone"]}"
  154. splunk_prefix = var.prefix
  155. environment = var.environment
  156. salt_master = var.salt_master
  157. proxy = var.proxy
  158. aws_partition = var.aws_partition
  159. aws_partition_alias = var.aws_partition_alias
  160. aws_region = var.aws_region
  161. }
  162. )
  163. }
  164. # mount /dev/xvdf at /opt/splunk
  165. part {
  166. content_type = "text/cloud-boothook"
  167. content = file("${path.module}/cloud-init/opt_splunk.boothook")
  168. }
  169. }
  170. ## Cluster Master Security Group
  171. #
  172. # Summary:
  173. # Ingress:
  174. # tcp/8000 - Splunk Web - vpc-access, legacy openvpn, legacy bastion
  175. # tcp/8089 - Splunk API - vpc-access, legacy openvpn, legacy bastion, vpc-splunk, vpc-private-services
  176. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC + var.splunk_legacy_cidr
  177. # tcp/8089 - MOOSE ONLY - 10.0.0.0/8
  178. # Egress:
  179. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC + var.splunk_legacy_cidr
  180. # tcp/9997-9998 - Splunk Data - Entire VPC + var.splunk_legacy_cidr
  181. #
  182. # In legacy, but not carried over:
  183. # Ingress:
  184. # tcp/9887 - IDX Replication - Entire VPC + var.splunk_legacy_cidr
  185. # tcp/8088 - Splunk HEC - Entire VPC + var.additional_source + var.splunk_legacy_cidr - TODO: Is this needed for CM?
  186. # tcp/9997-9998 - Splunk Data - Entire VPC + var.additional_source + var.splunk_legacy_cidr
  187. # Egress:
  188. # tcp/8088 - Entire VPC - Splunk HEC
  189. # tcp/9997-9998 - Entire VPC - Splunk Data
  190. # tcp/9887 - Entire VPC - IDX Replication
  191. #
  192. resource "aws_security_group" "cluster_master_security_group" {
  193. name = "cluster_master_security_group"
  194. description = "Security Group for Splunk Cluster Master Instance(s)"
  195. vpc_id = var.vpc_id
  196. tags = merge(var.standard_tags, var.tags)
  197. }
  198. resource "aws_security_group_rule" "splunk-web-in" {
  199. description = "Web access from bastions and vpn"
  200. type = "ingress"
  201. from_port = 8000
  202. to_port = 8000
  203. protocol = "tcp"
  204. #cidr_blocks = toset(concat(var.cidr_map["bastions"], var.cidr_map["vpns"]))
  205. cidr_blocks = var.cidr_map["vpc-access"]
  206. security_group_id = aws_security_group.cluster_master_security_group.id
  207. }
  208. resource "aws_security_group_rule" "splunk-api-in" {
  209. description = "Splunk API + Indexer Discovery"
  210. type = "ingress"
  211. from_port = 8089
  212. to_port = 8089
  213. protocol = "tcp"
  214. cidr_blocks = toset(concat(var.splunk_legacy_cidr, [ var.vpc_cidr ], var.cidr_map["vpc-access"], var.cidr_map["vpc-private-services"], var.cidr_map["vpc-splunk"]))
  215. security_group_id = aws_security_group.cluster_master_security_group.id
  216. }
  217. resource "aws_security_group_rule" "splunk-api-in-moose" {
  218. count = local.is_moose ? 1 : 0
  219. description = "Splunk API + Indexer Discovery - 10/8 for MOOSE ONLY"
  220. type = "ingress"
  221. from_port = 8089
  222. to_port = 8089
  223. protocol = "tcp"
  224. cidr_blocks = [ "10.0.0.0/8" ]
  225. security_group_id = aws_security_group.cluster_master_security_group.id
  226. }
  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.cluster_master_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.splunk_legacy_cidr, [ var.vpc_cidr ]))
  244. security_group_id = aws_security_group.cluster_master_security_group.id
  245. }
  246. resource "aws_security_group_rule" "splunk-data-out" {
  247. description = "Splunk Data Outbound to record to local indexers"
  248. type = "egress"
  249. from_port = 9997
  250. to_port = 9998
  251. protocol = "tcp"
  252. cidr_blocks = toset(concat(var.splunk_legacy_cidr, [ var.vpc_cidr ]))
  253. security_group_id = aws_security_group.cluster_master_security_group.id
  254. }