workers.tf 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. # Some instance variables
  2. locals {
  3. instance_name_worker = "${ var.prefix }-alsi-worker"
  4. }
  5. resource "aws_network_interface" "worker" {
  6. count = var.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( var.standard_tags,
  11. var.tags,
  12. {
  13. Name = "${local.instance_name_worker}-${count.index}",
  14. instance_num = count.index,
  15. instance_count = var.alsi_workers
  16. }
  17. )
  18. }
  19. resource "aws_instance" "worker" {
  20. count = var.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 = var.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( var.standard_tags,
  49. var.tags,
  50. {
  51. Name = "${local.instance_name_worker}-${count.index}",
  52. instance_num = count.index,
  53. instance_count = var.alsi_workers
  54. }
  55. )
  56. volume_tags = merge( var.standard_tags,
  57. var.tags,
  58. {
  59. Name = "${local.instance_name_worker}-${count.index}",
  60. instance_num = count.index,
  61. instance_count = var.alsi_workers
  62. }
  63. )
  64. }
  65. module "private_dns_record_worker" {
  66. count = var.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. data "template_file" "cloud-init-worker" {
  77. count = var.alsi_workers
  78. # Should these be in a common directory? I suspect they'd be reusable
  79. template = file("${path.module}/cloud-init/cloud-init.tpl")
  80. vars = {
  81. hostname = "${local.instance_name_worker}-${count.index}"
  82. fqdn = "${local.instance_name_worker}-${count.index}.${var.dns_info["private"]["zone"]}"
  83. splunk_prefix = var.prefix
  84. environment = var.environment
  85. salt_master = var.salt_master
  86. proxy = var.proxy
  87. aws_partition = var.aws_partition
  88. aws_partition_alias = var.aws_partition_alias
  89. aws_region = var.aws_region
  90. }
  91. }
  92. # Render a multi-part cloud-init config making use of the part
  93. # above, and other source files
  94. data "template_cloudinit_config" "cloud-init-worker" {
  95. count = var.alsi_workers
  96. gzip = true
  97. base64_encode = true
  98. # Main cloud-config configuration file.
  99. part {
  100. filename = "init.cfg"
  101. content_type = "text/cloud-config"
  102. content = data.template_file.cloud-init-worker[count.index].rendered
  103. }
  104. }
  105. ## ALSI Worker
  106. #
  107. # Summary:
  108. # Ingress:
  109. # 9000 - From ALBs
  110. # 9000 - From vpc-access
  111. # 8088 - From alb_hec
  112. # 9200 - From alb_elastic
  113. # 8088 - From alb_splunk_hec
  114. #
  115. # Egress:
  116. # 4200 - To master
  117. # 9997 - To Splunk
  118. resource "aws_security_group" "alsi_worker_security_group" {
  119. name_prefix = "${ var.prefix }_alsi_worker_security_group" # name prefix and livecycle allow for smooth updates
  120. lifecycle { create_before_destroy = true } # handle updates gracefully
  121. description = "Security Group for Aggregated Log Source Ingestion"
  122. vpc_id = var.vpc_id
  123. tags = merge(var.standard_tags, var.tags)
  124. }
  125. # Ingress
  126. resource "aws_security_group_rule" "alsi_worker_alb_elastic1" {
  127. description = "Health Check"
  128. type = "ingress"
  129. from_port = 9000
  130. to_port = 9000
  131. protocol = "tcp"
  132. source_security_group_id = aws_security_group.alsi-alb-elastic-sg.id
  133. security_group_id = aws_security_group.alsi_worker_security_group.id
  134. }
  135. resource "aws_security_group_rule" "alsi_worker_alb_elastic2" {
  136. description = "Data Stream"
  137. type = "ingress"
  138. from_port = 9200
  139. to_port = 9200
  140. protocol = "tcp"
  141. source_security_group_id = aws_security_group.alsi-alb-elastic-sg.id
  142. security_group_id = aws_security_group.alsi_worker_security_group.id
  143. }
  144. # TODO: Repeat top 2 for HEC and S2S forwarders
  145. resource "aws_security_group_rule" "alsi_worker_vpn_in1" {
  146. description = "Web access"
  147. type = "ingress"
  148. from_port = 9000
  149. to_port = 9000
  150. protocol = "tcp"
  151. cidr_blocks = var.cidr_map["vpc-access"]
  152. security_group_id = aws_security_group.alsi_worker_security_group.id
  153. }
  154. resource "aws_security_group_rule" "alsi_worker_vpn_in2" {
  155. description = "Web access"
  156. type = "ingress"
  157. from_port = 9200
  158. to_port = 9200
  159. protocol = "tcp"
  160. cidr_blocks = var.cidr_map["vpc-access"]
  161. security_group_id = aws_security_group.alsi_worker_security_group.id
  162. }
  163. resource "aws_security_group_rule" "alsi_worker_vpn_in3" {
  164. description = "Test Splunk access"
  165. type = "ingress"
  166. from_port = 9997
  167. to_port = 9998
  168. protocol = "tcp"
  169. cidr_blocks = var.cidr_map["vpc-access"]
  170. security_group_id = aws_security_group.alsi_worker_security_group.id
  171. }
  172. resource "aws_security_group_rule" "alsi_worker_vpn_in4" {
  173. description = "Test HEC access"
  174. type = "ingress"
  175. from_port = 8088
  176. to_port = 8088
  177. protocol = "tcp"
  178. cidr_blocks = var.cidr_map["vpc-access"]
  179. security_group_id = aws_security_group.alsi_worker_security_group.id
  180. }
  181. resource "aws_security_group_rule" "alsi_worker_external_in" {
  182. # NLB requires the security group to allow access
  183. count = var.alsi_splunk_nlb ? 1 : 0
  184. type = "ingress"
  185. from_port = 9997
  186. to_port = 9998
  187. protocol = "tcp"
  188. cidr_blocks = toset(concat(var.cidr_map["vpc-access"], var.trusted_ips, var.splunk_data_sources))
  189. security_group_id = aws_security_group.alsi-alb-hec-sg.id
  190. }
  191. # Egress
  192. resource "aws_security_group_rule" "alsi-interconnections" {
  193. description = "cribl replication"
  194. type = "egress"
  195. from_port = 4200
  196. to_port = 4200
  197. protocol = "tcp"
  198. source_security_group_id = aws_security_group.alsi_master_security_group.id
  199. security_group_id = aws_security_group.alsi_worker_security_group.id
  200. }
  201. resource "aws_security_group_rule" "alsi-worker-splunk-mgmt" {
  202. description = "Management Access"
  203. type = "egress"
  204. from_port = 8089
  205. to_port = 8089
  206. protocol = "tcp"
  207. cidr_blocks = [ var.vpc_cidr ]
  208. security_group_id = aws_security_group.alsi_worker_security_group.id
  209. }
  210. resource "aws_security_group_rule" "alsi-worker-splunk-data" {
  211. description = "Management Access"
  212. type = "egress"
  213. from_port = 9997
  214. to_port = 9998
  215. protocol = "tcp"
  216. cidr_blocks = [ var.vpc_cidr ]
  217. security_group_id = aws_security_group.alsi_worker_security_group.id
  218. }