From fd1599d82349e8d5f4abdd3edde0d5e0b7aea1dd Mon Sep 17 00:00:00 2001 From: Bogdan George Barna Date: Wed, 25 Oct 2023 19:10:45 +0300 Subject: [PATCH 01/10] Add optional network_interface_id variable --- main.tf | 12 +++++++----- variables.tf | 18 ++++++++++++++++-- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/main.tf b/main.tf index 73e6244..0699d1d 100644 --- a/main.tf +++ b/main.tf @@ -94,11 +94,12 @@ resource "aws_launch_template" "default" { # https://github.com/terraform-providers/terraform-provider-aws/issues/4570 network_interfaces { - description = module.this.id + description = var.network_interface_id == null ? module.this.id : null device_index = 0 - associate_public_ip_address = var.associate_public_ip_address - delete_on_termination = true - security_groups = var.security_group_ids + associate_public_ip_address = var.network_interface_id == null ? var.associate_public_ip_address : null + delete_on_termination = var.network_interface_id == null ? true : false + security_groups = var.network_interface_id == null ? var.security_group_ids : null + network_interface_id = var.network_interface_id } metadata_options { @@ -149,7 +150,8 @@ resource "aws_autoscaling_group" "default" { count = module.this.enabled ? 1 : 0 name_prefix = format("%s%s", module.this.id, module.this.delimiter) - vpc_zone_identifier = var.subnet_ids + vpc_zone_identifier = var.availability_zones == null ? var.subnet_ids : null + availability_zones = var.subnet_ids == null ? var.availability_zones : null max_size = var.max_size min_size = var.min_size load_balancers = var.load_balancers diff --git a/variables.tf b/variables.tf index 1d66390..06fa2ac 100644 --- a/variables.tf +++ b/variables.tf @@ -40,11 +40,18 @@ variable "launch_template_version" { } variable "associate_public_ip_address" { - type = bool - description = "Associate a public IP address with an instance in a VPC" + type = bool + # https://stackoverflow.com/a/76808361 + description = "Associate a public IP address with an instance in a VPC. If a network_interface id is specified, this can only be false." default = false } +variable "network_interface_id" { + type = string + description = "The ID of the network interface to attach. If specified, all the other network_interface block arguments are ignored." + default = null +} + variable "user_data_base64" { type = string description = "The Base64-encoded user data to provide when launching the instances" @@ -196,8 +203,15 @@ variable "min_size" { } variable "subnet_ids" { + type = list(string) description = "A list of subnet IDs to launch resources in" + default = null +} + +variable "availability_zones" { type = list(string) + description = "A list of Availability Zones where instances in the Auto Scaling group can be created. Used for launching into the default VPC subnet in each Availability Zone when not using the subnet_ids variable, or for attaching a network interface when an existing network interface ID is specified. Conflicts with subnet_ids." + default = null } variable "default_cooldown" { From 2189b985213b71007d01ad3611f9abb976783e55 Mon Sep 17 00:00:00 2001 From: Bogdan George Barna Date: Fri, 27 Oct 2023 12:02:51 +0300 Subject: [PATCH 02/10] Update var.associate_public_ip_address description --- variables.tf | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/variables.tf b/variables.tf index 06fa2ac..ac0a155 100644 --- a/variables.tf +++ b/variables.tf @@ -40,9 +40,8 @@ variable "launch_template_version" { } variable "associate_public_ip_address" { - type = bool - # https://stackoverflow.com/a/76808361 - description = "Associate a public IP address with an instance in a VPC. If a network_interface id is specified, this can only be false." + type = bool + description = "Associate a public IP address with an instance in a VPC. If `network_interface_id` is specified, this can only be `false` (see here for more info: https://stackoverflow.com/a/76808361)." default = false } From e266e799d972dc7373b0edfd3bf3340245bc292c Mon Sep 17 00:00:00 2001 From: Bogdan George Barna Date: Fri, 27 Oct 2023 12:06:05 +0300 Subject: [PATCH 03/10] Remove var.availability_zones, use data.aws_subnet --- main.tf | 13 +++++++++++-- variables.tf | 6 ------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/main.tf b/main.tf index 0699d1d..a0bd630 100644 --- a/main.tf +++ b/main.tf @@ -1,3 +1,12 @@ +data "aws_subnet" "this" { + for_each = toset(var.subnet_ids) + id = each.value +} + +locals { + availability_zones = [for subnet in data.aws_subnet.this : subnet.availability_zone] +} + resource "aws_launch_template" "default" { count = module.this.enabled ? 1 : 0 @@ -150,8 +159,8 @@ resource "aws_autoscaling_group" "default" { count = module.this.enabled ? 1 : 0 name_prefix = format("%s%s", module.this.id, module.this.delimiter) - vpc_zone_identifier = var.availability_zones == null ? var.subnet_ids : null - availability_zones = var.subnet_ids == null ? var.availability_zones : null + vpc_zone_identifier = var.network_interface_id == null ? var.subnet_ids : null + availability_zones = var.network_interface_id != null ? local.availability_zones : null max_size = var.max_size min_size = var.min_size load_balancers = var.load_balancers diff --git a/variables.tf b/variables.tf index ac0a155..4d4af4d 100644 --- a/variables.tf +++ b/variables.tf @@ -207,12 +207,6 @@ variable "subnet_ids" { default = null } -variable "availability_zones" { - type = list(string) - description = "A list of Availability Zones where instances in the Auto Scaling group can be created. Used for launching into the default VPC subnet in each Availability Zone when not using the subnet_ids variable, or for attaching a network interface when an existing network interface ID is specified. Conflicts with subnet_ids." - default = null -} - variable "default_cooldown" { type = number description = "The amount of time, in seconds, after a scaling activity completes before another scaling activity can start" From 34afef14c1e252112703e2dc58c5946c75a6bfb2 Mon Sep 17 00:00:00 2001 From: Bogdan George Barna Date: Fri, 27 Oct 2023 12:07:25 +0300 Subject: [PATCH 04/10] Update README --- .github/renovate.json | 7 ++++--- README.md | 6 ++++-- docs/terraform.md | 6 ++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.github/renovate.json b/.github/renovate.json index b61ed24..909df09 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -1,13 +1,14 @@ { "extends": [ "config:base", - ":preserveSemverRanges" + ":preserveSemverRanges", + ":rebaseStalePrs" ], - "baseBranches": ["main", "master", "/^release\\/v\\d{1,2}$/"], + "baseBranches": ["main"], "labels": ["auto-update"], "dependencyDashboardAutoclose": true, "enabledManagers": ["terraform"], "terraform": { - "ignorePaths": ["**/context.tf", "examples/**"] + "ignorePaths": ["**/context.tf"] } } diff --git a/README.md b/README.md index 6c6092f..b04e308 100644 --- a/README.md +++ b/README.md @@ -224,13 +224,14 @@ Available targets: | [aws_autoscaling_policy.scale_up](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_policy) | resource | | [aws_cloudwatch_metric_alarm.all_alarms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource | | [aws_launch_template.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource | +| [aws_subnet.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet) | data source | ## Inputs | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no | -| [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | Associate a public IP address with an instance in a VPC | `bool` | `false` | no | +| [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | Associate a public IP address with an instance in a VPC. If `network_interface_id` is specified, this can only be `false` (see here for more info: https://stackoverflow.com/a/76808361). | `bool` | `false` | no | | [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the `delimiter`
and treated as a single ID element. | `list(string)` | `[]` | no | | [autoscaling\_policies\_enabled](#input\_autoscaling\_policies\_enabled) | Whether to create `aws_autoscaling_policy` and `aws_cloudwatch_metric_alarm` resources to control Auto Scaling | `bool` | `true` | no | | [block\_device\_mappings](#input\_block\_device\_mappings) | Specify volumes to attach to the instance besides the volumes specified by the AMI |
list(object({
device_name = string
no_device = bool
virtual_name = string
ebs = object({
delete_on_termination = bool
encrypted = bool
iops = number
kms_key_id = string
snapshot_id = string
volume_size = number
volume_type = string
})
}))
| `[]` | no | @@ -289,6 +290,7 @@ Available targets: | [mixed\_instances\_policy](#input\_mixed\_instances\_policy) | policy to used mixed group of on demand/spot of differing types. Launch template is automatically generated. https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html#mixed_instances_policy-1 |
object({
instances_distribution = object({
on_demand_allocation_strategy = string
on_demand_base_capacity = number
on_demand_percentage_above_base_capacity = number
spot_allocation_strategy = string
spot_instance_pools = number
spot_max_price = string
})
override = list(object({
instance_type = string
weighted_capacity = number
}))
})
| `null` | no | | [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a `tag`.
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no | | [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no | +| [network\_interface\_id](#input\_network\_interface\_id) | The ID of the network interface to attach. If specified, all the other network\_interface block arguments are ignored. | `string` | `null` | no | | [placement](#input\_placement) | The placement specifications of the instances |
object({
affinity = string
availability_zone = string
group_name = string
host_id = string
tenancy = string
})
| `null` | no | | [placement\_group](#input\_placement\_group) | The name of the placement group into which you'll launch your instances, if any | `string` | `""` | no | | [protect\_from\_scale\_in](#input\_protect\_from\_scale\_in) | Allows setting instance protection. The autoscaling group will not select instances with this setting for terminination during scale in events | `bool` | `false` | no | @@ -304,7 +306,7 @@ Available targets: | [security\_group\_ids](#input\_security\_group\_ids) | A list of associated security group IDs | `list(string)` | `[]` | no | | [service\_linked\_role\_arn](#input\_service\_linked\_role\_arn) | The ARN of the service-linked role that the ASG will use to call other AWS services | `string` | `""` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | -| [subnet\_ids](#input\_subnet\_ids) | A list of subnet IDs to launch resources in | `list(string)` | n/a | yes | +| [subnet\_ids](#input\_subnet\_ids) | A list of subnet IDs to launch resources in | `list(string)` | `null` | no | | [suspended\_processes](#input\_suspended\_processes) | A list of processes to suspend for the AutoScaling Group. The allowed values are `Launch`, `Terminate`, `HealthCheck`, `ReplaceUnhealthy`, `AZRebalance`, `AlarmNotification`, `ScheduledActions`, `AddToLoadBalancer`. Note that if you suspend either the `Launch` or `Terminate` process types, it can prevent your autoscaling group from functioning properly. | `list(string)` | `[]` | no | | [tag\_specifications\_resource\_types](#input\_tag\_specifications\_resource\_types) | List of tag specification resource types to tag. Valid values are instance, volume, elastic-gpu and spot-instances-request. | `set(string)` |
[
"instance",
"volume"
]
| no | | [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no | diff --git a/docs/terraform.md b/docs/terraform.md index d2563ea..c5b81dc 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -27,13 +27,14 @@ | [aws_autoscaling_policy.scale_up](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_policy) | resource | | [aws_cloudwatch_metric_alarm.all_alarms](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource | | [aws_launch_template.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template) | resource | +| [aws_subnet.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet) | data source | ## Inputs | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no | -| [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | Associate a public IP address with an instance in a VPC | `bool` | `false` | no | +| [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | Associate a public IP address with an instance in a VPC. If `network_interface_id` is specified, this can only be `false` (see here for more info: https://stackoverflow.com/a/76808361). | `bool` | `false` | no | | [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the `delimiter`
and treated as a single ID element. | `list(string)` | `[]` | no | | [autoscaling\_policies\_enabled](#input\_autoscaling\_policies\_enabled) | Whether to create `aws_autoscaling_policy` and `aws_cloudwatch_metric_alarm` resources to control Auto Scaling | `bool` | `true` | no | | [block\_device\_mappings](#input\_block\_device\_mappings) | Specify volumes to attach to the instance besides the volumes specified by the AMI |
list(object({
device_name = string
no_device = bool
virtual_name = string
ebs = object({
delete_on_termination = bool
encrypted = bool
iops = number
kms_key_id = string
snapshot_id = string
volume_size = number
volume_type = string
})
}))
| `[]` | no | @@ -92,6 +93,7 @@ | [mixed\_instances\_policy](#input\_mixed\_instances\_policy) | policy to used mixed group of on demand/spot of differing types. Launch template is automatically generated. https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html#mixed_instances_policy-1 |
object({
instances_distribution = object({
on_demand_allocation_strategy = string
on_demand_base_capacity = number
on_demand_percentage_above_base_capacity = number
spot_allocation_strategy = string
spot_instance_pools = number
spot_max_price = string
})
override = list(object({
instance_type = string
weighted_capacity = number
}))
})
| `null` | no | | [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a `tag`.
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no | | [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no | +| [network\_interface\_id](#input\_network\_interface\_id) | The ID of the network interface to attach. If specified, all the other network\_interface block arguments are ignored. | `string` | `null` | no | | [placement](#input\_placement) | The placement specifications of the instances |
object({
affinity = string
availability_zone = string
group_name = string
host_id = string
tenancy = string
})
| `null` | no | | [placement\_group](#input\_placement\_group) | The name of the placement group into which you'll launch your instances, if any | `string` | `""` | no | | [protect\_from\_scale\_in](#input\_protect\_from\_scale\_in) | Allows setting instance protection. The autoscaling group will not select instances with this setting for terminination during scale in events | `bool` | `false` | no | @@ -107,7 +109,7 @@ | [security\_group\_ids](#input\_security\_group\_ids) | A list of associated security group IDs | `list(string)` | `[]` | no | | [service\_linked\_role\_arn](#input\_service\_linked\_role\_arn) | The ARN of the service-linked role that the ASG will use to call other AWS services | `string` | `""` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | -| [subnet\_ids](#input\_subnet\_ids) | A list of subnet IDs to launch resources in | `list(string)` | n/a | yes | +| [subnet\_ids](#input\_subnet\_ids) | A list of subnet IDs to launch resources in | `list(string)` | `null` | no | | [suspended\_processes](#input\_suspended\_processes) | A list of processes to suspend for the AutoScaling Group. The allowed values are `Launch`, `Terminate`, `HealthCheck`, `ReplaceUnhealthy`, `AZRebalance`, `AlarmNotification`, `ScheduledActions`, `AddToLoadBalancer`. Note that if you suspend either the `Launch` or `Terminate` process types, it can prevent your autoscaling group from functioning properly. | `list(string)` | `[]` | no | | [tag\_specifications\_resource\_types](#input\_tag\_specifications\_resource\_types) | List of tag specification resource types to tag. Valid values are instance, volume, elastic-gpu and spot-instances-request. | `set(string)` |
[
"instance",
"volume"
]
| no | | [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no | From 34cee1478959a662bffc2996aaeb7ff8e104f54d Mon Sep 17 00:00:00 2001 From: Bogdan George Barna Date: Tue, 31 Oct 2023 20:25:26 +0200 Subject: [PATCH 05/10] Remove var.subnet_ids default value --- variables.tf | 1 - 1 file changed, 1 deletion(-) diff --git a/variables.tf b/variables.tf index 47de89e..dd22d55 100644 --- a/variables.tf +++ b/variables.tf @@ -205,7 +205,6 @@ variable "min_size" { variable "subnet_ids" { type = list(string) description = "A list of subnet IDs to launch resources in" - default = null } variable "default_cooldown" { From 2d0cff493b4abe449527da84af96008e35e51d25 Mon Sep 17 00:00:00 2001 From: Bogdan George Barna Date: Tue, 31 Oct 2023 20:26:31 +0200 Subject: [PATCH 06/10] Coalesce locals blocks in main.tf --- main.tf | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/main.tf b/main.tf index 735bfe4..9e99cee 100644 --- a/main.tf +++ b/main.tf @@ -3,10 +3,6 @@ data "aws_subnet" "this" { id = each.value } -locals { - availability_zones = [for subnet in data.aws_subnet.this : subnet.availability_zone] -} - resource "aws_launch_template" "default" { count = module.this.enabled ? 1 : 0 @@ -150,6 +146,7 @@ locals { launch_template = local.launch_template_block override = var.mixed_instances_policy.override }) + availability_zones = [for subnet in data.aws_subnet.this : subnet.availability_zone] tags = { for key, value in module.this.tags : key => value if value != "" && value != null From 71788402784a91900164daf14f96433762141ce4 Mon Sep 17 00:00:00 2001 From: Bogdan George Barna Date: Tue, 31 Oct 2023 20:37:21 +0200 Subject: [PATCH 07/10] Update README --- README.md | 2 +- docs/terraform.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 00b6ec6..81315b7 100644 --- a/README.md +++ b/README.md @@ -306,7 +306,7 @@ Available targets: | [security\_group\_ids](#input\_security\_group\_ids) | A list of associated security group IDs | `list(string)` | `[]` | no | | [service\_linked\_role\_arn](#input\_service\_linked\_role\_arn) | The ARN of the service-linked role that the ASG will use to call other AWS services | `string` | `""` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | -| [subnet\_ids](#input\_subnet\_ids) | A list of subnet IDs to launch resources in | `list(string)` | `null` | no | +| [subnet\_ids](#input\_subnet\_ids) | A list of subnet IDs to launch resources in | `list(string)` | n/a | yes | | [suspended\_processes](#input\_suspended\_processes) | A list of processes to suspend for the AutoScaling Group. The allowed values are `Launch`, `Terminate`, `HealthCheck`, `ReplaceUnhealthy`, `AZRebalance`, `AlarmNotification`, `ScheduledActions`, `AddToLoadBalancer`. Note that if you suspend either the `Launch` or `Terminate` process types, it can prevent your autoscaling group from functioning properly. | `list(string)` | `[]` | no | | [tag\_specifications\_resource\_types](#input\_tag\_specifications\_resource\_types) | List of tag specification resource types to tag. Valid values are instance, volume, elastic-gpu and spot-instances-request. | `set(string)` |
[
"instance",
"volume"
]
| no | | [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no | diff --git a/docs/terraform.md b/docs/terraform.md index dacf82b..5f57342 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -109,7 +109,7 @@ | [security\_group\_ids](#input\_security\_group\_ids) | A list of associated security group IDs | `list(string)` | `[]` | no | | [service\_linked\_role\_arn](#input\_service\_linked\_role\_arn) | The ARN of the service-linked role that the ASG will use to call other AWS services | `string` | `""` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | -| [subnet\_ids](#input\_subnet\_ids) | A list of subnet IDs to launch resources in | `list(string)` | `null` | no | +| [subnet\_ids](#input\_subnet\_ids) | A list of subnet IDs to launch resources in | `list(string)` | n/a | yes | | [suspended\_processes](#input\_suspended\_processes) | A list of processes to suspend for the AutoScaling Group. The allowed values are `Launch`, `Terminate`, `HealthCheck`, `ReplaceUnhealthy`, `AZRebalance`, `AlarmNotification`, `ScheduledActions`, `AddToLoadBalancer`. Note that if you suspend either the `Launch` or `Terminate` process types, it can prevent your autoscaling group from functioning properly. | `list(string)` | `[]` | no | | [tag\_specifications\_resource\_types](#input\_tag\_specifications\_resource\_types) | List of tag specification resource types to tag. Valid values are instance, volume, elastic-gpu and spot-instances-request. | `set(string)` |
[
"instance",
"volume"
]
| no | | [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no | From 2ff5bfb779e19a00fdde8f64afbf90daecd72a08 Mon Sep 17 00:00:00 2001 From: Bogdan George Barna Date: Wed, 1 Nov 2023 13:16:51 +0200 Subject: [PATCH 08/10] Allow for var.subnet_ids to be an empty list in data.aws_subnet.this --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 9e99cee..4ef8147 100644 --- a/main.tf +++ b/main.tf @@ -1,5 +1,5 @@ data "aws_subnet" "this" { - for_each = toset(var.subnet_ids) + for_each = toset(length(var.subnet_ids) > 0 ? var.subnet_ids : []) id = each.value } From 9923498446e80f209d37aafa0a13c60e5fb8209d Mon Sep 17 00:00:00 2001 From: Bogdan George Barna Date: Thu, 2 Nov 2023 12:59:47 +0200 Subject: [PATCH 09/10] Change tomap() to map comprehension --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index 4ef8147..ac7b0e8 100644 --- a/main.tf +++ b/main.tf @@ -1,5 +1,5 @@ data "aws_subnet" "this" { - for_each = toset(length(var.subnet_ids) > 0 ? var.subnet_ids : []) + for_each = length(var.subnet_ids) > 0 ? { for idx, subnet in var.subnet_ids : index => subnet } : {} id = each.value } From e794d11999bb40ae3b3fe9d55ee71a4f81d9a4e4 Mon Sep 17 00:00:00 2001 From: Bogdan George Barna Date: Thu, 2 Nov 2023 20:08:08 +0200 Subject: [PATCH 10/10] Fix idx ref in map expression --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index ac7b0e8..9538e06 100644 --- a/main.tf +++ b/main.tf @@ -1,5 +1,5 @@ data "aws_subnet" "this" { - for_each = length(var.subnet_ids) > 0 ? { for idx, subnet in var.subnet_ids : index => subnet } : {} + for_each = length(var.subnet_ids) > 0 ? { for idx, subnet in var.subnet_ids : idx => subnet } : {} id = each.value }