master.tf 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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( var.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 = var.instance_types["alsi-master"]
  20. key_name = "msoc-build"
  21. monitoring = false
  22. iam_instance_profile = "msoc-default-instance-profile"
  23. ami = local.ami_map[local.ami_selection]
  24. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  25. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  26. # that could be removed.
  27. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  28. # These device definitions are optional, but added for clarity.
  29. root_block_device {
  30. volume_type = "gp2"
  31. #volume_size = Override via var?
  32. delete_on_termination = true
  33. encrypted = true
  34. kms_key_id = data.aws_kms_key.ebs-key.arn
  35. }
  36. network_interface {
  37. device_index = 0
  38. network_interface_id = aws_network_interface.master.id
  39. }
  40. user_data = data.template_cloudinit_config.cloud-init-master.rendered
  41. tags = merge( var.standard_tags,
  42. var.tags,
  43. { Name = local.instance_name_master, }
  44. )
  45. volume_tags = merge( var.standard_tags,
  46. var.tags,
  47. { Name = local.instance_name_master, }
  48. )
  49. }
  50. module "private_dns_record_master" {
  51. source = "../../../submodules/dns/private_A_record"
  52. name = local.instance_name_master
  53. ip_addresses = [ aws_instance.master.private_ip ]
  54. dns_info = var.dns_info
  55. reverse_enabled = var.reverse_enabled
  56. providers = {
  57. aws.c2 = aws.c2
  58. }
  59. }
  60. data "template_file" "cloud-init-master" {
  61. # Should these be in a common directory? I suspect they'd be reusable
  62. template = file("${path.module}/cloud-init/cloud-init.tpl")
  63. vars = {
  64. hostname = local.instance_name_master
  65. fqdn = "${local.instance_name_master}.${var.dns_info["private"]["zone"]}"
  66. splunk_prefix = var.prefix
  67. environment = var.environment
  68. salt_master = var.salt_master
  69. proxy = var.proxy
  70. aws_partition = var.aws_partition
  71. aws_partition_alias = var.aws_partition_alias
  72. aws_region = var.aws_region
  73. }
  74. }
  75. # Render a multi-part cloud-init config making use of the part
  76. # above, and other source files
  77. data "template_cloudinit_config" "cloud-init-master" {
  78. gzip = true
  79. base64_encode = true
  80. # Main cloud-config configuration file.
  81. part {
  82. filename = "init.cfg"
  83. content_type = "text/cloud-config"
  84. content = data.template_file.cloud-init-master.rendered
  85. }
  86. }
  87. ## Master
  88. #
  89. # Summary:
  90. # Ingress:
  91. # 9000 - From private ALB
  92. # 9000 - From vpc-access
  93. #
  94. # Egress:
  95. # 9997/9998 - To Splunk
  96. resource "aws_security_group" "alsi_master_security_group" {
  97. name_prefix = "${ var.prefix }_alsi_master_security_group" # name prefix and livecycle allow for smooth updates
  98. lifecycle { create_before_destroy = true } # handle updates gracefully
  99. description = "Security Group for Aggregated Log Source Ingestion"
  100. vpc_id = var.vpc_id
  101. tags = merge(var.standard_tags, var.tags)
  102. }
  103. # Ingress
  104. resource "aws_security_group_rule" "alsi-master-alb-web-in" {
  105. description = "Web access"
  106. type = "ingress"
  107. from_port = 9000
  108. to_port = 9000
  109. protocol = "tcp"
  110. source_security_group_id = aws_security_group.alsi-master-alb-sg.id
  111. security_group_id = aws_security_group.alsi_master_security_group.id
  112. }
  113. resource "aws_security_group_rule" "alsi-master-vpn-web-in" {
  114. description = "Web access"
  115. type = "ingress"
  116. from_port = 9000
  117. to_port = 9000
  118. protocol = "tcp"
  119. cidr_blocks = var.cidr_map["vpc-access"]
  120. security_group_id = aws_security_group.alsi_master_security_group.id
  121. }
  122. resource "aws_security_group_rule" "alsi-master-interconnections" {
  123. description = "Cribl Replication"
  124. type = "ingress"
  125. from_port = 4200
  126. to_port = 4200
  127. protocol = "tcp"
  128. source_security_group_id = aws_security_group.alsi_worker_security_group.id
  129. security_group_id = aws_security_group.alsi_master_security_group.id
  130. }
  131. # Egress
  132. resource "aws_security_group_rule" "alsi-master-splunk-mgmt" {
  133. description = "Management Access"
  134. type = "egress"
  135. from_port = 8089
  136. to_port = 8089
  137. protocol = "tcp"
  138. cidr_blocks = [ var.vpc_cidr ]
  139. security_group_id = aws_security_group.alsi_master_security_group.id
  140. }
  141. resource "aws_security_group_rule" "alsi-master-splunk-data" {
  142. description = "Management Access"
  143. type = "egress"
  144. from_port = 9997
  145. to_port = 9998
  146. protocol = "tcp"
  147. cidr_blocks = [ var.vpc_cidr ]
  148. security_group_id = aws_security_group.alsi_master_security_group.id
  149. }