Skip to content

Commit

Permalink
feat: Added weighted-forward rules for HTTP (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbloquel authored Apr 15, 2022
1 parent fd88b42 commit ba77760
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: /~https://github.com/antonbabenko/pre-commit-terraform
rev: v1.62.3
rev: v1.67.0
hooks:
- id: terraform_fmt
- id: terraform_validate
Expand All @@ -23,7 +23,7 @@ repos:
- '--args=--only=terraform_standard_module_structure'
- '--args=--only=terraform_workspace_remote'
- repo: /~https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
rev: v4.2.0
hooks:
- id: check-merge-conflict
- id: end-of-file-fixer
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ It's recommended you use this module with [terraform-aws-vpc](https://registry.t

## Notes

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 }`.
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 }`.

## Conditional creation

Expand Down
4 changes: 2 additions & 2 deletions examples/complete-alb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Note that this example may create resources which cost money. Run `terraform des
|------|--------|---------|
| <a name="module_acm"></a> [acm](#module\_acm) | terraform-aws-modules/acm/aws | ~> 3.0 |
| <a name="module_alb"></a> [alb](#module\_alb) | ../../ | n/a |
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 2.0 |
| <a name="module_lambda_function"></a> [lambda\_function](#module\_lambda\_function) | terraform-aws-modules/lambda/aws | ~> 3.0 |
| <a name="module_lb_disabled"></a> [lb\_disabled](#module\_lb\_disabled) | ../../ | n/a |
| <a name="module_security_group"></a> [security\_group](#module\_security\_group) | terraform-aws-modules/security-group/aws | ~> 4.0 |

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

## Inputs
Expand Down
58 changes: 45 additions & 13 deletions examples/complete-alb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ data "aws_vpc" "default" {
default = true
}

data "aws_subnet_ids" "all" {
vpc_id = data.aws_vpc.default.id
data "aws_subnets" "all" {
filter {
name = "vpc-id"
values = [data.aws_vpc.default.id]
}
}

resource "random_pet" "this" {
Expand All @@ -38,21 +41,21 @@ module "security_group" {
egress_rules = ["all-all"]
}

# module "log_bucket" {
# source = "terraform-aws-modules/s3-bucket/aws"
# version = "~> 1.0"
#module "log_bucket" {
# source = "terraform-aws-modules/s3-bucket/aws"
# version = "~> 3.0"
#
# bucket = "logs-${random_pet.this.id}"
# acl = "log-delivery-write"
# force_destroy = true
# attach_elb_log_delivery_policy = true
# }
# bucket = "logs-${random_pet.this.id}"
# acl = "log-delivery-write"
# force_destroy = true
# attach_elb_log_delivery_policy = true
#}

module "acm" {
source = "terraform-aws-modules/acm/aws"
version = "~> 3.0"

domain_name = local.domain_name # trimsuffix(data.aws_route53_zone.this.name, ".") # Terraform >= 0.12.17
domain_name = local.domain_name # trimsuffix(data.aws_route53_zone.this.name, ".")
zone_id = data.aws_route53_zone.this.id
}

Expand Down Expand Up @@ -90,7 +93,7 @@ module "alb" {

vpc_id = data.aws_vpc.default.id
security_groups = [module.security_group.security_group_id]
subnets = data.aws_subnet_ids.all.ids
subnets = data.aws_subnets.all.ids

# # See notes in README (ref: /~https://github.com/terraform-providers/terraform-provider-aws/issues/7987)
# access_logs = {
Expand Down Expand Up @@ -314,6 +317,35 @@ module "alb" {
}]
}]
},
{
http_tcp_listener_index = 0
priority = 4

actions = [{
type = "weighted-forward"
target_groups = [
{
target_group_index = 1
weight = 2
},
{
target_group_index = 0
weight = 1
}
]
stickiness = {
enabled = true
duration = 3600
}
}]

conditions = [{
query_strings = [{
key = "weighted"
value = "true"
}]
}]
},
{
http_tcp_listener_index = 0
priority = 5000
Expand Down Expand Up @@ -470,7 +502,7 @@ resource "null_resource" "download_package" {

module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"
version = "~> 2.0"
version = "~> 3.0"

function_name = "${random_pet.this.id}-lambda"
description = "My awesome lambda function"
Expand Down
4 changes: 2 additions & 2 deletions examples/complete-nlb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Note that this example may create resources which cost money. Run `terraform des

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

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

## Inputs
Expand Down
17 changes: 11 additions & 6 deletions examples/complete-nlb/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ data "aws_vpc" "default" {
default = true
}

data "aws_subnet_ids" "all" {
vpc_id = data.aws_vpc.default.id
data "aws_subnets" "all" {
filter {
name = "vpc-id"
values = [data.aws_vpc.default.id]
}
}

resource "random_pet" "this" {
Expand All @@ -27,6 +30,7 @@ data "aws_route53_zone" "this" {

# module "log_bucket" {
# source = "terraform-aws-modules/s3-bucket/aws"
# version = "~> 3.0"
#
# bucket = "logs-${random_pet.this.id}"
# acl = "log-delivery-write"
Expand All @@ -35,14 +39,15 @@ data "aws_route53_zone" "this" {
# }

module "acm" {
source = "terraform-aws-modules/acm/aws"
source = "terraform-aws-modules/acm/aws"
version = "~> 3.0"

domain_name = local.domain_name # trimsuffix(data.aws_route53_zone.this.name, ".") # Terraform >= 0.12.17
domain_name = local.domain_name # trimsuffix(data.aws_route53_zone.this.name, ".")
zone_id = data.aws_route53_zone.this.id
}

resource "aws_eip" "this" {
count = length(data.aws_subnet_ids.all.ids)
count = length(data.aws_subnets.all.ids)

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

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

# # See notes in README (ref: /~https://github.com/terraform-providers/terraform-provider-aws/issues/7987)
# access_logs = {
Expand Down
38 changes: 34 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ resource "aws_lb" "this" {
enable_waf_fail_open = var.enable_waf_fail_open
desync_mitigation_mode = var.desync_mitigation_mode

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

content {
enabled = lookup(access_logs.value, "enabled", lookup(access_logs.value, "bucket", null) != null)
bucket = lookup(access_logs.value, "bucket", null)
prefix = lookup(access_logs.value, "prefix", null)
enabled = try(access_logs.value.enabled, try(access_logs.value.bucket, null) != null)
bucket = try(access_logs.value.bucket, null)
prefix = try(access_logs.value.prefix, null)
}
}

Expand Down Expand Up @@ -450,6 +449,37 @@ resource "aws_lb_listener_rule" "http_tcp_listener_rule" {
}
}

# weighted forward actions
dynamic "action" {
for_each = [
for action_rule in var.http_tcp_listener_rules[count.index].actions :
action_rule
if action_rule.type == "weighted-forward"
]

content {
type = "forward"
forward {
dynamic "target_group" {
for_each = action.value["target_groups"]

content {
arn = aws_lb_target_group.main[target_group.value["target_group_index"]].id
weight = target_group.value["weight"]
}
}
dynamic "stickiness" {
for_each = [lookup(action.value, "stickiness", {})]

content {
enabled = try(stickiness.value["enabled"], false)
duration = try(stickiness.value["duration"], 1)
}
}
}
}
}

# Path Pattern condition
dynamic "condition" {
for_each = [
Expand Down

0 comments on commit ba77760

Please sign in to comment.