master.tf 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Some instance variables
  2. locals {
  3. instance_name_master = "${var.prefix}-alsi-master"
  4. }
  5. resource "aws_network_interface" "master" {
  6. subnet_id = var.subnets[0]
  7. security_groups = [data.aws_security_group.typical-host.id, aws_security_group.alsi_master_security_group.id]
  8. description = local.instance_name_master
  9. tags = merge(local.standard_tags,
  10. var.tags,
  11. { Name = local.instance_name_master }
  12. )
  13. }
  14. resource "aws_instance" "master" {
  15. tenancy = "default"
  16. ebs_optimized = true
  17. disable_api_termination = var.instance_termination_protection
  18. instance_initiated_shutdown_behavior = "stop"
  19. instance_type = local.instance_types["alsi-master"]
  20. key_name = "msoc-build"
  21. monitoring = false # checkov:skip=CKV_AWS_126:Detailed monitoring not needed at this time
  22. iam_instance_profile = "msoc-default-instance-profile"
  23. metadata_options {
  24. http_endpoint = "enabled"
  25. # checkov:skip=CKV_AWS_79:see tfsec explanation
  26. # tfsec:ignore:aws-ec2-enforce-http-token-imds Saltstack doesn't use s3 sources appropriately; see https://github.com/saltstack/salt/issues/60668
  27. http_tokens = "optional"
  28. }
  29. ami = local.ami_map[local.ami_selection]
  30. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  31. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  32. # that could be removed.
  33. lifecycle { ignore_changes = [ami, key_name, user_data, ebs_block_device] }
  34. # These device definitions are optional, but added for clarity.
  35. root_block_device {
  36. volume_type = "gp3"
  37. #volume_size = Override via var?
  38. delete_on_termination = true
  39. encrypted = true
  40. kms_key_id = data.aws_kms_key.ebs-key.arn
  41. }
  42. network_interface {
  43. device_index = 0
  44. network_interface_id = aws_network_interface.master.id
  45. }
  46. user_data = data.template_cloudinit_config.cloud-init-master.rendered
  47. tags = merge(local.standard_tags,
  48. var.tags,
  49. { Name = local.instance_name_master, }
  50. )
  51. volume_tags = merge(local.standard_tags,
  52. var.tags,
  53. { Name = local.instance_name_master, }
  54. )
  55. }
  56. module "private_dns_record_master" {
  57. source = "../../../submodules/dns/private_A_record"
  58. name = local.instance_name_master
  59. ip_addresses = [aws_instance.master.private_ip]
  60. dns_info = var.dns_info
  61. reverse_enabled = var.reverse_enabled
  62. providers = {
  63. aws.c2 = aws.c2
  64. }
  65. }
  66. # Render a multi-part cloud-init config making use of the part
  67. # above, and other source files
  68. data "template_cloudinit_config" "cloud-init-master" {
  69. gzip = true
  70. base64_encode = true
  71. # Main cloud-config configuration file.
  72. part {
  73. filename = "init.cfg"
  74. content_type = "text/cloud-config"
  75. content = templatefile("${path.module}/cloud-init/cloud-init.tpl",
  76. {
  77. hostname = local.instance_name_master
  78. fqdn = "${local.instance_name_master}.${var.dns_info["private"]["zone"]}"
  79. splunk_prefix = var.prefix
  80. environment = var.environment
  81. salt_master = local.salt_master
  82. proxy = local.proxy
  83. aws_partition = var.aws_partition
  84. aws_partition_alias = var.aws_partition_alias
  85. aws_region = var.aws_region
  86. }
  87. )
  88. }
  89. }
  90. ## Master
  91. #
  92. # Summary:
  93. # Ingress:
  94. # 9000 - From private ALB
  95. # 9000 - From vpc-access
  96. #
  97. # Egress:
  98. # 9997/9998 - To Splunk
  99. resource "aws_security_group" "alsi_master_security_group" {
  100. name_prefix = "${var.prefix}_alsi_master_security_group" # name prefix and livecycle allow for smooth updates
  101. lifecycle { create_before_destroy = true } # handle updates gracefully
  102. description = "Security Group for Aggregated Log Source Ingestion"
  103. vpc_id = var.vpc_id
  104. tags = merge(local.standard_tags, var.tags)
  105. }
  106. #----------------------------------------------------------------------------
  107. # INGRESS
  108. #----------------------------------------------------------------------------
  109. resource "aws_security_group_rule" "alsi-master-alb-web-in" {
  110. description = "Web access"
  111. type = "ingress"
  112. from_port = 9000
  113. to_port = 9000
  114. protocol = "tcp"
  115. source_security_group_id = aws_security_group.alsi-master-alb-sg.id
  116. security_group_id = aws_security_group.alsi_master_security_group.id
  117. }
  118. resource "aws_security_group_rule" "alsi-master-vpn-web-in" {
  119. description = "Web access"
  120. type = "ingress"
  121. from_port = 9000
  122. to_port = 9000
  123. protocol = "tcp"
  124. cidr_blocks = local.cidr_map["vpc-access"]
  125. security_group_id = aws_security_group.alsi_master_security_group.id
  126. }
  127. resource "aws_security_group_rule" "alsi-master-interconnections" {
  128. description = "Cribl Replication"
  129. type = "ingress"
  130. from_port = 4200
  131. to_port = 4200
  132. protocol = "tcp"
  133. source_security_group_id = aws_security_group.alsi_worker_security_group.id
  134. security_group_id = aws_security_group.alsi_master_security_group.id
  135. }
  136. #----------------------------------------------------------------------------
  137. # EGRESS
  138. #----------------------------------------------------------------------------
  139. resource "aws_security_group_rule" "alsi-master-splunk-mgmt" {
  140. description = "Management Access"
  141. type = "egress"
  142. from_port = 8089
  143. to_port = 8089
  144. protocol = "tcp"
  145. cidr_blocks = [var.vpc_cidr]
  146. security_group_id = aws_security_group.alsi_master_security_group.id
  147. }
  148. resource "aws_security_group_rule" "alsi-master-splunk-data" {
  149. description = "Management Access"
  150. type = "egress"
  151. from_port = 9997
  152. to_port = 9998
  153. protocol = "tcp"
  154. cidr_blocks = [var.vpc_cidr]
  155. security_group_id = aws_security_group.alsi_master_security_group.id
  156. }