From 87264265717e38b68301c1915eefaa202c3bf42d Mon Sep 17 00:00:00 2001 From: Jan Jacob Bos Date: Mon, 7 Nov 2022 16:34:26 +0100 Subject: [PATCH] Initial Commit --- main.tf | 36 ++++++++++++++++++++++++++++++++++++ variables.tf | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 main.tf create mode 100644 variables.tf diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..2e46049 --- /dev/null +++ b/main.tf @@ -0,0 +1,36 @@ +resource "aws_lb" "nlb_app" { + name = format("nlb-%s-lb01-app-%s", var.env_short, var.app_name) + internal = true + load_balancer_type = "network" + subnets = var.lb_subnet_ids +} + +resource "aws_lb_listener" "nlb_l_app" { + for_each = var.protocol + load_balancer_arn = aws_lb.nlb_app.arn + port = each.value + protocol = "TCP" + + default_action { + type = "forward" + target_group_arn = aws_lb_target_group.tg_app[each.key].arn + } + + lifecycle { + create_before_destroy = true + } +} + +resource "aws_lb_target_group" "tg_app" { + for_each = var.protocol + name = format("nlb-%s-lb01-app-%s-%s", var.env_short, var.app_name, each.key) + port = each.value + protocol = "TCP" + vpc_id = var.vpc_id +} + +resource "aws_autoscaling_attachment" "asg_attachment_app" { + for_each = var.protocol + autoscaling_group_name = var.asg_name + lb_target_group_arn = aws_lb_target_group.tg_app[each.key].arn +} \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..fd6b277 --- /dev/null +++ b/variables.tf @@ -0,0 +1,33 @@ +variable "env_short" { + type = string + description = "Set the short environment name, i.e. dev, test, acc, prod" +} + +variable "app_name" { + type = string + description = "Set the application name i.e. green or blue" +} + +variable "lb_subnet_ids" { + type = list(any) + default = null + description = "The list of subnet IDs where the lb will live" +} + +variable "protocol" { + type = map(string) + default = null + description = "list of names and ports to be listening on" +} + +variable "vpc_id" { + type = string + default = null + description = "Define VPC ID" +} + +variable "asg_name" { + type = string + default = null + description = "Set ASG name" +} \ No newline at end of file