main.tf 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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(local.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 = local.instance_type
  30. key_name = "msoc-build"
  31. monitoring = false
  32. iam_instance_profile = module.instance_profile.profile_id
  33. metadata_options {
  34. http_endpoint = "enabled"
  35. http_tokens = "optional" # tfsec:ignore:aws-ec2-enforce-http-token-imds Splunk uses v1 by default. MSOCI-2150
  36. }
  37. ami = local.ami_map[local.ami_selection]
  38. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  39. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  40. # that could be removed.
  41. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  42. # These device definitions are optional, but added for clarity.
  43. root_block_device {
  44. volume_type = "gp2"
  45. volume_size = local.volume_sizes["/"]
  46. delete_on_termination = true
  47. encrypted = true
  48. kms_key_id = data.aws_kms_key.ebs-key.arn
  49. }
  50. ebs_block_device {
  51. # /opt/splunk
  52. # Note: Not in AMI
  53. device_name = "/dev/xvdf"
  54. volume_size = local.volume_sizes["/opt/splunk"]
  55. delete_on_termination = true
  56. encrypted = true
  57. kms_key_id = data.aws_kms_key.ebs-key.arn
  58. }
  59. ebs_block_device {
  60. # swap
  61. device_name = "/dev/xvdm"
  62. volume_size = local.volume_sizes["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_size = local.volume_sizes["/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_size = local.volume_sizes["/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_size = local.volume_sizes["/var/tmp"]
  95. delete_on_termination = true
  96. encrypted = true
  97. kms_key_id = data.aws_kms_key.ebs-key.arn
  98. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  99. }
  100. ebs_block_device {
  101. # /var/log
  102. device_name = "/dev/xvdq"
  103. volume_size = local.volume_sizes["/var/log"]
  104. delete_on_termination = true
  105. encrypted = true
  106. kms_key_id = data.aws_kms_key.ebs-key.arn
  107. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  108. }
  109. ebs_block_device {
  110. # /var/log/audit
  111. device_name = "/dev/xvdr"
  112. volume_size = local.volume_sizes["/var/log/audit"]
  113. delete_on_termination = true
  114. encrypted = true
  115. kms_key_id = data.aws_kms_key.ebs-key.arn
  116. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  117. }
  118. ebs_block_device {
  119. # /tmp
  120. device_name = "/dev/xvds"
  121. volume_size = local.volume_sizes["/tmp"]
  122. delete_on_termination = true
  123. encrypted = true
  124. kms_key_id = data.aws_kms_key.ebs-key.arn
  125. snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  126. }
  127. network_interface {
  128. device_index = 0
  129. network_interface_id = aws_network_interface.instance.id
  130. }
  131. user_data = data.template_cloudinit_config.cloud-init.rendered
  132. tags = merge(local.standard_tags, var.tags, { Name = local.instance_name })
  133. volume_tags = merge(local.standard_tags, var.tags, var.instance_tags, { Name = local.instance_name })
  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 = local.salt_master
  161. proxy = local.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. ## Cluster Master Security Group
  175. #
  176. # Summary:
  177. # Ingress:
  178. # tcp/8000 - Splunk Web - vpc-access, legacy openvpn, legacy bastion
  179. # tcp/8089 - Splunk API - vpc-access, legacy openvpn, legacy bastion, vpc-splunk, vpc-private-services
  180. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC + local.splunk_legacy_cidr
  181. # tcp/8089 - MOOSE ONLY - 10.0.0.0/8
  182. # Egress:
  183. # tcp/8089 - Splunk API + IDX Discovery - Entire VPC + local.splunk_legacy_cidr
  184. # tcp/9997-9998 - Splunk Data - Entire VPC + local.splunk_legacy_cidr
  185. #
  186. # In legacy, but not carried over:
  187. # Ingress:
  188. # tcp/9887 - IDX Replication - Entire VPC + local.splunk_legacy_cidr
  189. # tcp/8088 - Splunk HEC - Entire VPC + var.additional_source + local.splunk_legacy_cidr - TODO: Is this needed for CM?
  190. # tcp/9997-9998 - Splunk Data - Entire VPC + var.additional_source + local.splunk_legacy_cidr
  191. # Egress:
  192. # tcp/8088 - Entire VPC - Splunk HEC
  193. # tcp/9997-9998 - Entire VPC - Splunk Data
  194. # tcp/9887 - Entire VPC - IDX Replication
  195. #
  196. resource "aws_security_group" "cluster_master_security_group" {
  197. name = "cluster_master_security_group"
  198. description = "Security Group for Splunk Cluster Master Instance(s)"
  199. vpc_id = var.vpc_id
  200. tags = merge(local.standard_tags, var.tags)
  201. }
  202. resource "aws_security_group_rule" "splunk-web-in" {
  203. description = "Web access from bastions and vpn"
  204. type = "ingress"
  205. from_port = 8000
  206. to_port = 8000
  207. protocol = "tcp"
  208. #cidr_blocks = toset(concat(local.cidr_map["bastions"], local.cidr_map["vpns"]))
  209. cidr_blocks = local.cidr_map["vpc-access"]
  210. security_group_id = aws_security_group.cluster_master_security_group.id
  211. }
  212. resource "aws_security_group_rule" "splunk-api-in" {
  213. description = "Splunk API + Indexer Discovery"
  214. type = "ingress"
  215. from_port = 8089
  216. to_port = 8089
  217. protocol = "tcp"
  218. cidr_blocks = toset(concat(local.splunk_legacy_cidr, [var.vpc_cidr], local.cidr_map["vpc-access"], local.cidr_map["vpc-private-services"], local.cidr_map["vpc-splunk"]))
  219. security_group_id = aws_security_group.cluster_master_security_group.id
  220. }
  221. resource "aws_security_group_rule" "splunk-api-in-moose" {
  222. count = local.is_moose ? 1 : 0
  223. description = "Splunk API + Indexer Discovery - 10/8 for MOOSE ONLY"
  224. type = "ingress"
  225. from_port = 8089
  226. to_port = 8089
  227. protocol = "tcp"
  228. cidr_blocks = ["10.0.0.0/8"]
  229. security_group_id = aws_security_group.cluster_master_security_group.id
  230. }
  231. resource "aws_security_group_rule" "ssh-out" {
  232. count = length(local.splunk_legacy_cidr) > 0 ? 1 : 0
  233. description = "SSH to legacy splunk"
  234. type = "egress"
  235. from_port = 22
  236. to_port = 22
  237. protocol = "tcp"
  238. cidr_blocks = local.splunk_legacy_cidr
  239. security_group_id = aws_security_group.cluster_master_security_group.id
  240. }
  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 = toset(concat(local.splunk_legacy_cidr, [var.vpc_cidr]))
  248. security_group_id = aws_security_group.cluster_master_security_group.id
  249. }
  250. resource "aws_security_group_rule" "splunk-data-out" {
  251. description = "Splunk Data Outbound to record to local indexers"
  252. type = "egress"
  253. from_port = 9997
  254. to_port = 9998
  255. protocol = "tcp"
  256. cidr_blocks = toset(concat(local.splunk_legacy_cidr, [var.vpc_cidr]))
  257. security_group_id = aws_security_group.cluster_master_security_group.id
  258. }