From 37824138e8d98fc8179177bc5c92880be2282d84 Mon Sep 17 00:00:00 2001 From: Yethal <26117918+Yethal@users.noreply.github.com> Date: Wed, 14 Aug 2024 00:17:44 +0200 Subject: [PATCH] feat!: Allow multiple traffic source attachments (#273) * Allow multiple traffic source attachments * feat: Complete remaining breaking changes and add upgrade guide --------- Co-authored-by: Bryant Biggs --- .pre-commit-config.yaml | 4 +- README.md | 8 +-- UPGRADE-8.0.md | 96 +++++++++++++++++++++++++++++++++++ examples/complete/README.md | 2 +- examples/complete/main.tf | 9 ++-- examples/complete/versions.tf | 2 +- main.tf | 18 +++---- variables.tf | 32 ++---------- versions.tf | 2 +- 9 files changed, 122 insertions(+), 51 deletions(-) create mode 100644 UPGRADE-8.0.md diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 395404e..3418005 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/antonbabenko/pre-commit-terraform - rev: v1.88.0 + rev: v1.92.1 hooks: - id: terraform_fmt - id: terraform_docs @@ -24,7 +24,7 @@ repos: - '--args=--only=terraform_unused_required_providers' - id: terraform_validate - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v4.6.0 hooks: - id: check-merge-conflict - id: end-of-file-fixer diff --git a/README.md b/README.md index 8e1f7ef..ca58ce7 100644 --- a/README.md +++ b/README.md @@ -230,7 +230,7 @@ Note: the default behavior of the module is to create an autoscaling group and l | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 1.0 | +| [terraform](#requirement\_terraform) | >= 1.3 | | [aws](#requirement\_aws) | >= 5.55 | ## Providers @@ -274,7 +274,6 @@ No modules. | [create\_launch\_template](#input\_create\_launch\_template) | Determines whether to create launch template or not | `bool` | `true` | no | | [create\_scaling\_policy](#input\_create\_scaling\_policy) | Determines whether to create target scaling policy schedule or not | `bool` | `true` | no | | [create\_schedule](#input\_create\_schedule) | Determines whether to create autoscaling group schedule or not | `bool` | `true` | no | -| [create\_traffic\_source\_attachment](#input\_create\_traffic\_source\_attachment) | Determines whether to create autoscaling group traffic source attachment | `bool` | `false` | no | | [credit\_specification](#input\_credit\_specification) | Customize the credit specification of the instance | `map(string)` | `{}` | no | | [default\_cooldown](#input\_default\_cooldown) | The amount of time, in seconds, after a scaling activity completes before another scaling activity can start | `number` | `null` | no | | [default\_instance\_warmup](#input\_default\_instance\_warmup) | Amount of time, in seconds, until a newly launched instance can contribute to the Amazon CloudWatch metrics. This delay lets an instance finish initializing before Amazon EC2 Auto Scaling aggregates instance metrics, resulting in more reliable usage data. Set this value equal to the amount of time that it takes for resource consumption to become stable after an instance reaches the InService state. | `number` | `null` | no | @@ -322,7 +321,6 @@ No modules. | [launch\_template\_use\_name\_prefix](#input\_launch\_template\_use\_name\_prefix) | Determines whether to use `launch_template_name` as is or create a unique name beginning with the `launch_template_name` as the prefix | `bool` | `true` | no | | [launch\_template\_version](#input\_launch\_template\_version) | Launch template version. Can be version number, `$Latest`, or `$Default` | `string` | `null` | no | | [license\_specifications](#input\_license\_specifications) | A list of license specifications to associate with | `map(string)` | `{}` | no | -| [load\_balancers](#input\_load\_balancers) | A list of elastic load balancer names to add to the autoscaling group names. Only valid for classic load balancers. For ALBs, use `target_group_arns` instead | `list(string)` | `[]` | no | | [maintenance\_options](#input\_maintenance\_options) | The maintenance options for the instance | `any` | `{}` | no | | [max\_instance\_lifetime](#input\_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 86400 and 31536000 seconds | `number` | `null` | no | | [max\_size](#input\_max\_size) | The maximum size of the autoscaling group | `number` | `null` | no | @@ -346,10 +344,8 @@ No modules. | [suspended\_processes](#input\_suspended\_processes) | A list of processes to suspend for the Auto Scaling Group. The allowed values are `Launch`, `Terminate`, `HealthCheck`, `ReplaceUnhealthy`, `AZRebalance`, `AlarmNotification`, `ScheduledActions`, `AddToLoadBalancer`, `InstanceRefresh`. Note that if you suspend either the `Launch` or `Terminate` process types, it can prevent your Auto Scaling Group from functioning properly | `list(string)` | `[]` | no | | [tag\_specifications](#input\_tag\_specifications) | The tags to apply to the resources during launch | `list(any)` | `[]` | no | | [tags](#input\_tags) | A map of tags to assign to resources | `map(string)` | `{}` | no | -| [target\_group\_arns](#input\_target\_group\_arns) | A set of `aws_alb_target_group` ARNs, for use with Application or Network Load Balancing | `list(string)` | `[]` | no | | [termination\_policies](#input\_termination\_policies) | A list of policies to decide how the instances in the Auto Scaling Group should be terminated. The allowed values are `OldestInstance`, `NewestInstance`, `OldestLaunchConfiguration`, `ClosestToNextInstanceHour`, `OldestLaunchTemplate`, `AllocationStrategy`, `Default` | `list(string)` | `[]` | no | -| [traffic\_source\_identifier](#input\_traffic\_source\_identifier) | Identifies the traffic source. For Application Load Balancers, Gateway Load Balancers, Network Load Balancers, and VPC Lattice, this will be the Amazon Resource Name (ARN) for a target group in this account and Region. For Classic Load Balancers, this will be the name of the Classic Load Balancer in this account and Region | `string` | `""` | no | -| [traffic\_source\_type](#input\_traffic\_source\_type) | Provides additional context for the value of identifier. The following lists the valid values: `elb` if `identifier` is the name of a Classic Load Balancer. `elbv2` if `identifier` is the ARN of an Application Load Balancer, Gateway Load Balancer, or Network Load Balancer target group. `vpc-lattice` if `identifier` is the ARN of a VPC Lattice target group | `string` | `"elbv2"` | no | +| [traffic\_source\_attachments](#input\_traffic\_source\_attachments) | Map of traffic source attachment definitions to create | `any` | `{}` | no | | [update\_default\_version](#input\_update\_default\_version) | Whether to update Default Version each update. Conflicts with `default_version` | `bool` | `null` | no | | [use\_mixed\_instances\_policy](#input\_use\_mixed\_instances\_policy) | Determines whether to use a mixed instances policy in the autoscaling group or not | `bool` | `false` | no | | [use\_name\_prefix](#input\_use\_name\_prefix) | Determines whether to use `name` as is or create a unique name beginning with the `name` as the prefix | `bool` | `true` | no | diff --git a/UPGRADE-8.0.md b/UPGRADE-8.0.md new file mode 100644 index 0000000..d4f5345 --- /dev/null +++ b/UPGRADE-8.0.md @@ -0,0 +1,96 @@ +# Upgrade from v7.x to v8.x + +If you have any questions regarding this upgrade process, please consult the [`examples`](https://github.com/terraform-aws-modules/terraform-aws-autoscaling/tree/master/examples/complete) directory: +If you find a bug, please open an issue with supporting configuration to reproduce. + +## List of backwards incompatible changes + +- `target_group_arns` and `load_balancers` are no longer supported on the ASG; instead users should use the `traffic_source_attachments` which cover this functionality and more +- Users can now create multiple `traffic_source_attachments` instead of previously where the module only supported one attachment +- The minimum supported version of Terraform has been raised to `v1.3` + +## Additional changes + +### Added + +- None + +### Modified + +- None + +### Variable and output changes + +1. Removed variables: + + - `load_balancers` + - `target_group_arns` + - `create_traffic_source_attachment` + - `traffic_source_identifier` + - `traffic_source_type` + +2. Renamed variables: + + - None + +3. Added variables: + + - `traffic_source_attachments` + +4. Removed outputs: + + - `launch_configuration_id` + - `launch_configuration_arn` + - `launch_configuration_name` + +5. Renamed outputs: + + - None + +6. Added outputs: + + - None + +## Upgrade Migrations + +### Before 7.x Example + +```hcl +module "asg" { + source = "terraform-aws-modules/autoscaling/aws" + version = "~> 7.0" + + # Truncated for brevity + + # Traffic source attachment + create_traffic_source_attachment = true + traffic_source_identifier = "arn:aws:elasticloadbalancing:us-east-2:123456789012:loadbalancer/app/my-load-balancer/1234567890123456" + traffic_source_type = "elbv2" + + ... +} +``` + +### After 8.x Example + +```hcl +module "asg" { + source = "terraform-aws-modules/autoscaling/aws" + version = "~> 8.0" + + # Truncated for brevity + + traffic_source_attachments = { + ex-alb = { + traffic_source_identifier = "arn:aws:elasticloadbalancing:us-east-2:123456789012:loadbalancer/app/my-load-balancer/1234567890123456" + traffic_source_type = "elbv2" + } + ... + } + ... +} +``` + +### State Changes + +There are no required state changes to migrate from `v7.x` to `v8.x`. diff --git a/examples/complete/README.md b/examples/complete/README.md index c058d3c..3a919a4 100644 --- a/examples/complete/README.md +++ b/examples/complete/README.md @@ -29,7 +29,7 @@ Note that this example may create resources which cost money. Run `terraform des | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 1.0 | +| [terraform](#requirement\_terraform) | >= 1.3 | | [aws](#requirement\_aws) | >= 5.55 | ## Providers diff --git a/examples/complete/main.tf b/examples/complete/main.tf index 135f134..e77a2c4 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -47,9 +47,12 @@ module "complete" { service_linked_role_arn = aws_iam_service_linked_role.autoscaling.arn # Traffic source attachment - create_traffic_source_attachment = true - traffic_source_identifier = module.alb.target_groups["ex_asg"].arn - traffic_source_type = "elbv2" + traffic_source_attachments = { + ex-alb = { + traffic_source_identifier = module.alb.target_groups["ex_asg"].arn + traffic_source_type = "elbv2" # default + } + } initial_lifecycle_hooks = [ { diff --git a/examples/complete/versions.tf b/examples/complete/versions.tf index 3617131..19f02e4 100644 --- a/examples/complete/versions.tf +++ b/examples/complete/versions.tf @@ -1,5 +1,5 @@ terraform { - required_version = ">= 1.0" + required_version = ">= 1.3" required_providers { aws = { diff --git a/main.tf b/main.tf index 824f8ee..5542e35 100644 --- a/main.tf +++ b/main.tf @@ -374,10 +374,9 @@ resource "aws_autoscaling_group" "this" { default_instance_warmup = var.default_instance_warmup protect_from_scale_in = var.protect_from_scale_in - # TODO - remove at next breaking change. Use `traffic_source_identifier`/`traffic_source_type` instead - load_balancers = var.load_balancers - # TODO - remove at next breaking change. Use `traffic_source_identifier`/`traffic_source_type` instead - target_group_arns = var.target_group_arns + # Use `traffic_source_identifier`/`traffic_source_type` instead + # load_balancers = var.load_balancers + # target_group_arns = var.target_group_arns placement_group = var.placement_group health_check_type = var.health_check_type health_check_grace_period = var.health_check_grace_period @@ -666,8 +665,9 @@ resource "aws_autoscaling_group" "idc" { default_instance_warmup = var.default_instance_warmup protect_from_scale_in = var.protect_from_scale_in - load_balancers = var.load_balancers - target_group_arns = var.target_group_arns + # Use `traffic_source_identifier`/`traffic_source_type` instead + # load_balancers = var.load_balancers + # target_group_arns = var.target_group_arns placement_group = var.placement_group health_check_type = var.health_check_type health_check_grace_period = var.health_check_grace_period @@ -928,13 +928,13 @@ resource "aws_autoscaling_group" "idc" { ################################################################################ resource "aws_autoscaling_traffic_source_attachment" "this" { - count = local.create && var.create_traffic_source_attachment ? 1 : 0 + for_each = { for k, v in var.traffic_source_attachments : k => v if local.create } autoscaling_group_name = var.ignore_desired_capacity_changes ? aws_autoscaling_group.idc[0].id : aws_autoscaling_group.this[0].id traffic_source { - identifier = var.traffic_source_identifier - type = var.traffic_source_type + identifier = each.value.traffic_source_identifier + type = try(each.value.traffic_source_type, "elbv2") } } diff --git a/variables.tf b/variables.tf index 8be37d6..81f4c14 100644 --- a/variables.tf +++ b/variables.tf @@ -121,18 +121,6 @@ variable "protect_from_scale_in" { default = false } -variable "load_balancers" { - description = "A list of elastic load balancer names to add to the autoscaling group names. Only valid for classic load balancers. For ALBs, use `target_group_arns` instead" - type = list(string) - default = [] -} - -variable "target_group_arns" { - description = "A set of `aws_alb_target_group` ARNs, for use with Application or Network Load Balancing" - type = list(string) - default = [] -} - variable "placement_group" { description = "The name of the placement group into which you'll launch your instances, if any" type = string @@ -472,22 +460,10 @@ variable "tag_specifications" { # Autoscaling group traffic source attachment ################################################################################ -variable "create_traffic_source_attachment" { - description = "Determines whether to create autoscaling group traffic source attachment" - type = bool - default = false -} - -variable "traffic_source_identifier" { - description = "Identifies the traffic source. For Application Load Balancers, Gateway Load Balancers, Network Load Balancers, and VPC Lattice, this will be the Amazon Resource Name (ARN) for a target group in this account and Region. For Classic Load Balancers, this will be the name of the Classic Load Balancer in this account and Region" - type = string - default = "" -} - -variable "traffic_source_type" { - description = "Provides additional context for the value of identifier. The following lists the valid values: `elb` if `identifier` is the name of a Classic Load Balancer. `elbv2` if `identifier` is the ARN of an Application Load Balancer, Gateway Load Balancer, or Network Load Balancer target group. `vpc-lattice` if `identifier` is the ARN of a VPC Lattice target group" - type = string - default = "elbv2" +variable "traffic_source_attachments" { + description = "Map of traffic source attachment definitions to create" + type = any + default = {} } ################################################################################ diff --git a/versions.tf b/versions.tf index 3617131..19f02e4 100644 --- a/versions.tf +++ b/versions.tf @@ -1,5 +1,5 @@ terraform { - required_version = ">= 1.0" + required_version = ">= 1.3" required_providers { aws = {