user393
user393

Reputation: 49

Terraform not picking the list(string) variable

I am getting the below error when i am trying run terraform plan.I am running terraform on terraform cloud and the var file is already set

Terraform error

Error: No value for required variable

  on _inputs.tf line 130:
 130: variable "mc-q" {

The root module input variable "mq-c" is not set,
and has no default value. Use a -var or -var-file
command line argument to provide a value for this
variable.


Error: Variables not allowed

  on variables/dev.tfvars line 127:
 127:     "target_group_arns" = ["${aws_lb_target_group._1416.arn}"]

Variables may not be used here.

I am using terraform 0.12.19 version.

Here is the Terraform code :

#asg.tf

#here is the asg group where I am using target_group_arns

    resource "aws_autoscaling_group" "asg_2" {
      name                    = "${var.mq-c.node_name}-asg-${var._env}"
      desired_capacity        = var.mq-c.asg.desired_capacity
      max_size                = var.mq-c.asg.max_size
      min_size                = var.mq-c.asg.min_size
      vpc_zone_identifier     = data.aws_subnet_ids.self.ids
      service_linked_role_arn = data.aws_iam_role.role-autoscaling.arn
      target_group_arns = var.mq-c.asg.target_group_arns
    
      health_check_type         = "EC2"
      health_check_grace_period = 300
      metrics_granularity       = "1Minute"
        
      termination_policies = [
           "OldestInstance",
           "OldestLaunchTemplate"]
      force_delete          = false
      protect_from_scale_in = var.mq-c.asg.protect_from_scale_in
      
      launch_template {
        id      = aws_launch_template.lt_2.id
        version = "$Latest"}}

#inputs.tf

#declare input types for asg

    variable "mq-c" {
            type = object({
            node_name = string
            lt = object({
              instance_type           = string
              disable_api_termination = bool
            })
            asg = object({
              desired_capacity      = number
              max_size              = number
              min_size              = number
              protect_from_scale_in = bool
              target_group_arns     = list(string)
            })
          })
        }

#dev.tfvars

#assigning variables for dev environment


    mq-c = {
      "node_name" = "mq-c"
      "lt" = {
        "instance_type"           = "m5.large"
        "disable_api_termination" = false
      }
      "asg" = {
        "desired_capacity"      = 1
        "max_size"              = 1
        "min_size"              = 1
        "protect_from_scale_in" = false
        "target_group_arns" = ["${aws_lb_target_group._1416.arn}"]
      }
    }

#creating network lb with multiple ports.

#lb.tf


    resource "aws_lb" "mq-lb" {
      name               = "mq"
      internal           = true
      load_balancer_type = "network"
    
      subnet_mapping {
        subnet_id = sort(data.aws_subnet_ids.self.ids)[0]
      }
      
      enable_deletion_protection = false
      idle_timeout               = 200
    }
    resource "aws_lb_listener" "_1416" {
      load_balancer_arn = aws_lb.mq-lb.arn
      protocol          = "TCP"
      port              = "1416"
      default_action {
        type             = "forward"
        target_group_arn = aws_lb_target_group._1416.arn
      }
    }
    resource "aws_lb_listener" "_1417" {
      load_balancer_arn = aws_lb.mq-lb.arn
      protocol          = "TCP"
      port              = "1417"
      default_action {
        type             = "forward"
        target_group_arn = aws_lb_target_group._1417.arn
      }
    }
    resource "aws_lb_target_group" "_1416" {
      name     = "1416"
      vpc_id   = data.aws_vpc.self.id
      port     = "1416"
      protocol = "TCP"
    }
    resource "aws_lb_target_group" "_1417" {
      name     = "1417"
      vpc_id   = data.aws_vpc.self.id
      port     = "1417"
      protocol = "TCP"
    }

When I am assigning the target_group_arns directly in the asg.tf,I am not getting any error and the target group is attaching to instance,however when I am specifying the target_group_arns in the tfvars file I am getting the validation error.

Upvotes: 2

Views: 1258

Answers (1)

Don
Don

Reputation: 574

You are getting a cascaded error because you are trying to do something that can't be done:
You can't use a resource variable in a tfvars file as an input parameter. So,
"target_group_arns" = ["${aws_lb_target_group._1416.arn}"]
in your dev.tfvars file will never work.
That's why you get the Variables not allowed error.
That error prevents the mq-c variable from being read and that's why you get the
No value for required variable error.
You'll need to adjust your design so that you don't need
"target_group_arns" = ["${aws_lb_target_group._1416.arn}"]
as an input parameter.

Upvotes: 2

Related Questions