main.tf 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. network_interfaces {
  34. associate_public_ip_address = false
  35. delete_on_termination = true
  36. security_groups = var.indexer_security_group_ids
  37. }
  38. key_name = var.key_name
  39. iam_instance_profile {
  40. name = var.iam_instance_profile
  41. }
  42. # Unlike for instances, you _must_ specify the volume size for a launch template
  43. block_device_mappings {
  44. device_name = "/dev/sda1"
  45. ebs {
  46. volume_type = "gp2"
  47. volume_size = var.volume_sizes["/"]
  48. delete_on_termination = true
  49. encrypted = true
  50. kms_key_id = data.aws_kms_key.ebs-key.arn
  51. }
  52. }
  53. block_device_mappings {
  54. device_name = "/dev/xvdf"
  55. ebs {
  56. volume_type = "gp2"
  57. volume_size = var.volume_sizes["/opt/splunk"]
  58. delete_on_termination = true
  59. encrypted = true
  60. kms_key_id = data.aws_kms_key.ebs-key.arn
  61. }
  62. }
  63. block_device_mappings {
  64. # swap
  65. device_name = "/dev/xvdm"
  66. ebs {
  67. volume_size = var.volume_sizes["swap"]
  68. delete_on_termination = true
  69. encrypted = true
  70. kms_key_id = data.aws_kms_key.ebs-key.arn
  71. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  72. # This may prompt replacement when the AMI is updated.
  73. # See:
  74. # https://github.com/hashicorp/terraform/issues/19958
  75. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  76. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  77. }
  78. }
  79. block_device_mappings {
  80. # /home
  81. device_name = "/dev/xvdn"
  82. ebs {
  83. volume_size = var.volume_sizes["/home"]
  84. delete_on_termination = true
  85. encrypted = true
  86. kms_key_id = data.aws_kms_key.ebs-key.arn
  87. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  88. }
  89. }
  90. block_device_mappings {
  91. # /var
  92. device_name = "/dev/xvdo"
  93. ebs {
  94. volume_size = var.volume_sizes["/var"]
  95. delete_on_termination = true
  96. encrypted = true
  97. kms_key_id = data.aws_kms_key.ebs-key.arn
  98. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  99. }
  100. }
  101. block_device_mappings {
  102. # /var/tmp
  103. device_name = "/dev/xvdp"
  104. ebs {
  105. volume_size = var.volume_sizes["/var/tmp"]
  106. delete_on_termination = true
  107. encrypted = true
  108. kms_key_id = data.aws_kms_key.ebs-key.arn
  109. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  110. }
  111. }
  112. block_device_mappings {
  113. # /var/log
  114. device_name = "/dev/xvdq"
  115. ebs {
  116. volume_size = var.volume_sizes["/var/log"]
  117. delete_on_termination = true
  118. encrypted = true
  119. kms_key_id = data.aws_kms_key.ebs-key.arn
  120. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  121. }
  122. }
  123. block_device_mappings {
  124. # /var/log/audit
  125. device_name = "/dev/xvdr"
  126. ebs {
  127. volume_size = var.volume_sizes["/var/log/audit"]
  128. delete_on_termination = true
  129. encrypted = true
  130. kms_key_id = data.aws_kms_key.ebs-key.arn
  131. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  132. }
  133. }
  134. block_device_mappings {
  135. # /tmp
  136. device_name = "/dev/xvds"
  137. ebs {
  138. volume_size = var.volume_sizes["/tmp"]
  139. delete_on_termination = true
  140. encrypted = true
  141. kms_key_id = data.aws_kms_key.ebs-key.arn
  142. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  143. }
  144. }
  145. tag_specifications {
  146. resource_type = "instance"
  147. tags = merge(var.tags, { "Name": "${var.name}-splunk-idx" }) # This may have no effect?
  148. }
  149. tag_specifications {
  150. resource_type = "volume"
  151. tags = merge(var.tags, { "Name": "${var.name}-splunk-idx" }) # This may have no effect
  152. }
  153. lifecycle {
  154. create_before_destroy = true
  155. }
  156. }
  157. resource "aws_autoscaling_group" "splunk_indexer_asg" {
  158. name = var.asg_name
  159. launch_template {
  160. id = aws_launch_template.splunk_indexer.id
  161. version = "$Latest"
  162. }
  163. # Placement groups are a good idea if we get bigger. This code works, but disabling for now because
  164. # t3* instance types don't support them.
  165. #placement_group = aws_placement_group.cluster-placement.id
  166. vpc_zone_identifier = var.vpc_zone_identifier
  167. min_size = var.min_size
  168. max_size = var.max_size
  169. tag {
  170. key = "Name"
  171. value = "${ var.name }-splunk-indexer-${ var.asg_number }"
  172. propagate_at_launch = true
  173. }
  174. # Must ignore changes to attachments, or tf will flip flop
  175. lifecycle {
  176. ignore_changes = [ load_balancers, target_group_arns ]
  177. }
  178. # how long to wait for a healthy instance. Default is 10m, which sucks when troubleshooting, but larger instances need it
  179. #wait_for_capacity_timeout = "1m"
  180. }