Created
November 18, 2019 16:10
-
-
Save akomic/8475cf4f73c56f87f4cbcb414d74e126 to your computer and use it in GitHub Desktop.
Terraform 0.12 AWS Network LB aws_lb_target_group_attachment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"inputs": { | |
"gelf-dev": { | |
"name": "GELF TCP_UDP DEV", | |
"proto": "TCP_UDP", | |
"port": 12205 | |
}, | |
"gelf-prod": { | |
"name": "GELF TCP_UDP PROD", | |
"proto": "TCP_UDP", | |
"port": 12206 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resource "aws_lb" "inputs" { | |
name = "tf-${var.Environment}-${var.instance_name}-inputs" | |
internal = false | |
load_balancer_type = "network" | |
subnets = [var.subnet_id] | |
tags = { | |
Environment = var.Environment | |
Terraform = "true" | |
propagate_at_launch = "true" | |
} | |
} | |
# INPUTS | |
resource "aws_lb_listener" "input" { | |
for_each = var.inputs | |
load_balancer_arn = aws_lb.inputs.arn | |
port = each.value.port | |
protocol = each.value.proto | |
default_action { | |
type = "forward" | |
target_group_arn = aws_lb_target_group.input[each.key].arn | |
} | |
depends_on = [aws_lb_target_group.input] | |
} | |
resource "aws_lb_target_group" "input" { | |
for_each = var.inputs | |
name = "${var.Environment}-${var.instance_name}-${each.key}" | |
port = each.value.port | |
protocol = each.value.proto | |
vpc_id = var.vpc_id | |
target_type = "instance" | |
health_check { | |
healthy_threshold = 2 | |
unhealthy_threshold = 2 | |
interval = 30 | |
port = 80 | |
protocol = "TCP" | |
} | |
tags = { | |
Environment = var.Environment | |
Terraform = "true" | |
} | |
} | |
resource "aws_lb_target_group_attachment" "input" { | |
for_each = var.inputs | |
target_group_arn = aws_lb_target_group.input[each.key].arn | |
port = each.value.port | |
target_id = aws_instance.server_instance.id | |
depends_on = [aws_lb_target_group.input] | |
} | |
locals { | |
targets = setproduct(keys(var.inputs), aws_instance.slave_instance[*].id) | |
} | |
resource "aws_lb_target_group_attachment" "input_slaves" { | |
count = length(local.targets) | |
target_group_arn = aws_lb_target_group.input[local.targets[count.index][0]].arn | |
port = var.inputs[local.targets[count.index][0]].port | |
target_id = local.targets[count.index][1] | |
depends_on = [aws_lb_target_group.input] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Master server | |
resource "aws_instance" "server_instance" { | |
key_name = var.key_name | |
ami = var.ami_id | |
subnet_id = var.subnet_id | |
vpc_security_group_ids = [aws_security_group.main_server_access.id] | |
instance_type = var.instance_type | |
user_data = data.template_file.userdata.rendered | |
iam_instance_profile = aws_iam_instance_profile.instance_profile.id | |
root_block_device { | |
volume_type = "gp2" | |
volume_size = var.volume_size | |
} | |
volume_tags = { | |
Environment = var.Environment | |
} | |
tags = { | |
created_by = "Terraform" | |
Terraform = "true" | |
Environment = var.Environment | |
Name = var.instance_name | |
} | |
} | |
# Slaves | |
resource "aws_instance" "slave_instance" { | |
count = var.slaves | |
key_name = var.key_name | |
ami = var.ami_id | |
subnet_id = var.subnet_id | |
vpc_security_group_ids = [aws_security_group.main_server_access.id] | |
instance_type = var.instance_type | |
user_data = data.template_file.userdata.rendered | |
iam_instance_profile = aws_iam_instance_profile.instance_profile.id | |
root_block_device { | |
volume_type = "gp2" | |
volume_size = var.volume_size | |
} | |
volume_tags = { | |
Environment = var.Environment | |
} | |
tags = { | |
created_by = "Terraform" | |
Terraform = "true" | |
Environment = var.Environment | |
Name = "${var.instance_name}-${count.index}" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inputs map of maps loaded from config.auto.tfvars.json | |
variable "inputs" {} | |
# Number of slave servers to launch | |
variable "slaves" { | |
default = "3" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment