X EC2 (or 1 ASG X:X); X external volumes; X network interfaces; KMS key for volumes; Key pair
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
7.9KB

  1. ####
  2. # Launch Template
  3. ####
  4. resource "aws_launch_template" "this" {
  5. count = var.use_autoscaling_group ? 1 : 0
  6. name = format("%s%s", var.prefix, var.launch_template_name)
  7. image_id = local.ami
  8. instance_type = var.instance_type
  9. key_name = local.key_pair_name
  10. user_data = var.user_data
  11. disable_api_termination = var.disable_api_termination
  12. ebs_optimized = var.ebs_optimized
  13. tags = merge(
  14. {
  15. "Name" = format("%s%s", var.prefix, var.launch_template_name)
  16. },
  17. var.tags,
  18. var.launch_template_tags,
  19. local.tags,
  20. )
  21. dynamic "cpu_options" {
  22. for_each = (var.cpu_threads_per_core != null || var.cpu_core_count != null) ? [1] : []
  23. content {
  24. core_count = var.cpu_core_count
  25. threads_per_core = var.cpu_threads_per_core
  26. }
  27. }
  28. dynamic "credit_specification" {
  29. for_each = local.is_t_instance_type && var.cpu_credits != null ? [1] : []
  30. content {
  31. cpu_credits = var.cpu_credits
  32. }
  33. }
  34. dynamic "block_device_mappings" {
  35. for_each = local.should_update_root_device ? [1] : []
  36. content {
  37. device_name = var.root_block_device_volume_device
  38. ebs {
  39. delete_on_termination = true
  40. encrypted = var.root_block_device_encrypted
  41. iops = var.root_block_device_iops
  42. volume_size = var.root_block_device_volume_size
  43. volume_type = var.root_block_device_volume_type
  44. kms_key_id = local.volume_kms_key_arn
  45. }
  46. }
  47. }
  48. dynamic "block_device_mappings" {
  49. for_each = data.null_data_source.ebs_block_device
  50. content {
  51. device_name = block_device_mappings.value.outputs.device_name
  52. ebs {
  53. delete_on_termination = true
  54. encrypted = true
  55. volume_size = lookup(block_device_mappings.value.outputs, "size", null)
  56. volume_type = lookup(block_device_mappings.value.outputs, "type", null)
  57. kms_key_id = local.volume_kms_key_arn
  58. }
  59. }
  60. }
  61. dynamic "block_device_mappings" {
  62. for_each = var.ephemeral_block_devices
  63. content {
  64. device_name = block_device_mappings.value.device_name
  65. virtual_name = lookup(block_device_mappings.value, "virtual_name", null)
  66. no_device = lookup(block_device_mappings.value, "no_device", null)
  67. }
  68. }
  69. dynamic "iam_instance_profile" {
  70. for_each = local.iam_instance_profile_name != null ? [1] : []
  71. content {
  72. name = local.iam_instance_profile_name
  73. }
  74. }
  75. dynamic "monitoring" {
  76. for_each = var.monitoring == true ? [1] : []
  77. content {
  78. enabled = true
  79. }
  80. }
  81. network_interfaces {
  82. description = format("%s%s", var.prefix, local.use_incremental_names ? "${format("%s-%0${var.num_suffix_digits}d", var.primary_network_interface_name, count.index + local.num_suffix_starting_index)} root network interface" : "${var.primary_network_interface_name} root network interface")
  83. security_groups = local.security_group_ids
  84. associate_public_ip_address = var.associate_public_ip_address
  85. ipv6_address_count = var.launch_template_ipv6_address_count
  86. ipv4_address_count = var.ipv4_address_count
  87. delete_on_termination = true
  88. }
  89. dynamic "placement" {
  90. for_each = var.placement_group != null ? [1] : []
  91. content {
  92. availability_zone = local.availability_zones[0]
  93. group_name = var.placement_group
  94. tenancy = var.tenancy
  95. host_id = var.host_id
  96. }
  97. }
  98. tag_specifications {
  99. resource_type = "instance"
  100. tags = merge(
  101. {
  102. "Name" = format("%s%s", var.prefix, var.name)
  103. },
  104. var.tags,
  105. var.instance_tags,
  106. )
  107. }
  108. tag_specifications {
  109. resource_type = "volume"
  110. tags = merge(
  111. {
  112. "Name" = format("%s%s", var.prefix, local.use_incremental_names ? format("%s-%0${var.num_suffix_digits}d", var.extra_volume_name, local.num_suffix_starting_index) : var.extra_volume_name)
  113. },
  114. var.tags,
  115. var.extra_volume_tags,
  116. local.tags,
  117. )
  118. }
  119. // This hack is necessary as for Terraform 0.13.2+ and AWS Provider 3.7.0+
  120. // Because aws_iam_service_linked_role resource returns a result before it's actually available, making ASG creation fail.
  121. provisioner "local-exec" {
  122. command = "sleep 10"
  123. }
  124. }
  125. ####
  126. # AutoScaling Group
  127. ####
  128. resource "aws_iam_service_linked_role" "asg" {
  129. count = var.use_autoscaling_group ? 1 : 0
  130. aws_service_name = "autoscaling.amazonaws.com"
  131. custom_suffix = format("%s%s", var.prefix, var.autoscaling_group_name)
  132. }
  133. ####
  134. # AutoScaling Group
  135. ####
  136. resource "aws_autoscaling_group" "this" {
  137. count = var.use_autoscaling_group ? 1 : 0
  138. name = format("%s%s", var.prefix, var.autoscaling_group_name)
  139. desired_capacity = var.autoscaling_group_desired_capacity
  140. max_size = var.autoscaling_group_max_size
  141. min_size = var.autoscaling_group_min_size
  142. health_check_grace_period = var.autoscaling_group_health_check_grace_period == -1 ? null : var.autoscaling_group_health_check_grace_period
  143. health_check_type = var.autoscaling_group_health_check_type
  144. default_cooldown = var.autoscaling_group_default_cooldown == -1 ? null : var.autoscaling_group_default_cooldown
  145. force_delete = false
  146. wait_for_capacity_timeout = var.autoscaling_group_wait_for_capacity_timeout
  147. min_elb_capacity = var.autoscaling_group_min_elb_capacity
  148. wait_for_elb_capacity = var.autoscaling_group_wait_for_elb_capacity
  149. vpc_zone_identifier = local.subnet_ids
  150. launch_template {
  151. id = aws_launch_template.this.*.id[0]
  152. version = aws_launch_template.this.*.latest_version[0]
  153. }
  154. termination_policies = var.autoscaling_group_termination_policies
  155. suspended_processes = var.autoscaling_group_suspended_processes
  156. metrics_granularity = var.autoscaling_group_metrics_granularity
  157. enabled_metrics = var.autoscaling_group_enabled_metrics
  158. max_instance_lifetime = var.autoscaling_group_max_instance_lifetime
  159. placement_group = var.placement_group
  160. service_linked_role_arn = aws_iam_service_linked_role.asg.*.arn[0]
  161. dynamic "tag" {
  162. for_each = merge(var.tags, var.instance_tags, local.tags)
  163. content {
  164. key = tag.key
  165. value = tag.value
  166. propagate_at_launch = true
  167. }
  168. }
  169. dynamic "tag" {
  170. for_each = var.autoscaling_group_tags
  171. content {
  172. key = tag.key
  173. value = tag.value
  174. propagate_at_launch = false
  175. }
  176. }
  177. timeouts {
  178. delete = "15m"
  179. }
  180. lifecycle {
  181. ignore_changes = [target_group_arns]
  182. }
  183. }
  184. resource "aws_autoscaling_attachment" "this" {
  185. count = var.use_autoscaling_group ? length(var.autoscaling_group_target_group_arns) : 0
  186. autoscaling_group_name = aws_autoscaling_group.this.*.id[0]
  187. alb_target_group_arn = element(var.autoscaling_group_target_group_arns, count.index)
  188. }
  189. resource "aws_autoscaling_schedule" "this" {
  190. count = var.use_autoscaling_group ? var.autoscaling_schedule_count : 0
  191. scheduled_action_name = local.use_incremental_names ? format("%s-%0${var.num_suffix_digits}d", var.autoscaling_schedule_name, count.index + local.num_suffix_starting_index) : var.autoscaling_schedule_name
  192. min_size = element(var.autoscaling_schedule_min_sizes, count.index)
  193. max_size = element(var.autoscaling_schedule_max_sizes, count.index)
  194. desired_capacity = element(var.autoscaling_schedule_desired_capacities, count.index)
  195. recurrence = element(var.autoscaling_schedule_recurrences, count.index)
  196. start_time = element(var.autoscaling_schedule_start_times, count.index) != null ? element(var.autoscaling_schedule_start_times, count.index) : timeadd(timestamp(), "1m")
  197. end_time = element(var.autoscaling_schedule_end_times, count.index)
  198. autoscaling_group_name = aws_autoscaling_group.this.*.name[0]
  199. lifecycle {
  200. ignore_changes = [
  201. start_time,
  202. ]
  203. }
  204. }