workers.tf 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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
  29. iam_instance_profile = "msoc-default-instance-profile"
  30. ami = local.ami_map[local.ami_selection]
  31. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  32. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  33. # that could be removed.
  34. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  35. # These device definitions are optional, but added for clarity.
  36. root_block_device {
  37. volume_type = "gp2"
  38. #volume_size = Override via var?
  39. delete_on_termination = true
  40. encrypted = true
  41. kms_key_id = data.aws_kms_key.ebs-key.arn
  42. }
  43. network_interface {
  44. device_index = 0
  45. network_interface_id = aws_network_interface.worker[count.index].id
  46. }
  47. user_data = data.template_cloudinit_config.cloud-init-worker[count.index].rendered
  48. tags = merge(local.standard_tags,
  49. var.tags,
  50. {
  51. Name = "${local.instance_name_worker}-${count.index}",
  52. instance_num = count.index,
  53. instance_count = local.alsi_workers
  54. }
  55. )
  56. volume_tags = merge(local.standard_tags,
  57. var.tags,
  58. {
  59. Name = "${local.instance_name_worker}-${count.index}",
  60. instance_num = count.index,
  61. instance_count = local.alsi_workers
  62. }
  63. )
  64. }
  65. module "private_dns_record_worker" {
  66. count = local.alsi_workers
  67. source = "../../../submodules/dns/private_A_record"
  68. name = "${local.instance_name_worker}-${count.index}"
  69. ip_addresses = [aws_instance.worker[count.index].private_ip]
  70. dns_info = var.dns_info
  71. reverse_enabled = var.reverse_enabled
  72. providers = {
  73. aws.c2 = aws.c2
  74. }
  75. }
  76. # Render a multi-part cloud-init config making use of the part
  77. # above, and other source files
  78. data "template_cloudinit_config" "cloud-init-worker" {
  79. count = local.alsi_workers
  80. gzip = true
  81. base64_encode = true
  82. # Main cloud-config configuration file.
  83. part {
  84. filename = "init.cfg"
  85. content_type = "text/cloud-config"
  86. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  87. {
  88. hostname = "${local.instance_name_worker}-${count.index}"
  89. fqdn = "${local.instance_name_worker}-${count.index}.${var.dns_info["private"]["zone"]}"
  90. splunk_prefix = var.prefix
  91. environment = var.environment
  92. salt_master = local.salt_master
  93. proxy = local.proxy
  94. aws_partition = var.aws_partition
  95. aws_partition_alias = var.aws_partition_alias
  96. aws_region = var.aws_region
  97. }
  98. )
  99. }
  100. }
  101. ## ALSI Worker
  102. #
  103. # Summary:
  104. # Ingress:
  105. # 9000 - From ALBs
  106. # 9000 - From vpc-access
  107. # 8088 - From alb_hec
  108. # 9200 - From alb_elastic
  109. # 8088 - From alb_splunk_hec
  110. #
  111. # Egress:
  112. # 4200 - To master
  113. # 9997 - To Splunk
  114. resource "aws_security_group" "alsi_worker_security_group" {
  115. name_prefix = "${var.prefix}_alsi_worker_security_group" # name prefix and livecycle allow for smooth updates
  116. lifecycle { create_before_destroy = true } # handle updates gracefully
  117. description = "Security Group for Aggregated Log Source Ingestion"
  118. vpc_id = var.vpc_id
  119. tags = merge(local.standard_tags, var.tags)
  120. }
  121. # Ingress
  122. resource "aws_security_group_rule" "alsi_worker_alb_elastic1" {
  123. description = "Health Check"
  124. type = "ingress"
  125. from_port = 9000
  126. to_port = 9000
  127. protocol = "tcp"
  128. source_security_group_id = aws_security_group.alsi-alb-elastic-sg.id
  129. security_group_id = aws_security_group.alsi_worker_security_group.id
  130. }
  131. resource "aws_security_group_rule" "alsi_worker_alb_elastic2" {
  132. description = "Data Stream"
  133. type = "ingress"
  134. from_port = 9200
  135. to_port = 9200
  136. protocol = "tcp"
  137. source_security_group_id = aws_security_group.alsi-alb-elastic-sg.id
  138. security_group_id = aws_security_group.alsi_worker_security_group.id
  139. }
  140. # TODO: Repeat top 2 for HEC and S2S forwarders
  141. resource "aws_security_group_rule" "alsi_worker_vpn_in1" {
  142. description = "Web access"
  143. type = "ingress"
  144. from_port = 9000
  145. to_port = 9000
  146. protocol = "tcp"
  147. cidr_blocks = local.cidr_map["vpc-access"]
  148. security_group_id = aws_security_group.alsi_worker_security_group.id
  149. }
  150. resource "aws_security_group_rule" "alsi_worker_vpn_in2" {
  151. description = "Web access"
  152. type = "ingress"
  153. from_port = 9200
  154. to_port = 9200
  155. protocol = "tcp"
  156. cidr_blocks = local.cidr_map["vpc-access"]
  157. security_group_id = aws_security_group.alsi_worker_security_group.id
  158. }
  159. resource "aws_security_group_rule" "alsi_worker_vpn_in3" {
  160. description = "Test Splunk access"
  161. type = "ingress"
  162. from_port = 9997
  163. to_port = 9998
  164. protocol = "tcp"
  165. cidr_blocks = local.cidr_map["vpc-access"]
  166. security_group_id = aws_security_group.alsi_worker_security_group.id
  167. }
  168. resource "aws_security_group_rule" "alsi_worker_vpn_in4" {
  169. description = "Test HEC access"
  170. type = "ingress"
  171. from_port = 8088
  172. to_port = 8088
  173. protocol = "tcp"
  174. cidr_blocks = local.cidr_map["vpc-access"]
  175. security_group_id = aws_security_group.alsi_worker_security_group.id
  176. }
  177. resource "aws_security_group_rule" "alsi_worker_external_in" {
  178. # NLB requires the security group to allow access
  179. count = local.alsi_splunk_nlb ? 1 : 0
  180. type = "ingress"
  181. from_port = 9997
  182. to_port = 9998
  183. protocol = "tcp"
  184. cidr_blocks = toset(concat(local.cidr_map["vpc-access"], local.trusted_ips, local.splunk_data_sources))
  185. security_group_id = aws_security_group.alsi-alb-hec-sg.id
  186. }
  187. # Egress
  188. resource "aws_security_group_rule" "alsi-interconnections" {
  189. description = "cribl replication"
  190. type = "egress"
  191. from_port = 4200
  192. to_port = 4200
  193. protocol = "tcp"
  194. source_security_group_id = aws_security_group.alsi_master_security_group.id
  195. security_group_id = aws_security_group.alsi_worker_security_group.id
  196. }
  197. resource "aws_security_group_rule" "alsi-worker-splunk-mgmt" {
  198. description = "Management Access"
  199. type = "egress"
  200. from_port = 8089
  201. to_port = 8089
  202. protocol = "tcp"
  203. cidr_blocks = [var.vpc_cidr]
  204. security_group_id = aws_security_group.alsi_worker_security_group.id
  205. }
  206. resource "aws_security_group_rule" "alsi-worker-splunk-data" {
  207. description = "Management Access"
  208. type = "egress"
  209. from_port = 9997
  210. to_port = 9998
  211. protocol = "tcp"
  212. cidr_blocks = [var.vpc_cidr]
  213. security_group_id = aws_security_group.alsi_worker_security_group.id
  214. }