From 2d7c97d3cc947dfeec5c1af79c0ce91a6c49cd22 Mon Sep 17 00:00:00 2001 From: Michael Wangsa Date: Fri, 9 Apr 2021 18:11:59 +0900 Subject: [PATCH] Add option to use the generated LT version --- README.md | 12 +++++++----- locals.tf | 1 + main.tf | 36 ++++++++++++++++++------------------ variables.tf | 12 ++++++++++++ 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 20ce3b3..f622597 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,10 @@ No requirements. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| +| asg\_instance\_refresh\_additional\_triggers | Set of additional property names that will trigger an Instance Refresh. A refresh will always be triggered by a change in any of launch\_configuration, launch\_template, or mixed\_instances\_policy. | `list(string)` | `null` | no | +| asg\_instance\_refresh\_healthy\_percentage | The amount of capacity in the Auto Scaling group that must remain healthy during an instance refresh to allow the operation to continue, as a percentage of the desired capacity of the Auto Scaling group. | `number` | `null` | no | +| asg\_instance\_refresh\_strategy | The strategy to use for instance refresh. The only allowed value is `Rolling`. | `string` | `null` | no | +| asg\_instance\_refresh\_warmup | The number of seconds until a newly launched instance is configured and ready to use. | `number` | `null` | no | | asg\_name | Creates a unique name for autoscaling group beginning with the specified prefix | `string` | `""` | no | | associate\_public\_ip\_address | Associate a public ip address with an instance in a VPC | `bool` | `false` | no | | block\_device\_mappings | Mappings of block devices, see https://www.terraform.io/docs/providers/aws/r/launch_template.html#block-devices | `list(any)` |
[
{}
]
| no | @@ -212,7 +216,7 @@ No requirements. | health\_check\_grace\_period | Time (in seconds) after instance comes into service before checking health | `number` | `300` | no | | health\_check\_type | Controls how health checking is done. Values are - EC2 and ELB | `string` | n/a | yes | | iam\_instance\_profile | The IAM instance profile to associate with launched instances | `string` | `""` | no | -| ignore\_desired\_capacity\_changes | Ignores any changes to `desired_count` parameter after apply. Note updating this value will destroy the existing service and recreate it. | `bool` | `false` | no | +| ignore\_desired\_capacity\_changes | Ignores any changes to `desired_capacity` parameter after apply. Note updating this value will destroy the existing service and recreate it. | `bool` | `false` | no | | image\_id | The EC2 image ID to launch | `string` | `""` | no | | initial\_lifecycle\_hook\_default\_result | Defines the action the Auto Scaling group should take when the lifecycle hook timeout elapses or if an unexpected failure occurs. The value for this parameter can be either CONTINUE or ABANDON | `string` | `"ABANDON"` | no | | initial\_lifecycle\_hook\_heartbeat\_timeout | Defines the amount of time, in seconds, that can elapse before the lifecycle hook times out. When the lifecycle hook times out, Auto Scaling performs the action defined in the DefaultResult parameter | `string` | `"60"` | no | @@ -226,6 +230,7 @@ No requirements. | launch\_template | The name of the launch template to use (if it is created outside of this module) | `string` | `""` | no | | load\_balancers | A list of elastic load balancer names to add to the autoscaling group names | `list(string)` | `[]` | no | | lt\_name | Creates a unique name for launch template beginning with the specified prefix | `string` | `""` | no | +| lt\_version | Launch Template version to use (if it is created outside of this module). | `string` | `null` | no | | max\_instance\_lifetime | The maximum amount of time, in seconds, that an instance can be in service, values must be either equal to 0 or between 604800 and 31536000 seconds | `number` | `null` | no | | max\_size | The maximum size of the auto scale group | `number` | n/a | yes | | metrics\_granularity | The granularity to associate with the metrics to collect. The only valid value is 1Minute | `string` | `"1Minute"` | no | @@ -248,14 +253,11 @@ No requirements. | tags\_as\_map | A map of tags and values in the same format as other resources accept. This will be converted into the non-standard format that the aws\_autoscaling\_group requires. | `map(any)` | `{}` | no | | target\_group\_arns | A list of aws\_alb\_target\_group ARNs, for use with Application Load Balancing | `list(string)` | `[]` | no | | termination\_policies | A list of policies to decide how the instances in the auto scale group should be terminated. The allowed values are OldestInstance, NewestInstance, OldestLaunchConfiguration, ClosestToNextInstanceHour, Default | `list(string)` |
[
"Default"
]
| no | +| use\_created\_lt\_latest\_version | Use version number generated by Launch Template instead of $Latest. | `bool` | `false` | no | | user\_data | The user data to provide when launching the instance | `string` | `" "` | no | | vpc\_zone\_identifier | A list of subnet IDs to launch resources in | `list(string)` | n/a | yes | | wait\_for\_capacity\_timeout | A maximum duration that Terraform should wait for ASG instances to be healthy before timing out. (See also Waiting for Capacity below.) Setting this to '0' causes Terraform to skip all Capacity Waiting behavior. | `string` | `"10m"` | no | | wait\_for\_elb\_capacity | Setting this will cause Terraform to wait for exactly this number of healthy instances in all attached load balancers on both create and update operations. Takes precedence over min\_elb\_capacity behavior. | `number` | `null` | no | -| asg\_instance\_refresh\_strategy | The strategy to use for instance refresh. The only allowed value is `Rolling`. | `string` | `null` | no | -| asg\_instance\_refresh\_warmup | The number of seconds until a newly launched instance is configured and ready to use. | `number` | `null` | no | -| asg\_instance\_refresh\_healthy\_percentage | The amount of capacity in the Auto Scaling group that must remain healthy during an instance refresh to allow the operation to continue, as a percentage of the desired capacity of the Auto Scaling group. | `number` | `null` | no | -| asg\_instance\_refresh\_additional\_triggers | Set of additional property names that will trigger an Instance Refresh. A refresh will always be triggered by a change in any of launch_configuration, launch_template, or mixed_instances_policy. | `list(string)` | `null` | no | ## Outputs diff --git a/locals.tf b/locals.tf index 042352a..89c1dc9 100644 --- a/locals.tf +++ b/locals.tf @@ -1,5 +1,6 @@ locals { tags_asg_format = null_resource.tags_as_list_of_maps.*.triggers + lt_version = var.lt_version != null ? var.lt_version : var.use_created_lt_latest_version ? element(concat(aws_launch_template.this.*.latest_version, ["$Latest"]), 0) : "$Latest" } resource "null_resource" "tags_as_list_of_maps" { diff --git a/main.tf b/main.tf index 99a85f8..a02ec6e 100644 --- a/main.tf +++ b/main.tf @@ -101,7 +101,7 @@ resource "aws_autoscaling_group" "this" { launch_template { launch_template_specification { launch_template_id = var.create_lt ? element(concat(aws_launch_template.this.*.id, [""]), 0) : var.launch_template - version = "$Latest" + version = local.lt_version } dynamic "override" { @@ -151,17 +151,17 @@ resource "aws_autoscaling_group" "this" { for_each = var.asg_instance_refresh_strategy != null ? [1] : [] content { - strategy = var.asg_instance_refresh_strategy - triggers = var.asg_instance_refresh_additional_triggers + strategy = var.asg_instance_refresh_strategy + triggers = var.asg_instance_refresh_additional_triggers - dynamic "preferences" { - for_each = var.asg_instance_refresh_warmup != null || var.asg_instance_refresh_healthy_percentage != null ? [1] : [] + dynamic "preferences" { + for_each = var.asg_instance_refresh_warmup != null || var.asg_instance_refresh_healthy_percentage != null ? [1] : [] - content { - instance_warmup = var.asg_instance_refresh_warmup - min_healthy_percentage = var.asg_instance_refresh_healthy_percentage - } + content { + instance_warmup = var.asg_instance_refresh_warmup + min_healthy_percentage = var.asg_instance_refresh_healthy_percentage } + } } } @@ -214,7 +214,7 @@ resource "aws_autoscaling_group" "this_ignore_desired_capacity_changes" { launch_template { launch_template_specification { launch_template_id = var.create_lt ? element(concat(aws_launch_template.this.*.id, [""]), 0) : var.launch_template - version = "$Latest" + version = local.lt_version } dynamic "override" { @@ -264,17 +264,17 @@ resource "aws_autoscaling_group" "this_ignore_desired_capacity_changes" { for_each = var.asg_instance_refresh_strategy != null ? [1] : [] content { - strategy = var.asg_instance_refresh_strategy - triggers = var.asg_instance_refresh_additional_triggers + strategy = var.asg_instance_refresh_strategy + triggers = var.asg_instance_refresh_additional_triggers - dynamic "preferences" { - for_each = var.asg_instance_refresh_warmup != null || var.asg_instance_refresh_healthy_percentage != null ? [1] : [] + dynamic "preferences" { + for_each = var.asg_instance_refresh_warmup != null || var.asg_instance_refresh_healthy_percentage != null ? [1] : [] - content { - instance_warmup = var.asg_instance_refresh_warmup - min_healthy_percentage = var.asg_instance_refresh_healthy_percentage - } + content { + instance_warmup = var.asg_instance_refresh_warmup + min_healthy_percentage = var.asg_instance_refresh_healthy_percentage } + } } } diff --git a/variables.tf b/variables.tf index 27d77c8..ec3e9ef 100644 --- a/variables.tf +++ b/variables.tf @@ -87,6 +87,12 @@ variable "launch_template" { default = "" } +variable "lt_version" { + description = "Launch Template version to use (if it is created outside of this module)." + type = string + default = null +} + # Launch template variable "image_id" { description = "The EC2 image ID to launch" @@ -363,3 +369,9 @@ variable "asg_instance_refresh_additional_triggers" { type = list(string) default = null } + +variable "use_created_lt_latest_version" { + description = "Use version number generated by Launch Template instead of $Latest." + default = false + type = bool +}