Home > Net >  Terraform resource "aws_alb_target_group" not working. How to fix it?
Terraform resource "aws_alb_target_group" not working. How to fix it?

Time:10-31

I was latest terrfaorm version 1.3.3 and aws version "aws-cli/2.7.5 Python/3.9.11 Windows/10 exe/AMD64 prompt/off" and here is my script.

# Application load balancer

resource "aws_elb" "main" {
    name            = "constructor-io-elb-tf"
    description     = "Creating new ELB for the constructor-io"
    subnets         = aws_subnet.public.*.id 
    security_groups = [aws_security_group.lb.id]
}

# Creating a target group for http

resource "aws_alb_target_group" "tg" {
    name        = "constuctor-target-group-tf"
    port        = 80
    provider    = http
    vpc_id      = aws_vpc.main.id
    target_type = "ip"

    health_check {
        healthy_threshold   = "2"
        unhealthy_threshold = 1
        interval            = "20"
        protocol            = http
        matcher             = "200"
        timeout             = "5"
        health_check_path   = var.health_check_path
    }
}

# Redirecting all the traffic from ALB to target group

resource "aws_alb_listener" "listener" {
    load_balancer_arn = aws.alb.main.id
    port              = var.app_port
    protocol          = http

    default_action {
        target_group_arn = aws_alb_target_group.tg.id
        type             = "forward"
    }
  
}

Wehn I run "terraform apply it was saying,

│ Error: Invalid resource type
│
│   on alb.tf line 12, in resource "aws_lb_target_group" "tg":
│   12: resource "aws_lb_target_group" "tg" {
│
│ The provider hashicorp/http does not support resource type "aws_lb_target_group".

I also tried with "aws_alb_target_group" and upgraded using "terraform init -upgrade" Nothing works.

CodePudding user response:

Please make sure you read the documentation properly prior to running any configuration [1]. Terraform is complaining here because it expects the http provider. Other than that there are more errors. You need to change the code to be the following:

resource "aws_alb_target_group" "tg" {
    name        = "constuctor-target-group-tf"
    port        = 80
    vpc_id      = aws_vpc.main.id
    target_type = "ip"

    health_check {
        healthy_threshold   = "2"
        unhealthy_threshold = 1
        interval            = "20"
        protocol            = "HTTP" # <---- it has to be with quotes and uppercase
        matcher             = "200"
        timeout             = "5"
        health_check_path   = var.health_check_path
    }
}

# Redirecting all the traffic from ALB to target group

resource "aws_alb_listener" "listener" {
    load_balancer_arn = aws.alb.main.id
    port              = var.app_port
    protocol          = "HTTP" # <---- it has to be with quotes and uppercase

    default_action {
        target_group_arn = aws_alb_target_group.tg.id
        type             = "forward"
    }
  
}

[1] https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group#protocol

CodePudding user response:

The configuration for your target group are incorrect. Following your example, you should configure it like this:

resource "aws_alb_target_group" "tg" {
    name        = "constuctor-target-group-tf"
    port        = "80"
    protocol    = "HTTP"
    vpc_id      = aws_vpc.main.id
    target_type = "ip"

    health_check {
        healthy_threshold   = "2"
        unhealthy_threshold = 1
        interval            = "20"
        protocol            = "HTTP"
        matcher             = "200"
        timeout             = "5"
        health_check_path   = var.health_check_path
    }
}

And the listener:

resource "aws_alb_listener" "listener" {
    load_balancer_arn = aws.alb.main.id
    port              = "80"
    protocol          = "HTTP"

  default_action {
    type             = "forward"
    target_group_arn = aws_alb_target_group.my_lb_target_group.arn
  }
}

If you still have any doubts, feel free to contact me.

  • Related