workers.tf 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # Some instance variables
  2. locals {
  3. instance_name_worker = "${var.prefix}-alsi-worker"
  4. }
  5. resource "aws_network_interface" "worker" {
  6. count = local.alsi_workers
  7. subnet_id = var.subnets[count.index % length(var.subnets)] # evenly distributed across subnets
  8. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.alsi_worker_security_group.id]
  9. description = "${local.instance_name_worker}-${count.index}"
  10. tags = merge(local.standard_tags,
  11. var.tags,
  12. {
  13. Name = "${local.instance_name_worker}-${count.index}",
  14. instance_num = count.index,
  15. instance_count = local.alsi_workers
  16. }
  17. )
  18. }
  19. resource "aws_instance" "worker" {
  20. count = local.alsi_workers
  21. #availability_zone = var.azs[count.index % 2] # automatically determined by the network interface
  22. tenancy = "default"
  23. ebs_optimized = true
  24. disable_api_termination = var.instance_termination_protection
  25. instance_initiated_shutdown_behavior = "stop"
  26. instance_type = local.instance_types["alsi-worker"]
  27. key_name = "msoc-build"
  28. monitoring = false # checkov:skip=CKV_AWS_126:Detailed monitoring not needed at this time
  29. iam_instance_profile = "msoc-default-instance-profile"
  30. metadata_options {
  31. http_endpoint = "enabled"
  32. # checkov:skip=CKV_AWS_79:see tfsec explanation
  33. # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately; see https://github.com/saltstack/salt/issues/60668
  34. http_tokens = "optional"
  35. }
  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 = "gp3"
  44. #volume_size = Override via var?
  45. delete_on_termination = true
  46. encrypted = true
  47. kms_key_id = data.aws_kms_key.ebs-key.arn
  48. }
  49. network_interface {
  50. device_index = 0
  51. network_interface_id = aws_network_interface.worker[count.index].id
  52. }
  53. user_data = data.template_cloudinit_config.cloud-init-worker[count.index].rendered
  54. tags = merge(local.standard_tags,
  55. var.tags,
  56. {
  57. Name = "${local.instance_name_worker}-${count.index}",
  58. instance_num = count.index,
  59. instance_count = local.alsi_workers
  60. }
  61. )
  62. volume_tags = merge(local.standard_tags,
  63. var.tags,
  64. {
  65. Name = "${local.instance_name_worker}-${count.index}",
  66. instance_num = count.index,
  67. instance_count = local.alsi_workers
  68. }
  69. )
  70. }
  71. module "private_dns_record_worker" {
  72. count = local.alsi_workers
  73. source = "../../../submodules/dns/private_A_record"
  74. name = "${local.instance_name_worker}-${count.index}"
  75. ip_addresses = [aws_instance.worker[count.index].private_ip]
  76. dns_info = var.dns_info
  77. reverse_enabled = var.reverse_enabled
  78. providers = {
  79. aws.c2 = aws.c2
  80. }
  81. }
  82. # Render a multi-part cloud-init config making use of the part
  83. # above, and other source files
  84. data "template_cloudinit_config" "cloud-init-worker" {
  85. count = local.alsi_workers
  86. gzip = true
  87. base64_encode = true
  88. # Main cloud-config configuration file.
  89. part {
  90. filename = "init.cfg"
  91. content_type = "text/cloud-config"
  92. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  93. {
  94. hostname = "${local.instance_name_worker}-${count.index}"
  95. fqdn = "${local.instance_name_worker}-${count.index}.${var.dns_info["private"]["zone"]}"
  96. splunk_prefix = var.prefix
  97. environment = var.environment
  98. salt_master = local.salt_master
  99. proxy = local.proxy
  100. aws_partition = var.aws_partition
  101. aws_partition_alias = var.aws_partition_alias
  102. aws_region = var.aws_region
  103. }
  104. )
  105. }
  106. }
  107. ## ALSI Worker
  108. #
  109. # Summary:
  110. # Ingress:
  111. # 9000 - From ALBs
  112. # 9000 - From vpc-access
  113. # 8088 - From alb_hec
  114. # 9200 - From alb_elastic
  115. # 8088 - From alb_splunk_hec
  116. #
  117. # Egress:
  118. # 4200 - To master
  119. # 9997 - To Splunk
  120. resource "aws_security_group" "alsi_worker_security_group" {
  121. name_prefix = "${var.prefix}_alsi_worker_security_group" # name prefix and livecycle allow for smooth updates
  122. lifecycle { create_before_destroy = true } # handle updates gracefully
  123. description = "Security Group for Aggregated Log Source Ingestion"
  124. vpc_id = var.vpc_id
  125. tags = merge(local.standard_tags, var.tags)
  126. }
  127. #----------------------------------------------------------------------------
  128. # INGRESS
  129. #----------------------------------------------------------------------------
  130. resource "aws_security_group_rule" "alsi_worker_alb_elastic1" {
  131. description = "Health Check"
  132. type = "ingress"
  133. from_port = 9000
  134. to_port = 9000
  135. protocol = "tcp"
  136. source_security_group_id = aws_security_group.alsi-alb-elastic-sg.id
  137. security_group_id = aws_security_group.alsi_worker_security_group.id
  138. }
  139. resource "aws_security_group_rule" "alsi_worker_alb_elastic2" {
  140. description = "Data Stream"
  141. type = "ingress"
  142. from_port = 9200
  143. to_port = 9200
  144. protocol = "tcp"
  145. source_security_group_id = aws_security_group.alsi-alb-elastic-sg.id
  146. security_group_id = aws_security_group.alsi_worker_security_group.id
  147. }
  148. # TODO: Repeat top 2 for HEC and S2S forwarders
  149. resource "aws_security_group_rule" "alsi_worker_vpn_in1" {
  150. description = "Web access"
  151. type = "ingress"
  152. from_port = 9000
  153. to_port = 9000
  154. protocol = "tcp"
  155. cidr_blocks = local.cidr_map["vpc-access"]
  156. security_group_id = aws_security_group.alsi_worker_security_group.id
  157. }
  158. resource "aws_security_group_rule" "alsi_worker_vpn_in2" {
  159. description = "Web access"
  160. type = "ingress"
  161. from_port = 9200
  162. to_port = 9200
  163. protocol = "tcp"
  164. cidr_blocks = local.cidr_map["vpc-access"]
  165. security_group_id = aws_security_group.alsi_worker_security_group.id
  166. }
  167. resource "aws_security_group_rule" "alsi_worker_vpn_in3" {
  168. description = "Test Splunk access"
  169. type = "ingress"
  170. from_port = 9997
  171. to_port = 9998
  172. protocol = "tcp"
  173. cidr_blocks = local.cidr_map["vpc-access"]
  174. security_group_id = aws_security_group.alsi_worker_security_group.id
  175. }
  176. resource "aws_security_group_rule" "alsi_worker_vpn_in4" {
  177. description = "Test HEC access"
  178. type = "ingress"
  179. from_port = 8088
  180. to_port = 8088
  181. protocol = "tcp"
  182. cidr_blocks = local.cidr_map["vpc-access"]
  183. security_group_id = aws_security_group.alsi_worker_security_group.id
  184. }
  185. resource "aws_security_group_rule" "alsi_worker_external_in" {
  186. # NLB requires the security group to allow access
  187. description = "Test Splunk access"
  188. count = local.alsi_splunk_nlb ? 1 : 0
  189. type = "ingress"
  190. from_port = 9997
  191. to_port = 9998
  192. protocol = "tcp"
  193. cidr_blocks = toset(concat(local.cidr_map["vpc-access"], local.trusted_ips, local.splunk_data_sources))
  194. security_group_id = aws_security_group.alsi-alb-hec-sg.id
  195. }
  196. #----------------------------------------------------------------------------
  197. # EGRESS
  198. #----------------------------------------------------------------------------
  199. resource "aws_security_group_rule" "alsi-interconnections" {
  200. description = "Cribl Replication"
  201. type = "egress"
  202. from_port = 4200
  203. to_port = 4200
  204. protocol = "tcp"
  205. source_security_group_id = aws_security_group.alsi_master_security_group.id
  206. security_group_id = aws_security_group.alsi_worker_security_group.id
  207. }
  208. resource "aws_security_group_rule" "alsi-worker-splunk-mgmt" {
  209. description = "Management Access"
  210. type = "egress"
  211. from_port = 8089
  212. to_port = 8089
  213. protocol = "tcp"
  214. cidr_blocks = [var.vpc_cidr]
  215. security_group_id = aws_security_group.alsi_worker_security_group.id
  216. }
  217. resource "aws_security_group_rule" "alsi-worker-splunk-data" {
  218. description = "Management Access"
  219. type = "egress"
  220. from_port = 9997
  221. to_port = 9998
  222. protocol = "tcp"
  223. cidr_blocks = [var.vpc_cidr]
  224. security_group_id = aws_security_group.alsi_worker_security_group.id
  225. }