main.tf 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. resource "aws_placement_group" "cluster-placement" {
  8. name = "splunk-indexer-cluster-placement-${var.asg_number}"
  9. # Cluster placement is an interesting question for us.
  10. # Since we're multisite, we're going to make each site use a 'cluster' strategy to keep indexers
  11. # close together.
  12. #
  13. # 'spread' would be more appropriate if we were worried about individual site
  14. # failures.
  15. #
  16. # And it doesn't really matter atm since all of ours are count=1
  17. strategy = "cluster"
  18. }
  19. #better solutions are to upgrade to TF .12 or maybe this...
  20. #https://github.com/mavin/terraform-aws-tags-to-asg-tags/blob/master/vars.tf
  21. #https://github.com/cloudposse/terraform-aws-ec2-autoscale-group/blob/0.11/master/main.tf
  22. #TF verison 11 does not support conditional operations with the values as lists.
  23. #the /dev/xvdf device is not needed in Prod, just Test.
  24. resource "aws_launch_template" "splunk_indexer" {
  25. name = var.launch_conf_name
  26. instance_type = var.idx_instance_type
  27. image_id = local.ami_map[local.ami_selection]
  28. user_data = var.user_data
  29. ebs_optimized = true
  30. tags = var.tags
  31. network_interfaces {
  32. associate_public_ip_address = false
  33. delete_on_termination = true
  34. security_groups = var.indexer_security_group_ids
  35. }
  36. key_name = var.key_name
  37. iam_instance_profile {
  38. name = var.iam_instance_profile
  39. }
  40. # Unlike for instances, you _must_ specify the volume size for a launch template
  41. block_device_mappings {
  42. device_name = "/dev/sda1"
  43. ebs {
  44. volume_type = "gp2"
  45. volume_size = var.volume_sizes["/"]
  46. delete_on_termination = true
  47. encrypted = true
  48. kms_key_id = data.aws_kms_key.ebs-key.arn
  49. }
  50. }
  51. block_device_mappings {
  52. device_name = "/dev/xvdf"
  53. ebs {
  54. volume_type = "gp2"
  55. volume_size = var.volume_sizes["/opt/splunk"]
  56. delete_on_termination = true
  57. encrypted = true
  58. kms_key_id = data.aws_kms_key.ebs-key.arn
  59. }
  60. }
  61. block_device_mappings {
  62. # swap
  63. device_name = "/dev/xvdm"
  64. ebs {
  65. volume_size = var.volume_sizes["swap"]
  66. delete_on_termination = true
  67. encrypted = true
  68. kms_key_id = data.aws_kms_key.ebs-key.arn
  69. # Snapshot IDs need to be grabbed from the ami, or it will replace every time. It's ugly.
  70. # This may prompt replacement when the AMI is updated.
  71. # See:
  72. # https://github.com/hashicorp/terraform/issues/19958
  73. # https://github.com/terraform-providers/terraform-provider-aws/issues/13118
  74. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdm"].ebs.snapshot_id
  75. }
  76. }
  77. block_device_mappings {
  78. # /home
  79. device_name = "/dev/xvdn"
  80. ebs {
  81. volume_size = var.volume_sizes["/home"]
  82. delete_on_termination = true
  83. encrypted = true
  84. kms_key_id = data.aws_kms_key.ebs-key.arn
  85. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdn"].ebs.snapshot_id
  86. }
  87. }
  88. block_device_mappings {
  89. # /var
  90. device_name = "/dev/xvdo"
  91. ebs {
  92. volume_size = var.volume_sizes["/var"]
  93. delete_on_termination = true
  94. encrypted = true
  95. kms_key_id = data.aws_kms_key.ebs-key.arn
  96. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdo"].ebs.snapshot_id
  97. }
  98. }
  99. block_device_mappings {
  100. # /var/tmp
  101. device_name = "/dev/xvdp"
  102. ebs {
  103. volume_size = var.volume_sizes["/var/tmp"]
  104. delete_on_termination = true
  105. encrypted = true
  106. kms_key_id = data.aws_kms_key.ebs-key.arn
  107. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdp"].ebs.snapshot_id
  108. }
  109. }
  110. block_device_mappings {
  111. # /var/log
  112. device_name = "/dev/xvdq"
  113. ebs {
  114. volume_size = var.volume_sizes["/var/log"]
  115. delete_on_termination = true
  116. encrypted = true
  117. kms_key_id = data.aws_kms_key.ebs-key.arn
  118. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdq"].ebs.snapshot_id
  119. }
  120. }
  121. block_device_mappings {
  122. # /var/log/audit
  123. device_name = "/dev/xvdr"
  124. ebs {
  125. volume_size = var.volume_sizes["/var/log/audit"]
  126. delete_on_termination = true
  127. encrypted = true
  128. kms_key_id = data.aws_kms_key.ebs-key.arn
  129. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvdr"].ebs.snapshot_id
  130. }
  131. }
  132. block_device_mappings {
  133. # /tmp
  134. device_name = "/dev/xvds"
  135. ebs {
  136. volume_size = var.volume_sizes["/tmp"]
  137. delete_on_termination = true
  138. encrypted = true
  139. kms_key_id = data.aws_kms_key.ebs-key.arn
  140. #snapshot_id = local.block_device_mappings[local.ami_selection]["/dev/xvds"].ebs.snapshot_id
  141. }
  142. }
  143. tag_specifications {
  144. resource_type = "instance"
  145. tags = merge(var.tags, { "Name": "${var.name}-splunk-idx" }) # This may have no effect?
  146. }
  147. tag_specifications {
  148. resource_type = "volume"
  149. tags = merge(var.tags, { "Name": "${var.name}-splunk-idx" }) # This may have no effect
  150. }
  151. lifecycle {
  152. create_before_destroy = true
  153. }
  154. }
  155. resource "aws_autoscaling_group" "splunk_indexer_asg" {
  156. name = var.asg_name
  157. launch_template {
  158. id = aws_launch_template.splunk_indexer.id
  159. version = "$Latest"
  160. }
  161. placement_group = aws_placement_group.cluster-placement.id
  162. vpc_zone_identifier = var.vpc_zone_identifier
  163. min_size = var.min_size
  164. max_size = var.max_size
  165. tag {
  166. key = "Name"
  167. value = "${ var.name }-splunk-indexer-${ var.asg_number }"
  168. propagate_at_launch = true
  169. }
  170. # Must ignore changes to attachments, or tf will flip flop
  171. lifecycle {
  172. ignore_changes = [ load_balancers, target_group_arns ]
  173. }
  174. # how long to wait for a healthy instance. Default is 10m, which sucks when troubleshooting, but larger instances need it
  175. #wait_for_capacity_timeout = "1m"
  176. }