Unable to create ECS Service along with Application Load Balancer and associate it with Target Group using the script. Getting Error: InvalidParameterException: The target group with targetGroupArn does not have associated load balancer
terraform {
The configuration for this backend will be filled in by Terragrunt
backend “s3” {}
}
provider “aws” {
region = “${var.aws_region}”
}
data “aws_region” “current” {}
data “template_file” “log_bucket_policy” {
template = “{file("{path.module}/log-bucket-policy.json.tpl”)}"
vars = {
elb_account_id = 47998XXXXX
bucket_name = aws_s3_bucket.alb_logs_bucket.bucket
}
}
resource “aws_s3_bucket” “alb_logs_bucket” {
bucket = “compass-be-alb-logs-${var.env}”
acl = “private”
}
resource “aws_s3_bucket_policy” “logs_bucket_policy” {
bucket = “{aws_s3_bucket.alb_logs_bucket.id}"
policy = "{data.template_file.log_bucket_policy.rendered}”
}
resource “aws_security_group” “backend_alb_sg” {
name = “CompassBackendALBSG-{var.env}"
description = "Allow HTTP from customer network"
vpc_id = "{var.vpc_id}”
ingress {
description = "Allow HTTPS from customer network."
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = var.customer_network_cidr
}
ingress {
description = "Allow HTTP from customer network."
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = var.customer_network_cidr
}
egress {
description = "Allow all trafic out"
from_port = 1
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge({
"Name" = "CompassBackendALBSG-${var.env}"
}, tomap(var.default_tags))
}
resource “aws_lb” “backend_lb” {
name = “Compass-backend-alb-{var.env}"
internal = false
load_balancer_type = "application"
subnets = var.private_subnets
security_groups = ["{aws_security_group.backend_alb_sg.id}”]
enable_deletion_protection = true
access_logs {
bucket = "${aws_s3_bucket.alb_logs_bucket.bucket}"
enabled = true
}
tags = var.default_tags
}
resource “aws_lb_target_group” “fargate_target_group” {
name = “Compass-backend-tg-{var.env}"
port = var.container_port
protocol = "HTTP"
target_type = "ip"
vpc_id = "{var.vpc_id}”
health_check {
path = "/api/v"
port = 8080
matcher = "200-299"
interval = 60
timeout = 10
healthy_threshold = 3
unhealthy_threshold = 5
}
}
resource “aws_lb_listener” “compass_backend_listener_443” {
load_balancer_arn = “{aws_lb.backend_lb.arn}"
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = "{var.backend_cert_arn}”
default_action {
target_group_arn = "${aws_lb_target_group.fargate_target_group.arn}"
type = "forward"
}
}
resource “aws_lb_listener” “compass_backend_listener_80” {
load_balancer_arn = “${aws_lb.backend_lb.arn}”
port = 80
protocol = “HTTP”
default_action {
type = "redirect"
redirect {
port = 443
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}