In AWS using terraform, have to create a load balancer resource only when the LB does not exist. For this, I can have a variable defined like "lb_exists=true" and based on this "true" value, the resource has to be created otherwise, terraform should skip the LB creation.
To achieve this, I am planning to have depends_on. If "lb_exists=false", then depends_on should be an empty list or set to no resource name.
locals {
lb_exists = "true"
}
resource "aws_lb" "test" {
name = var.alb_name
internal = false
load_balancer_type = "application"
#...
}
data "aws_lb" "test" {
lb_exists = "${local.lb_exists}"
depends_on = ["aws_lb.test"]
}
What is the right way to achieve this and how? is there any workaround for this. I do not want to recreate the LB if exists in the AWS. Please suggest.
[EDIT] copying the full code
provider "aws" {
region = var.aws_region
}
resource "aws_lb" "test" {
count = local.lb_exists == "true" ? 1 : 0
name = var.alb_name
internal = false
load_balancer_type = "application"
security_groups = var.alb_security_groups
subnets = var.alb_subnets
enable_deletion_protection = true
access_logs {
#bucket = aws_s3_bucket.lb_logs.bucket
bucket = "aws-lab-demo"
prefix = "test-lb-logs"
enabled = false
}
tags = {
Environment = var.env
Name = var.vpc_id
}
}
data "aws_lb" "test" {
# count = local.lb_exists == "true" ? 1 : 0
arn = "${aws_lb.test.arn}"
name = "${aws_lb.test.name}"
}
resource "aws_lb_target_group" "test" {
name = var.alb_name
port = 8080
protocol = "HTTP"
vpc_id = var.vpc_id
stickiness {
type = "lb_cookie"
cookie_duration = 1800
enabled = false
}
health_check {
healthy_threshold = 3
unhealthy_threshold = 10
timeout = 5
interval = 10
path = "/"
port = "8081"
}
}
data "aws_lb_target_group" "test" {
arn = "${aws_lb_target_group.test.arn}"
name = "${aws_lb_target_group.test.name}"
}
resource "aws_lb_listener" "front_end" {
load_balancer_arn = data.aws_lb.test.arn
port = "80"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = data.aws_lb_target_group.test.arn
}
}