Skip to content

Commit

Permalink
apply id based module
Browse files Browse the repository at this point in the history
  • Loading branch information
Sungmin Lee authored and Sungmin Lee committed Nov 8, 2022
1 parent 6095aaa commit b4954d8
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 82 deletions.
139 changes: 65 additions & 74 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,55 @@
# Multiple Load Balancer Module

This document describes the Terraform module that creates multiple Ncloud Load Balancers.

Before use `Load Balancer module`, you need create `VPC module`, `Server module` and `Target Group module`.

- [VPC module](https://registry.terraform.io/modules/terraform-ncloud-modules/vpc/ncloud/latest)
- [Server module](https://registry.terraform.io/modules/terraform-ncloud-modules/server/ncloud/latest)
- [Target Group module](https://registry.terraform.io/modules/terraform-ncloud-modules/target-group/ncloud/latest)
## **This version of the module requires Terraform version 1.3.0 or later.**

This document describes the Terraform module that creates multiple Ncloud Load Balancers.

## Variable Declaration

### `variable.tf`
### Structure : `variable.tf`

You need to create `variable.tf` and declare the VPC variable to recognize VPC variable in `terraform.tfvars`. You can change the variable name to whatever you want.
You need to create `variable.tf` and copy & paste the variable declaration below.

**You can change the variable name to whatever you want.**

``` hcl
variable "load_balancers" { default = [] }
variable "load_balancers" {
type = list(object({
name = string
description = optional(string, "")
type = string // NETWORK | NETWORK_PROXY | APPLICATION
network_type = optional(string, "PUBLIC") // PUBLIC (default) | PRIVATE
vpc_name = string
subnet_names = list(string)
throughput_type = optional(string, "SMALL") // SMALL (default) | MEDUIM | LARGE
// Only SMALL can be selected when type is NETWORK and network_type is PRIVATE
idle_timeout = optional(number, 60) // 60 (default)
listeners = optional(list(object({
protocol = string // TCP (when type is NETWORK), TCP/TLS (when type is NETWORK_PROXY), HTTP/HTTPS (when type is APPLICATION)
port = number
target_group_name = string
// The properties below are valid only when the listener protocol is HTTPS or TLS.
ssl_certificate_no = optional(string, null)
tls_min_version_type = optional(string, "TLSV10") // TLSV10 (default) | TLSV11 | TLSV12
// The property below are valid only when the listener protocol is HTTPS
use_http2 = optional(bool, false) // false (default)
})), [])
}))
default = []
}
```

### `terraform.tfvars`
### Example : `terraform.tfvars`

You can create `terraform.tfvars` and refer to the sample below to write variable declarations.
You can create a `terraform.tfvars` and refer to the sample below to write the variable specification you want.
File name can be `terraform.tfvars` or anything ending in `.auto.tfvars`

#### Structure

``` hcl
load_balancers = [
{
name = string // (Required)
description = string // (Required)
type = string // (Required) NETWORK | NETWORK_PROXY | APPLICATION
network_type = string // (Optional) PUBLIC (default) | PRIVATE
vpc_name = string // (Required)
subnet_names = [string] // (Required)
throughput_type = string // (Optional) SMALL (default) | MEDUIM | LARGE
// Only SMALL can be selected when type is NETWORK and network_type is PRIVATE
idle_timeout = number // (Optional) 60 (default)
listeners = [
{
protocol = string // (Required) TCP | TLS | HTTP | HTTPS
// TCP when type is NETWORK, TCP/TLS when type is NETWORK_PROXY
// HTTP/HTTPS when type is APPLICATION
port = number // (Required)
target_group_name = string // (Required)
ssl_certificate_no = string // (Required if listener protocol is HTTPS or TLS)
tls_min_version_type = string // (Optional if listener protocol is HTTPS or TLS) TLSV10 (default) | TLSV11 | TLSV12
use_http2 = bool // (Optional if listener protocol is HTTPS or TLS) false (default)
}
]
}
]
```

#### Example
**It must exactly match the variable name above.**

``` hcl
load_balancers = [
Expand Down Expand Up @@ -85,8 +77,6 @@ load_balancers = [
port = 443
target_group_name = "tg-foo-proxy-tcp"
ssl_certificate_no = "7589"
tls_min_version_type = "TLSV12"
use_http2 = true
}
]
},
Expand All @@ -108,10 +98,12 @@ load_balancers = [
target_group_name = "tg-foo-http"
},
{
protocol = "HTTPS"
port = 443
target_group_name = "tg-foo-https"
ssl_certificate_no = "7589"
protocol = "HTTPS"
port = 443
target_group_name = "tg-foo-https"
ssl_certificate_no = "7589"
tls_min_version_type = "TLSV12"
use_http2 = true
}
]
},
Expand Down Expand Up @@ -140,13 +132,9 @@ load_balancers = [

Map your `Load Balancer variable name` to a `local Load Balancer variable`. `Load Balancer module` are created using `local Load Balancer variables`. This eliminates the need to change the variable name reference structure in the `Load Balancer module`.

Also, the `Load Balancer module` is designed to be used with `VPC module`, and `Target Group module` together. So the `VPC module`, and `Target Group module` must also be specified as `local VPC module variable` and `local Target Group module variable`.

``` hcl
locals {
load_balancers = var.load_balancers
module_vpcs = module.vpcs
module_target_groups = module.target_groups
}
```

Expand All @@ -159,24 +147,27 @@ module "load_balancers" {
for_each = { for lb in local.load_balancers : lb.name => lb }
name = each.value.name
description = lookup(each.value, "description", "")
type = each.value.type
network_type = lookup(each.value, "network_type", "PUBLIC")
subnet_no_list = [for subnet_name in each.value.subnet_names : local.module_vpcs[each.value.vpc_name].subnets[subnet_name].id]
throughput_type = lookup(each.value, "throughput_type", "SMALL")
idle_timeout = lookup(each.value, "idle_timeout", 60)
listeners = [for listener in each.value.listeners : {
protocol = listener.protocol
port = listener.port
target_group_no = local.module_target_groups[listener.target_group_name].target_group.id
ssl_certificate_no = lookup(listener, "ssl_certificate_no", null)
tls_min_version_type = ((listener.protocol == "TLS") || (listener.protocol == "HTTPS") ? lookup(listener, "tls_min_version_type", "TLSV10") : null)
use_http2 = listener.protocol == "HTTPS" ? lookup(listener, "use_http2", false) : null
}]
name = each.value.name
description = each.value.description
type = each.value.type
network_type = each.value.network_type
// you can use "vpc_name" & "subnet_name". Then module will find "subnet_id" from "DataSource: ncloud_subnet".
vpc_name = each.value.vpc_name
subnet_names = each.value.subnet_names1
// or "subnet_id" instead
# subnet_ids = [ for subnet_name in each.value.subnet_names : module.vpcs[each.value.vpc_name].subnets[subnet_name].id ]
throughput_type = each.value.throughput_type
idle_timeout = each.value.idle_timeout
// you can use "listeners" with "target_group_name" as object attribute.
listeners = each.value.listeners
// or "listeners" with "target_group_id" instead.
# listeners = [for listener in each.value.listeners : merge(
# { for k, v in listener : k => v if k != "target_group_name" },
# { target_group_id = module.target_groups[listener.target_group_name].target_group.id }
# )]
}
Expand Down
52 changes: 44 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@

data "ncloud_vpc" "vpc" {
count = var.vpc_name != null ? 1 : 0

filter {
name = "name"
values = [var.vpc_name]
}
}

data "ncloud_subnet" "subnets" {
for_each = toset(coalesce(var.subnet_names, []))

vpc_no = one(data.ncloud_vpc.vpc.*.id)
filter {
name = "name"
values = [each.key]
}
}

resource "ncloud_lb" "lb" {
name = var.name
description = var.description
type = var.type
network_type = var.network_type
subnet_no_list = var.subnet_no_list
subnet_no_list = coalesce(var.subnet_no_list, coalesce(var.subnet_ids, values(data.ncloud_subnet.subnets).*.id))
throughput_type = var.throughput_type
idle_timeout = var.idle_timeout
}


data "ncloud_lb_target_group" "target_groups" {
for_each = toset([for listener in var.listeners : listener.target_group_name if can(listener.target_group_name)])

filter {
name = "name"
values = [each.key]
}
}


resource "ncloud_lb_listener" "lb_listeners" {
count = length(var.listeners)

load_balancer_no = ncloud_lb.lb.id
protocol = var.listeners[count.index].protocol
port = var.listeners[count.index].port
target_group_no = var.listeners[count.index].target_group_no
tls_min_version_type = var.listeners[count.index].tls_min_version_type
ssl_certificate_no = var.listeners[count.index].ssl_certificate_no
use_http2 = var.listeners[count.index].use_http2
load_balancer_no = ncloud_lb.lb.id
protocol = var.listeners[count.index].protocol
port = var.listeners[count.index].port
target_group_no = (
try(var.listeners[count.index].target_group_no,
try(var.listeners[count.index].target_group_id,
data.ncloud_lb_target_group.target_groups[var.listeners[count.index].target_group_name].id
)))

tls_min_version_type = (var.listeners[count.index].protocol == "TLS") || (var.listeners[count.index].protocol == "HTTPS") ? var.listeners[count.index].tls_min_version_type : null
ssl_certificate_no = (var.listeners[count.index].protocol == "TLS") || (var.listeners[count.index].protocol == "HTTPS") ? var.listeners[count.index].ssl_certificate_no : null
use_http2 = (var.listeners[count.index].protocol == "HTTPS") ? var.listeners[count.index].use_http2 : null
}
20 changes: 20 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,29 @@ variable "network_type" {
default = "PUBLIC"
}

variable "vpc_name" {
description = "(Required) See the description in the readme"
type = string
default = null
}

variable "subnet_names" {
description = "(Required) See the description in the readme"
type = list(any)
default = null
}


variable "subnet_no_list" {
description = "(Required) See the description in the readme"
type = list(any)
default = null
}

variable "subnet_ids" {
description = "(Required) See the description in the readme. Same with subnet_no_list"
type = list(any)
default = null
}

variable "throughput_type" {
Expand Down

0 comments on commit b4954d8

Please sign in to comment.