Skip to content
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

Add alb-instance-target-group and alb-ip-target-group module #24

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions modules/alb-instance-target-group/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# alb-instance-target-group

This module creates following resources.

- `aws_lb_target_group`
- `aws_lb_target_group_attachment` (optional)

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 3.71 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.1.0 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_lb_target_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group) | resource |
| [aws_lb_target_group_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group_attachment) | resource |
| [aws_resourcegroups_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/resourcegroups_group) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_name"></a> [name](#input\_name) | (Required) Name of the target group. A maximum of 32 alphanumeric characters including hyphens are allowed, but the name must not begin or end with a hyphen. | `string` | n/a | yes |
| <a name="input_port"></a> [port](#input\_port) | (Required) The number of port on which targets receive traffic, unless overridden when registering a specific target. Valid values are either ports 1-65535. | `number` | n/a | yes |
| <a name="input_protocol"></a> [protocol](#input\_protocol) | (Required) The protocol to use for routing traffic to the targets. Valid values are `HTTP` and `HTTPS`. Defaults to `HTTP`. | `string` | n/a | yes |
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | (Required) The ID of the VPC which the target group belongs to. | `string` | n/a | yes |
| <a name="input_deregistration_delay"></a> [deregistration\_delay](#input\_deregistration\_delay) | (Optional) The time to wait for in-flight requests to complete while deregistering a target. During this time, the state of the target is draining. | `number` | `300` | no |
| <a name="input_health_check"></a> [health\_check](#input\_health\_check) | (Optional) Health Check configuration block. The associated load balancer periodically sends requests to the registered targets to test their status. `health_check` block as defined below.<br> (Optional) `port` - The port the load balancer uses when performing health checks on targets. The default is the port on which each target receives traffic from the load balancer. Valid values are either ports 1-65535.<br> (Optional) `protocol` - Protocol to use to connect with the target. The possible values are `HTTP` and `HTTPS`. Defaults to `HTTP`.<br> (Optional) `healthy_threshold` - The number of consecutive health checks successes required before considering an unhealthy target healthy. Valid value range is 2 - 10. Defaults to `5`.<br> (Optional) `unhealthy_threshold` - The number of consecutive health check failures required before considering a target unhealthy. Valid value range is 2 - 10. Defaults to `2`.<br> (Optional) `interval` - Approximate amount of time, in seconds, between health checks of an individual target. Valid value range is 5 - 300. Defaults to `30`.<br> (Optional) `timeout` - The amount of time, in seconds, during which no response means a failed health check. Valid value range is 2 - 120. Defaults to `5`.<br> (Optional) `path` - Use the default path of `/` to ping the root, or specify a custom path if preferred. Only valid if the `protocol` is `HTTP` or `HTTPS`. | `any` | `{}` | no |
| <a name="input_load_balancing_algorithm"></a> [load\_balancing\_algorithm](#input\_load\_balancing\_algorithm) | (Optional) Determines how the load balancer selects targets when routing requests. Valid values are `ROUND_ROBIN` or `LEAST_OUTSTANDING_REQUESTS`. Defaults to `ROUND_ROBIN`. | `string` | `"ROUND_ROBIN"` | no |
| <a name="input_module_tags_enabled"></a> [module\_tags\_enabled](#input\_module\_tags\_enabled) | (Optional) Whether to create AWS Resource Tags for the module informations. | `bool` | `true` | no |
| <a name="input_protocol_version"></a> [protocol\_version](#input\_protocol\_version) | (Optional) Use `HTTP1` to send requests to targets using HTTP/1.1. Supported when the request protocol is HTTP/1.1 or HTTP/2. Use `HTTP2` to send requests to targets using HTTP/2. Supported when the request protocol is HTTP/2 or gRPC, but gRPC-specific features are not available. Use `GRPC` to send requests to targets using gRPC. Supported when the request protocol is gRPC. Defaults to `HTTP1`. | `string` | `"HTTP1"` | no |
| <a name="input_resource_group_description"></a> [resource\_group\_description](#input\_resource\_group\_description) | (Optional) The description of Resource Group. | `string` | `"Managed by Terraform."` | no |
| <a name="input_resource_group_enabled"></a> [resource\_group\_enabled](#input\_resource\_group\_enabled) | (Optional) Whether to create Resource Group to find and group AWS resources which are created by this module. | `bool` | `true` | no |
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | (Optional) The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. | `string` | `""` | no |
| <a name="input_slow_start_duration"></a> [slow\_start\_duration](#input\_slow\_start\_duration) | (Optional) The amount time for a newly registered targets to warm up before the load balancer sends them a full share of requests. During this period, targets receives an increasing share of requests until it reaches its fair share. Requires `30` to `900` seconds to enable, or `0` seconds to disable. This attribute cannot be combined with the Least outstanding requests algorithm. | `number` | `0` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | (Optional) A map of tags to add to all resources. | `map(string)` | `{}` | no |
| <a name="input_targets"></a> [targets](#input\_targets) | (Optional) A set of targets to add to the target group. Each value of `targets` block as defined below.<br> (Required) `instance` - This is the Instance ID for an instance, or the container ID for an ECS container.<br> (Optional) `port` - The port on which targets receive traffic. | `set(map(string))` | `[]` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_arn"></a> [arn](#output\_arn) | The Amazon Resource Name (ARN) of the target group. |
| <a name="output_arn_suffix"></a> [arn\_suffix](#output\_arn\_suffix) | The ARN suffix for use with CloudWatch Metrics. |
| <a name="output_attributes"></a> [attributes](#output\_attributes) | Attributes of the Instance target group of network load balancer. |
| <a name="output_health_check"></a> [health\_check](#output\_health\_check) | Health Check configuration of the target group. |
| <a name="output_id"></a> [id](#output\_id) | The ID of the target group. |
| <a name="output_name"></a> [name](#output\_name) | The name of the target group. |
| <a name="output_port"></a> [port](#output\_port) | The port number on which the target receive trrafic. |
| <a name="output_protocol"></a> [protocol](#output\_protocol) | The protocol to use to connect with the target. |
| <a name="output_protocol_version"></a> [protocol\_version](#output\_protocol\_version) | The protocol version to use to send requests to targets. |
| <a name="output_targets"></a> [targets](#output\_targets) | A set of targets in the target group. |
| <a name="output_type"></a> [type](#output\_type) | The target type of the target group. |
| <a name="output_vpc_id"></a> [vpc\_id](#output\_vpc\_id) | The ID of the VPC which the target group belongs to. |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
84 changes: 84 additions & 0 deletions modules/alb-instance-target-group/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
locals {
metadata = {
package = "terraform-aws-load-balancer"
version = trimspace(file("${path.module}/../../VERSION"))
module = basename(path.module)
name = var.name
}
module_tags = var.module_tags_enabled ? {
"module.terraform.io/package" = local.metadata.package
"module.terraform.io/version" = local.metadata.version
"module.terraform.io/name" = local.metadata.module
"module.terraform.io/full-name" = "${local.metadata.package}/${local.metadata.module}"
"module.terraform.io/instance" = local.metadata.name
} : {}
}


# INFO: Not supported attributes
# - `connection_termination`
# - `lambda_multi_value_headers_enabled`
# - `preserve_client_ip`
# - `proxy_protocol_v2`
resource "aws_lb_target_group" "this" {
name = var.name

vpc_id = var.vpc_id

target_type = "instance"
port = var.port
protocol = var.protocol
protocol_version = var.protocol_version

## Attributes
deregistration_delay = var.deregistration_delay
load_balancing_algorithm_type = lower(var.load_balancing_algorithm)
slow_start = var.slow_start_duration
# - `stickiness`

health_check {
enabled = true

port = try(var.health_check.port, var.port)
protocol = try(var.health_check.protocol, "HTTP")

healthy_threshold = try(var.health_check.healthy_threshold, 5)
unhealthy_threshold = try(var.health_check.unhealthy_threshold, 2)
interval = try(var.health_check.interval, 30)
timeout = try(var.health_check.timeout, 5)

matcher = (var.protocol_version != "GRPC"
? try(var.health_check.success_codes, "200")
: try(var.health_check.success_codes, "12"))
path = (var.protocol_version != "GRPC"
? try(var.health_check.path, "/")
: try(var.health_check.path, "/AWS.ALB/healthcheck"))
}

tags = merge(
{
"Name" = local.metadata.name
},
local.module_tags,
var.tags,
)
}


###################################################
# Attachment for ALB Instance Target Group
###################################################

# INFO: Not supported attributes
# - `availability_zone`
resource "aws_lb_target_group_attachment" "this" {
for_each = {
for target in var.targets :
target.instance => target
}

target_group_arn = aws_lb_target_group.this.arn

target_id = each.key
port = try(each.value.port, var.port)
}
80 changes: 80 additions & 0 deletions modules/alb-instance-target-group/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
output "arn" {
description = "The Amazon Resource Name (ARN) of the target group."
value = aws_lb_target_group.this.arn
}

output "arn_suffix" {
description = "The ARN suffix for use with CloudWatch Metrics."
value = aws_lb_target_group.this.arn_suffix
}

output "id" {
description = "The ID of the target group."
value = aws_lb_target_group.this.id
}

output "name" {
description = "The name of the target group."
value = aws_lb_target_group.this.name
}

output "vpc_id" {
description = "The ID of the VPC which the target group belongs to."
value = aws_lb_target_group.this.vpc_id
}

output "type" {
description = "The target type of the target group."
value = upper(aws_lb_target_group.this.target_type)
}

output "port" {
description = "The port number on which the target receive trrafic."
value = aws_lb_target_group.this.port
}

output "protocol" {
description = "The protocol to use to connect with the target."
value = aws_lb_target_group.this.protocol
}

output "protocol_version" {
description = "The protocol version to use to send requests to targets."
value = aws_lb_target_group.this.protocol_version
}

output "targets" {
description = "A set of targets in the target group."
value = [
for target in aws_lb_target_group_attachment.this : {
instance = target.target_id
port = target.port
}
]
}

output "attributes" {
description = "Attributes of the Instance target group of network load balancer."
value = {
deregistration_delay = aws_lb_target_group.this.deregistration_delay
load_balancing_algorithm = upper(aws_lb_target_group.this.load_balancing_algorithm_type)
slow_start_duration = aws_lb_target_group.this.slow_start
stickiness = aws_lb_target_group.this.stickiness[0]
}
}

output "health_check" {
description = "Health Check configuration of the target group."
value = {
protocol = aws_lb_target_group.this.health_check[0].protocol
port = aws_lb_target_group.this.health_check[0].port

healthy_threshold = aws_lb_target_group.this.health_check[0].healthy_threshold
unhealthy_threshold = aws_lb_target_group.this.health_check[0].unhealthy_threshold
interval = aws_lb_target_group.this.health_check[0].interval
timeout = aws_lb_target_group.this.health_check[0].timeout

success_codes = aws_lb_target_group.this.health_check[0].matcher
path = aws_lb_target_group.this.health_check[0].path
}
}
44 changes: 44 additions & 0 deletions modules/alb-instance-target-group/resource-group.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
locals {
resource_group_name = (var.resource_group_name != ""
? var.resource_group_name
: join(".", [
local.metadata.package,
local.metadata.module,
replace(local.metadata.name, "/[^a-zA-Z0-9_\\.-]/", "-"),
])
)
resource_group_filters = [
for key, value in local.module_tags : {
"Key" = key
"Values" = [value]
}
]
resource_group_query = <<-JSON
{
"ResourceTypeFilters": [
"AWS::AllSupported"
],
"TagFilters": ${jsonencode(local.resource_group_filters)}
}
JSON
}

resource "aws_resourcegroups_group" "this" {
count = (var.resource_group_enabled && var.module_tags_enabled) ? 1 : 0

name = local.resource_group_name
description = var.resource_group_description

resource_query {
type = "TAG_FILTERS_1_0"
query = local.resource_group_query
}

tags = merge(
{
"Name" = local.resource_group_name
},
local.module_tags,
var.tags,
)
}
Loading