Skip to content

Commit b53ce70

Browse files
feat: Additional target group attachments (#351)
Co-authored-by: Bryant Biggs <[email protected]>
1 parent 3e9c6cb commit b53ce70

File tree

8 files changed

+41
-4
lines changed

8 files changed

+41
-4
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.83.5
3+
rev: v1.86.0
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_wrapper_module_for_each
@@ -28,3 +28,4 @@ repos:
2828
hooks:
2929
- id: check-merge-conflict
3030
- id: end-of-file-fixer
31+
- id: trailing-whitespace

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ No modules.
371371
| [aws_lb_listener_certificate.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener_certificate) | resource |
372372
| [aws_lb_listener_rule.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener_rule) | resource |
373373
| [aws_lb_target_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group) | resource |
374+
| [aws_lb_target_group_attachment.additional](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group_attachment) | resource |
374375
| [aws_lb_target_group_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group_attachment) | resource |
375376
| [aws_route53_record.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
376377
| [aws_security_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
@@ -384,6 +385,7 @@ No modules.
384385
| Name | Description | Type | Default | Required |
385386
|------|-------------|------|---------|:--------:|
386387
| <a name="input_access_logs"></a> [access\_logs](#input\_access\_logs) | Map containing access logging configuration for load balancer | `map(string)` | `{}` | no |
388+
| <a name="input_additional_target_group_attachments"></a> [additional\_target\_group\_attachments](#input\_additional\_target\_group\_attachments) | Map of additional target group attachments to create. Use `target_group_key` to attach to the target group created in `target_groups` | `any` | `{}` | no |
387389
| <a name="input_associate_web_acl"></a> [associate\_web\_acl](#input\_associate\_web\_acl) | Indicates whether a Web Application Firewall (WAF) ACL should be associated with the load balancer | `bool` | `false` | no |
388390
| <a name="input_connection_logs"></a> [connection\_logs](#input\_connection\_logs) | Map containing access logging configuration for load balancer | `map(string)` | `{}` | no |
389391
| <a name="input_create"></a> [create](#input\_create) | Controls if resources should be created (affects nearly all resources) | `bool` | `true` | no |

UPGRADE-5.0.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ If you found a bug, please open an issue in this repository.
1818
- aws_lb_listener.frontend_http_tcp_no_logs
1919
- aws_lb_listener.frontend_https_no_logs
2020
- aws_lb_listener_certificate.https_listener_no_logs
21-
21+
2222
If you've been using ALB without access logs enabled then you need to run `terraform state mv` to rename resources to new names:
23-
23+
2424
- aws_lb.this
2525
- aws_lb_target_group.main
2626
- aws_lb_listener.frontend_http_tcp
2727
- aws_lb_listener.frontend_https
2828
- aws_lb_listener_certificate.https_listener
2929

3030
For example, this command will rename ALB resource: `terraform state mv aws_lb.application_no_logs aws_lb.this`
31-
31+
3232
2. Removed variable `target_groups_count`, `http_tcp_listeners_count`, `extra_ssl_certs_count`, `http_tcp_listeners_count`.
3333

3434
3. Removed variable `target_groups_defaults`. Instead, all `health_check` and `stickiness` settings should be implicit for each target group.

examples/complete-alb/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Note that this example may create resources which cost money. Run `terraform des
5050
| [aws_cognito_user_pool.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cognito_user_pool) | resource |
5151
| [aws_cognito_user_pool_client.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cognito_user_pool_client) | resource |
5252
| [aws_cognito_user_pool_domain.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cognito_user_pool_domain) | resource |
53+
| [aws_instance.other](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource |
5354
| [aws_instance.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance) | resource |
5455
| [null_resource.download_package](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
5556
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |

examples/complete-alb/main.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,15 @@ module "alb" {
398398
}
399399
}
400400

401+
additional_target_group_attachments = {
402+
ex-instance-other = {
403+
target_group_key = "ex-instance"
404+
target_type = "instance"
405+
target_id = aws_instance.other.id
406+
port = "80"
407+
}
408+
}
409+
401410
# Route53 Record(s)
402411
route53_records = {
403412
A = {
@@ -530,6 +539,12 @@ resource "aws_instance" "this" {
530539
subnet_id = element(module.vpc.private_subnets, 0)
531540
}
532541

542+
resource "aws_instance" "other" {
543+
ami = data.aws_ssm_parameter.al2.value
544+
instance_type = "t3.nano"
545+
subnet_id = element(module.vpc.private_subnets, 0)
546+
}
547+
533548
##################################################################
534549
# AWS Cognito User Pool
535550
##################################################################

main.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,17 @@ resource "aws_lb_target_group_attachment" "this" {
541541
depends_on = [aws_lambda_permission.this]
542542
}
543543

544+
resource "aws_lb_target_group_attachment" "additional" {
545+
for_each = { for k, v in var.additional_target_group_attachments : k => v if local.create }
546+
547+
target_group_arn = aws_lb_target_group.this[each.value.target_group_key].arn
548+
target_id = each.value.target_id
549+
port = try(each.value.target_type, null) == "lambda" ? null : try(each.value.port, var.default_port)
550+
availability_zone = try(each.value.availability_zone, null)
551+
552+
depends_on = [aws_lambda_permission.this]
553+
}
554+
544555
################################################################################
545556
# Lambda Permission
546557
################################################################################

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ variable "target_groups" {
196196
default = {}
197197
}
198198

199+
variable "additional_target_group_attachments" {
200+
description = "Map of additional target group attachments to create. Use `target_group_key` to attach to the target group created in `target_groups`"
201+
type = any
202+
default = {}
203+
}
204+
199205
################################################################################
200206
# Security Group
201207
################################################################################

wrappers/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module "wrapper" {
44
for_each = var.items
55

66
access_logs = try(each.value.access_logs, var.defaults.access_logs, {})
7+
additional_target_group_attachments = try(each.value.additional_target_group_attachments, var.defaults.additional_target_group_attachments, {})
78
associate_web_acl = try(each.value.associate_web_acl, var.defaults.associate_web_acl, false)
89
connection_logs = try(each.value.connection_logs, var.defaults.connection_logs, {})
910
create = try(each.value.create, var.defaults.create, true)

0 commit comments

Comments
 (0)