-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
aws_lb_target_group_attachment: target_id should be a list #9901
Comments
Seems like starting from Pitty I was not able to find it in official docs, but stackoverflow saved my day https://stackoverflow.com/a/58158252 kudos to
|
I've been struggling with a slightly different usecase which this proposal would also resolve. I've created an ALB module which sets up certificates, target groups, listeners, listener rules and attachments in addition to the load balancer itself. The module takes in a few lists of instance IDs (one list per target group). As the instances are produced by another module, it is currently (to the best of my understanding) impossible to create these attachments in any other way than passing in a separate variable per target group which specifies the size of the group - this is because terraform cannot resolve a plan if the number of resources is not known. Here's a code snippet of what I was attempting to do:
This works great if the instances already exist but fails completely if they don't - and also causes problems any time an instance is terminated outside of terraform (eg. when AWS retires one of them). Being able to pass a list of targets to the |
I'm having the same issue, instances are being created in a separate module so |
This seems to be a huge problem for attaching a LB to an autoscaling group as well. When editing target groups manually, i can easily add instances with multiple ports at once, but if i try to work around it in the provider by creating multiple target groups for each port and corresponding attachments that attach my LB to the ASG, i will get multiple target groups but from the attachments, only one will actually attach to the ASG. So there appears to be no way currently to solve this. |
the only workaround if you have multiple target_id's and multiple target_group_arn's at the moment is basically copying the block to the amount of the target_id/target_group_arn, counting or for_each through the resources and referencing the other by [0], [1] and so on. for instance, like this: resource "aws_lb_target_group_attachment" "network1" { or `resource "aws_lb_target_group_attachment" "network0" { resource "aws_lb_target_group_attachment" "network1" { At least I don't know another way at the moment. Hope that example might help someone, or someone has a better way to handle it for now. Cheers |
I pretty much have the same requirement. I have a user controlled number of instances and a user controlled number of ports I want to configure on a load balancer. I can create target groups for every port (e.g 80,443,8089) but I cannot create the target group attachments for each instance AND each target group. Any other workarounds for this situation? UPDATE Initially I tried to reference the instance ids and target_group_arn directly but would get an error stating that I cannot use a resource that has not already been created. The error was specifically this:
This caused me to use setproduct to build a list of indexes based on the input values. variables
Use setproduct to find all of the possible combinations of element
Use foreach to create a map, loop through it and reference the the target group arn and instance ids based off of index:
This dynamically attaches instances to target groups based on the input values. For clarity when using a var.listener_port of
Hope this helps someone. |
Changing |
Sounds good, get it done! Six months too late for me, sadly. One of those ugly workarounds is going into my code. |
For others encountering this issue, as Ajay suggests above, locals {
# This produces a list of lists with contents like:
# [[arn_1, instance_id_1], [arn_1, instance_id_2], [arn_2, instance_id_1], [arn_2, instance_id_2]] etc
product_list = setproduct(aws_lb_target_group.dynamic[*].arn, var.instance_ids)
}
resource "aws_lb_target_group_attachment" "dynamic_attachment" {
# We need to create an attachment for each instance on each additional port
# Therefore we need numInstances * numPorts and use `setproduct` to find the possible combinations
# There is an outstanding Github issue to change `target_id` to accept a list as input instead of a string
# https://github.com/hashicorp/terraform-provider-aws/issues/9901
count = length(local.product_list)
target_group_arn = local.product_list[count.index][0]
target_id = local.product_list[count.index][1]
} If either input list is empty, |
Hello everyone - We've started work on this issue and wanted to communicate our proposed path forward. BackgroundThe
Each of these options requires a trade off. Option 1 (new, plural resource) would introduce a second method for registering target groups, at least until a major release where the singular attachment resource can be removed. Option 2 (new ProposalThe recommendation is to proceed with a new, plural target registration resource (Option 1) that more closely aligns with the AWS API. While this does introduce a second method for registering target groups, it does not directly conflict with the existing singular resource, and will not cause target groups defined with that resource to be removed. It also avoids overloading a single resource with multiple ways to specify the same infrastructure and repetitive argument names. Lastly, there is a clear path to deprecating the The proposed resource configuration would be: resource "aws_lb_target_registration" "example" {
target_group_arn = aws_lb_target_group.example.arn
target {
target_id = aws_instance.example1.id
port = 80
availability_zone = "us-east-1a"
}
target {
target_id = aws_instance.example2.id
port = 8080
availability_zone = "us-west-2c"
}
} With this approach, the existing target group attachment resources will be deprecated. The documentation should be updated to recommend usage of the plural registration resource, and an issue should be created for removal of the resource in a future major version. |
Hello everyone - after some internal discussion, we decided to step back and assess how "relationship" resources have historically been handled in the AWS provider. This research has been collected in a new proposal #32552, currently open for community feedback. In summary, the AWS provider has historically maintained a strong preference for "singular" representations of parent-child relationships (ie. one child per resource versus many in a single resource). For this feature request, this means we'd propose leaving Given that this design decision has a practical impact on the resolution of this issue, please feel free to leave feedback on the proposal or documentation additions. We'll plan to leave it open for at least the next week to allow time for community suggestions. Thanks for your patience! |
Closed via #32518 |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Community Note
Description
When there are multiple target_groups for a LB and multiple instances needed to be attached to each target_group, there is no easy way to do that for reason
target_id
only being a string inaws_lb_target_group_attachment
resource. Whilst AWS console allows to select multiple instances during the target attachment, terraform should behave the similar way as well.The register-targets is the AWS CLI equivalent (to aws_lb_target_group_attachment) and there,
--targets
is a list. An example in that page says:The Go SDK also has the similar
RegisterTargets
, whereTargets
also a list. So I think it's well supported in the API layer.New or Affected Resource(s)
Potential Terraform Configuration
vars.tf
instances.tf
balencer.tf
References
https://discuss.hashicorp.com/t/aws-lb-target-group-attachment-how-to-add-multiple-instances-per-lb-listeners
https://docs.aws.amazon.com/elasticloadbalancing/latest/APIReference/API_RegisterTargets.html
https://docs.aws.amazon.com/sdk-for-go/api/service/elbv2/#ELBV2.RegisterTargets
https://docs.aws.amazon.com/cli/latest/reference/elbv2/register-targets.html
The text was updated successfully, but these errors were encountered: