Skip to content

Instantly share code, notes, and snippets.

@brandonjbjelland
Created March 23, 2018 03:41
Show Gist options
  • Save brandonjbjelland/55645a308af1cf3cc3b9207926aeb897 to your computer and use it in GitHub Desktop.
Save brandonjbjelland/55645a308af1cf3cc3b9207926aeb897 to your computer and use it in GitHub Desktop.
alb snippets
resource "aws_lb_target_group" "main" {
name = "${lookup(var.target_groups[count.index], "name")}"
vpc_id = "${var.vpc_id}"
port = "${lookup(var.target_groups[count.index], "backend_port")}"
protocol = "${upper(lookup(var.target_groups[count.index], "backend_protocol"))}"
deregistration_delay = "${lookup(var.target_groups[count.index], "deregistration_delay", lookup(var.target_groups_defaults, "deregistration_delay"))}"
target_type = "${lookup(var.target_groups[count.index], "target_type", lookup(var.target_groups_defaults, "target_type"))}"
health_check {
interval = "${lookup(var.target_groups[count.index], "health_check_interval", lookup(var.target_groups_defaults, "health_check_interval"))}"
path = "${lookup(var.target_groups[count.index], "health_check_path", lookup(var.target_groups_defaults, "health_check_path"))}"
port = "${lookup(var.target_groups[count.index], "health_check_port", lookup(var.target_groups_defaults, "health_check_port"))}"
healthy_threshold = "${lookup(var.target_groups[count.index], "health_check_healthy_threshold", lookup(var.target_groups_defaults, "health_check_healthy_threshold"))}"
unhealthy_threshold = "${lookup(var.target_groups[count.index], "health_check_unhealthy_threshold", lookup(var.target_groups_defaults, "health_check_unhealthy_threshold"))}"
timeout = "${lookup(var.target_groups[count.index], "health_check_timeout", lookup(var.target_groups_defaults, "health_check_timeout"))}"
protocol = "${upper(lookup(var.target_groups[count.index], "healthcheck_protocol", lookup(var.target_groups[count.index], "backend_protocol")))}"
matcher = "${lookup(var.target_groups[count.index], "health_check_matcher", lookup(var.target_groups_defaults, "health_check_matcher"))}"
}
stickiness {
type = "lb_cookie"
cookie_duration = "${lookup(var.target_groups[count.index], "cookie_duration", lookup(var.target_groups_defaults, "cookie_duration"))}"
enabled = "${lookup(var.target_groups[count.index], "stickiness_enabled", lookup(var.target_groups_defaults, "stickiness_enabled"))}"
}
tags = "${merge(var.tags, map("Name", lookup(var.target_groups[count.index], "name")))}"
count = "${var.target_groups_count}"
depends_on = ["aws_lb.application"]
}
locals {
target_groups_count = 2
target_groups = "${list(
map("name", "foo",
"backend_protocol", "HTTP",
"backend_port", 80,
),
map("name", "bar",
"backend_protocol", "HTTP",
"backend_port", 8080,
),
)}
}
module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "3.1.0"
load_balancer_name = "test-alb-${random_string.suffix.result}"
security_groups = ["${module.security_group.this_security_group_id}"]
log_bucket_name = "${aws_s3_bucket.log_bucket.id}"
log_location_prefix = "${var.log_location_prefix}"
subnets = "${module.vpc.public_subnets}"
tags = "${local.tags}"
vpc_id = "${module.vpc.vpc_id}"
https_listeners = "${local.https_listeners}"
https_listeners_count = "${local.https_listeners_count}"
http_tcp_listeners = "${local.http_tcp_listeners}"
http_tcp_listeners_count = "${local.http_tcp_listeners_count}"
target_groups = "${local.target_groups}"
target_groups_count = "${local.target_groups_count}"
extra_ssl_certs = "${local.extra_ssl_certs}"
extra_ssl_certs_count = "${local.extra_ssl_certs_count}"
}
variable "target_groups" {
description = "A list of maps containing key/value pairs that define the target groups to be created. Order of these maps is important and the index of these are to be referenced in listener definitions. Required key/values: name, backend_protocol, backend_port. Optional key/values are in the target_groups_defaults variable."
type = "list"
default = []
}
variable "target_groups_count" {
description = "A manually provided count/length of the target_groups list of maps since the list cannot be computed."
default = 0
}
variable "target_groups_defaults" {
description = "Default values for target groups as defined by the list of maps."
type = "map"
default = {
"cookie_duration" = 86400
"deregistration_delay" = 300
"health_check_interval" = 10
"health_check_healthy_threshold" = 3
"health_check_path" = "/"
"health_check_port" = "traffic-port"
"health_check_timeout" = 5
"health_check_unhealthy_threshold" = 3
"health_check_matcher" = "200-299"
"stickiness_enabled" = true
"target_type" = "instance"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment