main.tf 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Some instance variables
  2. locals {
  3. ami_selection = "minion" # master, minion, ...
  4. instance_name = "${ var.prefix }-alsi"
  5. is_moose = length(regexall("moose", var.prefix)) > 0 ? true : false
  6. }
  7. # Rather than pass in the aws security group, we just look it up.
  8. data "aws_security_group" "typical-host" {
  9. name = "typical-host"
  10. vpc_id = var.vpc_id
  11. }
  12. # Use the default EBS key
  13. data "aws_kms_key" "ebs-key" {
  14. key_id = "alias/ebs_root_encrypt_decrypt"
  15. }
  16. resource "aws_network_interface" "instance" {
  17. count = var.alsi_count
  18. subnet_id = var.subnets[ count.index % length(var.subnets) ] # evenly distributed across subnets
  19. security_groups = [ data.aws_security_group.typical-host.id, aws_security_group.alsi_security_group.id ]
  20. description = "${local.instance_name}-${count.index}"
  21. tags = merge( var.standard_tags,
  22. var.tags,
  23. {
  24. Name = "${local.instance_name}-${count.index}",
  25. instance_num = count.index,
  26. instance_count = var.alsi_count
  27. }
  28. )
  29. }
  30. resource "aws_instance" "instance" {
  31. count = var.alsi_count
  32. #availability_zone = var.azs[count.index % 2] # automatically determined by the network interface
  33. tenancy = "default"
  34. ebs_optimized = true
  35. disable_api_termination = var.instance_termination_protection
  36. instance_initiated_shutdown_behavior = "stop"
  37. instance_type = var.instance_type
  38. key_name = "msoc-build"
  39. monitoring = false
  40. iam_instance_profile = "msoc-default-instance-profile"
  41. ami = local.ami_map[local.ami_selection]
  42. # We need to ignore ebs_block_device changes, because if the AMI changes, so does the snapshot_id.
  43. # If they add a feature to block more specific changes (eg `ebs_block_devices[*].snapshot_id`), then
  44. # that could be removed.
  45. lifecycle { ignore_changes = [ ami, key_name, user_data, ebs_block_device ] }
  46. # These device definitions are optional, but added for clarity.
  47. root_block_device {
  48. volume_type = "gp2"
  49. #volume_size = Override via var?
  50. delete_on_termination = true
  51. encrypted = true
  52. kms_key_id = data.aws_kms_key.ebs-key.arn
  53. }
  54. network_interface {
  55. device_index = 0
  56. network_interface_id = aws_network_interface.instance[count.index].id
  57. }
  58. user_data = data.template_cloudinit_config.cloud-init[count.index].rendered
  59. tags = merge( var.standard_tags,
  60. var.tags,
  61. {
  62. Name = "${local.instance_name}-${count.index}",
  63. instance_num = count.index,
  64. instance_count = var.alsi_count
  65. }
  66. )
  67. volume_tags = merge( var.standard_tags,
  68. var.tags,
  69. {
  70. Name = "${local.instance_name}-${count.index}",
  71. instance_num = count.index,
  72. instance_count = var.alsi_count
  73. }
  74. )
  75. }
  76. module "private_dns_record" {
  77. count = var.alsi_count
  78. source = "../../../submodules/dns/private_A_record"
  79. name = "${local.instance_name}-${count.index}"
  80. ip_addresses = [ aws_instance.instance[count.index].private_ip ]
  81. dns_info = var.dns_info
  82. reverse_enabled = var.reverse_enabled
  83. providers = {
  84. aws.c2 = aws.c2
  85. }
  86. }
  87. data "template_file" "cloud-init" {
  88. count = var.alsi_count
  89. # Should these be in a common directory? I suspect they'd be reusable
  90. template = file("${path.module}/cloud-init/cloud-init.tpl")
  91. vars = {
  92. hostname = "${local.instance_name}-${count.index}"
  93. fqdn = "${local.instance_name}-${count.index}.${var.dns_info["private"]["zone"]}"
  94. splunk_prefix = var.prefix
  95. environment = var.environment
  96. salt_master = var.salt_master
  97. proxy = var.proxy
  98. aws_partition = var.aws_partition
  99. aws_partition_alias = var.aws_partition_alias
  100. aws_region = var.aws_region
  101. }
  102. }
  103. # Render a multi-part cloud-init config making use of the part
  104. # above, and other source files
  105. data "template_cloudinit_config" "cloud-init" {
  106. count = var.alsi_count
  107. gzip = true
  108. base64_encode = true
  109. # Main cloud-config configuration file.
  110. part {
  111. filename = "init.cfg"
  112. content_type = "text/cloud-config"
  113. content = data.template_file.cloud-init[count.index].rendered
  114. }
  115. }
  116. ## ALSI
  117. #
  118. # Summary:
  119. # Ingress:
  120. # 443 - from ALB
  121. # 443 - From vpc-access
  122. #
  123. # Egress:
  124. # 8089 - To Splunk
  125. # 9997 - To Splunk
  126. resource "aws_security_group" "alsi_security_group" {
  127. name_prefix = "${ var.prefix }_alsi_security_group" # name prefix and livecycle allow for smooth updates
  128. lifecycle { create_before_destroy = true } # handle updates gracefully
  129. description = "Security Group for Aggregated Log Source Ingestion"
  130. vpc_id = var.vpc_id
  131. tags = merge(var.standard_tags, var.tags)
  132. }
  133. # Ingress
  134. resource "aws_security_group_rule" "alsi-alb-web-in" {
  135. description = "Web access"
  136. type = "ingress"
  137. from_port = 443
  138. to_port = 443
  139. protocol = "tcp"
  140. source_security_group_id = aws_security_group.alsi-alb-sg.id
  141. security_group_id = aws_security_group.alsi_security_group.id
  142. }
  143. resource "aws_security_group_rule" "alsi-access-web-in" {
  144. description = "Web access"
  145. type = "ingress"
  146. from_port = 443
  147. to_port = 443
  148. protocol = "tcp"
  149. cidr_blocks = var.cidr_map["vpc-access"]
  150. security_group_id = aws_security_group.alsi_security_group.id
  151. }
  152. # Egress
  153. resource "aws_security_group_rule" "splunk-mgmt" {
  154. description = "Management Access"
  155. type = "egress"
  156. from_port = 8089
  157. to_port = 8089
  158. protocol = "tcp"
  159. cidr_blocks = [ var.vpc_cidr ]
  160. security_group_id = aws_security_group.alsi_security_group.id
  161. }
  162. resource "aws_security_group_rule" "splunk-data" {
  163. description = "Management Access"
  164. type = "egress"
  165. from_port = 9997
  166. to_port = 9998
  167. protocol = "tcp"
  168. cidr_blocks = [ var.vpc_cidr ]
  169. security_group_id = aws_security_group.alsi_security_group.id
  170. }