Terraform 0.12.19在Cloud中配置list(string)变量触发报错求助
Hey there, let's break down and fix those two Terraform errors you're hitting with v0.12.19 in Terraform Cloud:
Error 1: Root module variable "mq-c" is not set and has no default value
This happens because Terraform can't locate a valid value for the mq-c variable you declared in inputs.tf. Even though you defined the value in dev.tfvars, you need to ensure Terraform Cloud is actually loading that configuration for your workspace.
Fixes:
- In Terraform Cloud Workspace:
- Navigate to your workspace's Variables tab.
- Under the Terraform Variables section, add the entire
mq-cobject as a variable (paste the JSON-like structure fromdev.tfvars), or - Create a Variable Set containing the
dev.tfvarscontent and attach it to your dev workspace, or - If your
dev.tfvarsis stored in your linked VCS repo, configure your workspace to use this file for variable inputs (check the "Terraform Variables" settings for file inclusion options).
- Optional Fallback: Add a default value to the
mq-cvariable ininputs.tf(useful for testing baseline configurations):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) }) }) # Example default configuration default = { node_name = "default-mq-c" lt = { instance_type = "t2.micro" disable_api_termination = false } asg = { desired_capacity = 1 max_size = 2 min_size = 0 protect_from_scale_in = false target_group_arns = [] } } }
Error 2: Using ${aws_lb_target_group._1416.arn} in dev.tfvars is not allowed
This is intentional Terraform behavior: .tfvars files are designed for static values only (like strings, numbers, or lists of static text). You can't reference dynamic Terraform resource attributes (like aws_lb_target_group._1416.arn) here because resource ARNs are generated during Terraform's execution phase, while .tfvars files are parsed much earlier (during variable loading).
Fixes:
Choose an option based on how flexible your configuration needs to be:
Option 1: Directly reference the target group ARN in asg.tf (simplest)
Since you confirmed this works, replace the variable reference with the actual resource ARN in your autoscaling group:
resource "aws_autoscaling_group" "asg_2" { # ... your existing configuration ... target_group_arns = [aws_lb_target_group._1416.arn] # Replace var.mq-c.asg.target_group_arns # ... rest of your config ... }
Option 2: Keep variable flexibility with dynamic data lookup (multi-environment friendly)
If you need different target groups per environment, pass static target group names in .tfvars instead of ARNs, then use a data source to fetch the dynamic ARNs:
Update
inputs.tfto accept target group names: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_names = list(string) # Renamed from target_group_arns }) }) }Update
dev.tfvarsto pass static target group names: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_names" = ["1416"] # Static name, not dynamic ARN } }Add a data source in asg.tf to fetch ARNs from names:
# Fetch target group ARNs by name data "aws_lb_target_group" "mq_target_groups" { for_each = toset(var.mq-c.asg.target_group_names) name = each.value vpc_id = data.aws_vpc.self.id } resource "aws_autoscaling_group" "asg_2" { # ... your existing configuration ... target_group_arns = values(data.aws_lb_target_group.mq_target_groups)[*].arn # Map names to ARNs # ... rest of your config ... }
Option 3: Use modules for reusable components
For larger setups, wrap your load balancer and target groups in a Terraform module that outputs target group ARNs. Then reference the module's output in your ASG configuration to keep your code clean and reusable across environments.
内容的提问来源于stack exchange,提问作者user393

