From e9de9a1a548d65c297fda66a7ed14b38efbe3458 Mon Sep 17 00:00:00 2001 From: Bryan Recinos Date: Fri, 7 Jan 2022 16:02:42 -0600 Subject: [PATCH] [v1.0.0] initial module implementation --- .gitignore | 4 +++ README.md | 58 +++++++++++++++++++++++++++++++++++++++-- main.tf | 32 +++++++++++++++++++++++ outputs.tf | 4 +++ variables.tf | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ versions.tf | 10 +++++++ 6 files changed, 179 insertions(+), 2 deletions(-) create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 variables.tf create mode 100644 versions.tf diff --git a/.gitignore b/.gitignore index 7a3e2fd..f9577dd 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,7 @@ override.tf.json # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan # example: *tfplan* + +.DS_Store + +.terraform.lock.hcl \ No newline at end of file diff --git a/README.md b/README.md index ee6473a..4104e33 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,56 @@ -# terraform-aws-alb-target-group -Terraform AWS Load balancer Target group +# Terraform AWS Load Balancer Target group module + +## Basic Usage Example +``` +module "target_group" { + source = "github.com/bryan-rhm/terraform-aws-alb-target-group?ref=v1.0.0" + name = "my-target" + vpc_id = var.vpc_id + target_type = "ip" + stickiness = { + type = "lb_cookie" + } +} +``` +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.0.4 | +| [aws](#requirement\_aws) | >= 3.43.0 | + +## Providers + +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | 3.71.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 | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [health\_check](#input\_health\_check) | Health Check configuration block. |
object({
path = string
timeout = number
matcher = optional(string)
interval = number
protocol = optional(string)
healthy_threshold = optional(number)
unhealthy_threshold = optional(number)
})
| `null` | no | +| [name](#input\_name) | (Optional, Forces new resource) Name of the target group. If omitted, Terraform will assign a random, unique name. | `string` | `null` | no | +| [name\_prefix](#input\_name\_prefix) | (Optional, Forces new resource) Creates a unique name beginning with the specified prefix. Conflicts with name. Cannot be longer than 6 characters. | `string` | `null` | no | +| [port](#input\_port) | Port on which targets receive traffic, unless overridden when registering a specific target. | `number` | `80` | no | +| [protocol](#input\_protocol) | Should be one of GENEVE, HTTP, HTTPS, TCP, TCP\_UDP, TLS, or UDP | `string` | `"HTTP"` | no | +| [stickiness](#input\_stickiness) | Stickiness configuration block. |
object({
type = string # The only current possible values are lb_cookie, app_cookie for ALBs, and source_ip for NLBs.
cookie_name = optional(string) # Only needed when type is app_cookie.
cookie_duration = optional(number) # Only used when the type is lb_cookie. Default value 86400
})
| `null` | no | +| [tags](#input\_tags) | A map of tags to assign to the target group. If configured with a provider default\_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level. | `map(any)` | `null` | no | +| [target\_type](#input\_target\_type) | Type of target that you must specify when registering targets with this target group. Valid values: ip\|instance\|lambda | `string` | `"instance"` | no | +| [vpc\_id](#input\_vpc\_id) | Identifier of the VPC in which to create the target group. Required when target\_type is instance or ip | `string` | `null` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| [output](#output\_output) | Target group attributes | diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..74eabbe --- /dev/null +++ b/main.tf @@ -0,0 +1,32 @@ +resource "aws_lb_target_group" "this" { + name = var.name + name_prefix = substr(var.name_prefix,0,6) + port = var.port + protocol = var.protocol + vpc_id = var.vpc_id + target_type = var.target_type + + dynamic "health_check" { + for_each = var.health_check != null ? [1] : [] + content { + path = try(var.health_check.path, null) + timeout = var.health_check.timeout + matcher = try(var.health_check.matcher, null) + interval = var.health_check.interval + protocol = try(var.health_check.protocol, null) + healthy_threshold = try(var.health_check.healthy_threshold, null) + unhealthy_threshold = try(var.health_check.unhealthy_threshold, null) + } + } + + dynamic "stickiness" { + for_each = var.stickiness != null ? [1] : [] + content { + type = var.stickiness.type + cookie_name = try(var.stickiness.cookie_name, null) + cookie_duration = try(var.stickiness.cookie_duration, null) + } + } + + tags = var.tags +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..7a5463b --- /dev/null +++ b/outputs.tf @@ -0,0 +1,4 @@ +output "output" { + description = "Target group attributes" + value = aws_lb_target_group.this +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..9d2df7a --- /dev/null +++ b/variables.tf @@ -0,0 +1,73 @@ +variable "name" { + description = "(Optional, Forces new resource) Name of the target group. If omitted, Terraform will assign a random, unique name." + default = null + type = string +} + +variable "name_prefix" { + description = "(Optional, Forces new resource) Creates a unique name beginning with the specified prefix. Conflicts with name. Cannot be longer than 6 characters." + default = null + type = string +} + +variable "port" { + description = "Port on which targets receive traffic, unless overridden when registering a specific target." + default = 80 + type = number +} + +variable "protocol" { + description = "Should be one of GENEVE, HTTP, HTTPS, TCP, TCP_UDP, TLS, or UDP" + default = "HTTP" + type = string +} + +variable "vpc_id" { + description = "Identifier of the VPC in which to create the target group. Required when target_type is instance or ip" + default = null + type = string +} + +variable "target_type" { + description = "Type of target that you must specify when registering targets with this target group. Valid values: ip|instance|lambda" + default = "instance" + type = string +} + +variable "health_check" { + description = "Health Check configuration block." + default = null + type = object({ + path = string + timeout = number + matcher = optional(string) + interval = number + protocol = optional(string) + healthy_threshold = optional(number) + unhealthy_threshold = optional(number) + }) + # Example: + # path = "/" + # timeout = 10 + # matcher = "200-399" + # interval = 15 + # protocol = "HTTP" + # healthy_threshold = 2 + # unhealthy_threshold = 2 +} + +variable "stickiness" { + description = "Stickiness configuration block." + default = null + type = object({ + type = string # The only current possible values are lb_cookie, app_cookie for ALBs, and source_ip for NLBs. + cookie_name = optional(string) # Only needed when type is app_cookie. + cookie_duration = optional(number) # Only used when the type is lb_cookie. Default value 86400 + }) +} + +variable "tags" { + type = map(any) + default = null + description = "A map of tags to assign to the target group. If configured with a provider default_tags configuration block present, tags with matching keys will overwrite those defined at the provider-level." +} diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..0e7187b --- /dev/null +++ b/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 1.0.4" + experiments = [module_variable_optional_attrs] + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 3.43.0" + } + } +}