Search code examples
linuxamazon-web-servicesterraformaws-application-load-balancer

Argument or block definition required Terraform - Error


I have autoscaling group and I need to create an application load balancer for accessing the application this is my two code for autoscaling and application load balancer but I get this issue

autoscaling group

    resource "aws_launch_configuration" "OS-Type"{
        name_prefix = "OS-Type"
    
        image_id = "ami-0996d3051b72b5b2c"
        instance_type = "t2.micro"
        
        lifecycle {
         create_before_destroy = true
        }
    }
        
    resource "aws_autoscaling_group" "Dynamic-IN"{
        name = "Dynamic-EC2-instance"
        min_size = 1
        max_size = 4
        desired_capacity = 2
        health_check_type = "ELB"
        launch_configuration = aws_launch_configuration.OS-Type.name
        vpc_zone_identifier = [aws_subnet.P-AV1.id, aws_subnet.P-AV2.id]
        target_group_arns="aws_lb.App-lb.name"
        lifecycle {
          create_before_destroy = true
        }
    }

Application load balancer

    resource "aws_lb_target_group" "Target-group"{
        name = "Target-group"
        port = 80 
        protocol = "HTTP"
        vpc_id = aws_vpc.main.id
    }

    resource "aws_lb" "App-lb"{
        name = "Application-load-balancer"
        load_balancer_type = "application"
        subnets = [aws_subnet.P-AV1.id , aws_subnet.P-AV2.id]
        internal = false
    }

    resource "aws_autoscaling_attachment" "TG-attach" {
      autoscaling_group_name = aws_autoscaling_group.Dynamic-IN.id
      alb_target_group_arn   = aws_lb_target_group.Target-group.arn
    }

I get this error

Error: Argument or block definition required

on autoscalling-group.tf line 20, in resource "aws_autoscaling_group" "Dynamic-IN": 20: target_group.arns="aws_lb.App-lb.name"

An argument or block definition is required here. To set an argument, use the equals sign "=" to introduce the argument value.

I have tried

I have tried aws_lb.App-lb.arns for the target group alos but not working in the both ways


Solution

  • Yes like you suspect there should not quotes there:
    target_group_arns="aws_lb.App-lb.name"

    and that 'target_group_arns' is a set not a single item: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group#target_group_arns

    target_group_arns (Optional) A set of aws_alb_target_group ARNs, for use with Application or Network Load Balancing.

    Your code should probably be something like:

    resource "aws_autoscaling_group" "Dynamic-IN" {
        name     = "Dynamic-EC2-instance"
        min_size = 1
        max_size = 4
    
        desired_capacity     = 2
        health_check_type    = "ELB"
        launch_configuration = aws_launch_configuration.OS-Type.name
        vpc_zone_identifier  = [ aws_subnet.P-AV1.id, aws_subnet.P-AV2.id ]
        target_group_arns    = [ aws_lb_target_group.Target-group.arn ]
    
        lifecycle {
          create_before_destroy = true
        }
    }