main.tf 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. locals {
  2. ami_selection = "minion" # master, minion, ...
  3. }
  4. data "aws_kms_key" "ebs-key" {
  5. key_id = "alias/ebs_root_encrypt_decrypt"
  6. }
  7. # Placement groups are a good idea if we get bigger. This code works, but disabling for now because
  8. # t3* instance types don't support them.
  9. #resource "aws_placement_group" "cluster-placement" {
  10. # name = "splunk-indexer-cluster-placement-${var.asg_number}"
  11. # # Cluster placement is an interesting question for us.
  12. # # Since we're multisite, we're going to make each site use a 'cluster' strategy to keep indexers
  13. # # close together.
  14. # #
  15. # # 'spread' would be more appropriate if we were worried about individual site
  16. # # failures.
  17. # #
  18. # # And it doesn't really matter atm since all of ours are count=1
  19. # strategy = "cluster"
  20. #}
  21. #better solutions are to upgrade to TF .12 or maybe this...
  22. #https://github.com/mavin/terraform-aws-tags-to-asg-tags/blob/master/vars.tf
  23. #https://github.com/cloudposse/terraform-aws-ec2-autoscale-group/blob/0.11/master/main.tf
  24. #TF verison 11 does not support conditional operations with the values as lists.
  25. #the /dev/xvdf device is not needed in Prod, just Test.
  26. resource "aws_launch_template" "splunk_indexer" {
  27. name = var.launch_conf_name
  28. instance_type = var.idx_instance_type
  29. image_id = local.ami_map[local.ami_selection]
  30. user_data = var.user_data
  31. ebs_optimized = true
  32. tags = var.tags
  33. metadata_options {
  34. http_endpoint = "enabled"
  35. http_tokens = "optional" # tfsec:ignore:aws-autoscaling-enforce-http-token-imds Smartstore needs to be configured to use imdsv2, MSOCI-2150
  36. }
  37. network_interfaces {
  38. associate_public_ip_address = false
  39. delete_on_termination = true
  40. security_groups = var.indexer_security_group_ids
  41. }
  42. key_name = var.key_name
  43. iam_instance_profile {
  44. name = var.iam_instance_profile
  45. }
  46. # Unlike for instances, you _must_ specify the volume size for a launch template
  47. block_device_mappings {
  48. device_name = "/dev/sda1"
  49. ebs {
  50. volume_type = "gp2"
  51. volume_size = var.volume_sizes["/"]
  52. delete_on_termination = true
  53. encrypted = true
  54. kms_key_id = data.aws_kms_key.ebs-key.arn
  55. }
  56. }
  57. block_device_mappings {
  58. device_name = "/dev/xvdf"
  59. ebs {
  60. volume_type = "gp2"
  61. volume_size = var.volume_sizes["/opt/splunk"]
  62. delete_on_termination = true
  63. encrypted = true
  64. kms_key_id = data.aws_kms_key.ebs-key.arn
  65. }
  66. }
  67. block_device_mappings {
  68. # swap
  69. device_name = "/dev/xvdm"
  70. ebs {
  71. volume_size = var.volume_sizes["swap"]
  72. delete_on_termination = true
  73. encrypted = true
  74. kms_key_id = data.aws_kms_key.ebs-key.arn
  75. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  76. # This may prompt replacement when the AMI is updated.
  77. # See:
  78. # https://github.com/hashicorp/terraform/issues/19958
  79. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  80. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  81. }
  82. }
  83. block_device_mappings {
  84. # /home
  85. device_name = "/dev/xvdn"
  86. ebs {
  87. volume_size = var.volume_sizes["/home"]
  88. delete_on_termination = true
  89. encrypted = true
  90. kms_key_id = data.aws_kms_key.ebs-key.arn
  91. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  92. }
  93. }
  94. block_device_mappings {
  95. # /var
  96. device_name = "/dev/xvdo"
  97. ebs {
  98. volume_size = var.volume_sizes["/var"]
  99. delete_on_termination = true
  100. encrypted = true
  101. kms_key_id = data.aws_kms_key.ebs-key.arn
  102. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  103. }
  104. }
  105. block_device_mappings {
  106. # /var/tmp
  107. device_name = "/dev/xvdp"
  108. ebs {
  109. volume_size = var.volume_sizes["/var/tmp"]
  110. delete_on_termination = true
  111. encrypted = true
  112. kms_key_id = data.aws_kms_key.ebs-key.arn
  113. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  114. }
  115. }
  116. block_device_mappings {
  117. # /var/log
  118. device_name = "/dev/xvdq"
  119. ebs {
  120. volume_size = var.volume_sizes["/var/log"]
  121. delete_on_termination = true
  122. encrypted = true
  123. kms_key_id = data.aws_kms_key.ebs-key.arn
  124. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  125. }
  126. }
  127. block_device_mappings {
  128. # /var/log/audit
  129. device_name = "/dev/xvdr"
  130. ebs {
  131. volume_size = var.volume_sizes["/var/log/audit"]
  132. delete_on_termination = true
  133. encrypted = true
  134. kms_key_id = data.aws_kms_key.ebs-key.arn
  135. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  136. }
  137. }
  138. block_device_mappings {
  139. # /tmp
  140. device_name = "/dev/xvds"
  141. ebs {
  142. volume_size = var.volume_sizes["/tmp"]
  143. delete_on_termination = true
  144. encrypted = true
  145. kms_key_id = data.aws_kms_key.ebs-key.arn
  146. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  147. }
  148. }
  149. tag_specifications {
  150. resource_type = "instance"
  151. tags = merge(var.tags, { "Name" : "${var.name}-splunk-idx" }) # This may have no effect?
  152. }
  153. tag_specifications {
  154. resource_type = "volume"
  155. tags = merge(var.tags, { "Name" : "${var.name}-splunk-idx" }) # This may have no effect
  156. }
  157. lifecycle {
  158. create_before_destroy = true
  159. }
  160. }
  161. resource "aws_autoscaling_group" "splunk_indexer_asg" {
  162. name = var.asg_name
  163. launch_template {
  164. id = aws_launch_template.splunk_indexer.id
  165. version = "$Latest"
  166. }
  167. # Placement groups are a good idea if we get bigger. This code works, but disabling for now because
  168. # t3* instance types don't support them.
  169. #placement_group = aws_placement_group.cluster-placement.id
  170. vpc_zone_identifier = var.vpc_zone_identifier
  171. min_size = var.min_size
  172. max_size = var.max_size
  173. tag {
  174. key = "Name"
  175. value = "${var.name}-splunk-indexer-${var.asg_number}"
  176. propagate_at_launch = true
  177. }
  178. # Must ignore changes to attachments, or tf will flip flop
  179. lifecycle {
  180. ignore_changes = [load_balancers, target_group_arns]
  181. }
  182. # how long to wait for a healthy instance. Default is 10m, which sucks when troubleshooting, but larger instances need it
  183. #wait_for_capacity_timeout = "1m"
  184. # Default metrics for ASG
  185. enabled_metrics = [
  186. "GroupAndWarmPoolDesiredCapacity",
  187. "GroupAndWarmPoolTotalCapacity",
  188. "GroupDesiredCapacity",
  189. "GroupInServiceCapacity",
  190. "GroupInServiceInstances",
  191. "GroupMaxSize",
  192. "GroupMinSize",
  193. "GroupPendingCapacity",
  194. "GroupPendingInstances",
  195. "GroupStandbyCapacity",
  196. "GroupStandbyInstances",
  197. "GroupTerminatingCapacity",
  198. "GroupTerminatingInstances",
  199. "GroupTotalCapacity",
  200. "GroupTotalInstances",
  201. "WarmPoolDesiredCapacity",
  202. "WarmPoolMinSize",
  203. "WarmPoolPendingCapacity",
  204. "WarmPoolTerminatingCapacity",
  205. "WarmPoolTotalCapacity",
  206. "WarmPoolWarmedCapacity",
  207. ]
  208. suspended_processes = var.suspended_processes
  209. }