Skip to content

Commit ba77760

Browse files
authored
feat: Added weighted-forward rules for HTTP (#236)
1 parent fd88b42 commit ba77760

File tree

7 files changed

+97
-30
lines changed

7 files changed

+97
-30
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
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.62.3
3+
rev: v1.67.0
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_validate
@@ -23,7 +23,7 @@ repos:
2323
- '--args=--only=terraform_standard_module_structure'
2424
- '--args=--only=terraform_workspace_remote'
2525
- repo: https://github.com/pre-commit/pre-commit-hooks
26-
rev: v4.1.0
26+
rev: v4.2.0
2727
hooks:
2828
- id: check-merge-conflict
2929
- id: end-of-file-fixer

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ It's recommended you use this module with [terraform-aws-vpc](https://registry.t
268268

269269
## Notes
270270

271-
1. Terraform AWS provider >= v2.39.0 (via Terraform >= 0.12) has [issue #16674](https://github.com/hashicorp/terraform-provider-aws/issues/16674) related to "Provider produced inconsistent final plan". It means that S3 bucket has to be created before referencing it as an argument inside `access_logs = { bucket = "my-already-created-bucket-for-logs" }`, so this won't work: `access_logs = { bucket = module.log_bucket.s3_bucket_id }`.
271+
1. Terraform AWS provider version v2.39.0 and newer has [issue #16674](https://github.com/hashicorp/terraform-provider-aws/issues/16674) related to "Provider produced inconsistent final plan". It means that S3 bucket has to be created before referencing it as an argument inside `access_logs = { bucket = "my-already-created-bucket-for-logs" }`, so this won't work: `access_logs = { bucket = module.log_bucket.s3_bucket_id }`.
272272

273273
## Conditional creation
274274

examples/complete-alb/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Note that this example may create resources which cost money. Run `terraform des
3838
|------|--------|---------|
3939
| <a name="module_acm"></a> [acm](#module\_acm) | terraform-aws-modules/acm/aws | ~> 3.0 |
4040
| <a name="module_alb"></a> [alb](#module\_alb) | ../../ | n/a |
41-
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 2.0 |
41+
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 3.0 |
4242
| <a name="module_lb_disabled"></a> [lb\_disabled](#module\_lb\_disabled) | ../../ | n/a |
4343
| <a name="module_security_group"></a> [security\_group](#module\_security\_group) | terraform-aws-modules/security-group/aws | ~> 4.0 |
4444

@@ -54,7 +54,7 @@ Note that this example may create resources which cost money. Run `terraform des
5454
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
5555
| [aws_ami.amazon_linux](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami) | data source |
5656
| [aws_route53_zone.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route53_zone) | data source |
57-
| [aws_subnet_ids.all](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | data source |
57+
| [aws_subnets.all](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
5858
| [aws_vpc.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
5959

6060
## Inputs

examples/complete-alb/main.tf

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ data "aws_vpc" "default" {
1313
default = true
1414
}
1515

16-
data "aws_subnet_ids" "all" {
17-
vpc_id = data.aws_vpc.default.id
16+
data "aws_subnets" "all" {
17+
filter {
18+
name = "vpc-id"
19+
values = [data.aws_vpc.default.id]
20+
}
1821
}
1922

2023
resource "random_pet" "this" {
@@ -38,21 +41,21 @@ module "security_group" {
3841
egress_rules = ["all-all"]
3942
}
4043

41-
# module "log_bucket" {
42-
# source = "terraform-aws-modules/s3-bucket/aws"
43-
# version = "~> 1.0"
44+
#module "log_bucket" {
45+
# source = "terraform-aws-modules/s3-bucket/aws"
46+
# version = "~> 3.0"
4447
#
45-
# bucket = "logs-${random_pet.this.id}"
46-
# acl = "log-delivery-write"
47-
# force_destroy = true
48-
# attach_elb_log_delivery_policy = true
49-
# }
48+
# bucket = "logs-${random_pet.this.id}"
49+
# acl = "log-delivery-write"
50+
# force_destroy = true
51+
# attach_elb_log_delivery_policy = true
52+
#}
5053

5154
module "acm" {
5255
source = "terraform-aws-modules/acm/aws"
5356
version = "~> 3.0"
5457

55-
domain_name = local.domain_name # trimsuffix(data.aws_route53_zone.this.name, ".") # Terraform >= 0.12.17
58+
domain_name = local.domain_name # trimsuffix(data.aws_route53_zone.this.name, ".")
5659
zone_id = data.aws_route53_zone.this.id
5760
}
5861

@@ -90,7 +93,7 @@ module "alb" {
9093

9194
vpc_id = data.aws_vpc.default.id
9295
security_groups = [module.security_group.security_group_id]
93-
subnets = data.aws_subnet_ids.all.ids
96+
subnets = data.aws_subnets.all.ids
9497

9598
# # See notes in README (ref: https://github.com/terraform-providers/terraform-provider-aws/issues/7987)
9699
# access_logs = {
@@ -314,6 +317,35 @@ module "alb" {
314317
}]
315318
}]
316319
},
320+
{
321+
http_tcp_listener_index = 0
322+
priority = 4
323+
324+
actions = [{
325+
type = "weighted-forward"
326+
target_groups = [
327+
{
328+
target_group_index = 1
329+
weight = 2
330+
},
331+
{
332+
target_group_index = 0
333+
weight = 1
334+
}
335+
]
336+
stickiness = {
337+
enabled = true
338+
duration = 3600
339+
}
340+
}]
341+
342+
conditions = [{
343+
query_strings = [{
344+
key = "weighted"
345+
value = "true"
346+
}]
347+
}]
348+
},
317349
{
318350
http_tcp_listener_index = 0
319351
priority = 5000
@@ -470,7 +502,7 @@ resource "null_resource" "download_package" {
470502

471503
module "lambda_function" {
472504
source = "terraform-aws-modules/lambda/aws"
473-
version = "~> 2.0"
505+
version = "~> 3.0"
474506

475507
function_name = "${random_pet.this.id}-lambda"
476508
description = "My awesome lambda function"

examples/complete-nlb/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Note that this example may create resources which cost money. Run `terraform des
3434

3535
| Name | Source | Version |
3636
|------|--------|---------|
37-
| <a name="module_acm"></a> [acm](#module\_acm) | terraform-aws-modules/acm/aws | n/a |
37+
| <a name="module_acm"></a> [acm](#module\_acm) | terraform-aws-modules/acm/aws | ~> 3.0 |
3838
| <a name="module_nlb"></a> [nlb](#module\_nlb) | ../../ | n/a |
3939

4040
## Resources
@@ -44,7 +44,7 @@ Note that this example may create resources which cost money. Run `terraform des
4444
| [aws_eip.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eip) | resource |
4545
| [random_pet.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/pet) | resource |
4646
| [aws_route53_zone.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/route53_zone) | data source |
47-
| [aws_subnet_ids.all](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet_ids) | data source |
47+
| [aws_subnets.all](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnets) | data source |
4848
| [aws_vpc.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
4949

5050
## Inputs

examples/complete-nlb/main.tf

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ data "aws_vpc" "default" {
1313
default = true
1414
}
1515

16-
data "aws_subnet_ids" "all" {
17-
vpc_id = data.aws_vpc.default.id
16+
data "aws_subnets" "all" {
17+
filter {
18+
name = "vpc-id"
19+
values = [data.aws_vpc.default.id]
20+
}
1821
}
1922

2023
resource "random_pet" "this" {
@@ -27,6 +30,7 @@ data "aws_route53_zone" "this" {
2730

2831
# module "log_bucket" {
2932
# source = "terraform-aws-modules/s3-bucket/aws"
33+
# version = "~> 3.0"
3034
#
3135
# bucket = "logs-${random_pet.this.id}"
3236
# acl = "log-delivery-write"
@@ -35,14 +39,15 @@ data "aws_route53_zone" "this" {
3539
# }
3640

3741
module "acm" {
38-
source = "terraform-aws-modules/acm/aws"
42+
source = "terraform-aws-modules/acm/aws"
43+
version = "~> 3.0"
3944

40-
domain_name = local.domain_name # trimsuffix(data.aws_route53_zone.this.name, ".") # Terraform >= 0.12.17
45+
domain_name = local.domain_name # trimsuffix(data.aws_route53_zone.this.name, ".")
4146
zone_id = data.aws_route53_zone.this.id
4247
}
4348

4449
resource "aws_eip" "this" {
45-
count = length(data.aws_subnet_ids.all.ids)
50+
count = length(data.aws_subnets.all.ids)
4651

4752
vpc = true
4853
}
@@ -63,7 +68,7 @@ module "nlb" {
6368
# subnets = tolist(data.aws_subnet_ids.all.ids)
6469

6570
# Use `subnet_mapping` to attach EIPs
66-
subnet_mapping = [for i, eip in aws_eip.this : { allocation_id : eip.id, subnet_id : tolist(data.aws_subnet_ids.all.ids)[i] }]
71+
subnet_mapping = [for i, eip in aws_eip.this : { allocation_id : eip.id, subnet_id : tolist(data.aws_subnets.all.ids)[i] }]
6772

6873
# # See notes in README (ref: https://github.com/terraform-providers/terraform-provider-aws/issues/7987)
6974
# access_logs = {

main.tf

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ resource "aws_lb" "this" {
2222
enable_waf_fail_open = var.enable_waf_fail_open
2323
desync_mitigation_mode = var.desync_mitigation_mode
2424

25-
# See notes in README (ref: https://github.com/terraform-providers/terraform-provider-aws/issues/7987)
2625
dynamic "access_logs" {
2726
for_each = length(keys(var.access_logs)) == 0 ? [] : [var.access_logs]
2827

2928
content {
30-
enabled = lookup(access_logs.value, "enabled", lookup(access_logs.value, "bucket", null) != null)
31-
bucket = lookup(access_logs.value, "bucket", null)
32-
prefix = lookup(access_logs.value, "prefix", null)
29+
enabled = try(access_logs.value.enabled, try(access_logs.value.bucket, null) != null)
30+
bucket = try(access_logs.value.bucket, null)
31+
prefix = try(access_logs.value.prefix, null)
3332
}
3433
}
3534

@@ -450,6 +449,37 @@ resource "aws_lb_listener_rule" "http_tcp_listener_rule" {
450449
}
451450
}
452451

452+
# weighted forward actions
453+
dynamic "action" {
454+
for_each = [
455+
for action_rule in var.http_tcp_listener_rules[count.index].actions :
456+
action_rule
457+
if action_rule.type == "weighted-forward"
458+
]
459+
460+
content {
461+
type = "forward"
462+
forward {
463+
dynamic "target_group" {
464+
for_each = action.value["target_groups"]
465+
466+
content {
467+
arn = aws_lb_target_group.main[target_group.value["target_group_index"]].id
468+
weight = target_group.value["weight"]
469+
}
470+
}
471+
dynamic "stickiness" {
472+
for_each = [lookup(action.value, "stickiness", {})]
473+
474+
content {
475+
enabled = try(stickiness.value["enabled"], false)
476+
duration = try(stickiness.value["duration"], 1)
477+
}
478+
}
479+
}
480+
}
481+
}
482+
453483
# Path Pattern condition
454484
dynamic "condition" {
455485
for_each = [

0 commit comments

Comments
 (0)