CodexBloom - Programming Q&A Platform

Terraform how to to reference outputs from a module in another module with inconsistent variable types

๐Ÿ‘€ Views: 1066 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-05
terraform modules type-mismatch HCL

I'm working with an scenario where I'm trying to pass output values from one Terraform module to another, but I'm running into problems with type mismatches... I have module A that outputs a string variable, and I want to pass that output to module B. However, module B is expecting a list type for that variable.\n\nHereโ€™s a snippet of my module A where I define the output:\n```hcl\noutput "my_output" {\n value = "some-string-value"\n}\n```\nIn module B, I define the variable like this:\n```hcl\nvariable "input_from_module_a" {\n type = list(string)\n}\n```\nAnd I try to reference it in my main Terraform file like this:\n```hcl\nmodule "module_a" {\n source = "./module_a"\n}\n\nmodule "module_b" {\n source = "./module_b"\n input_from_module_a = [module.module_a.my_output]\n}\n```\nWhen I run `terraform apply`, I get the following behavior message:\n````plaintext\nError: Incorrect attribute value type\n\n on main.tf line XX:\n input_from_module_a = [module.module_a.my_output]\n\nInappropriate value for attribute "input_from_module_a": string required.\n```\nIโ€™ve tried wrapping the output in `tolist()` and `flatten()`, but it still results in the same behavior. Iโ€™m not sure how to convert the single string output to a format that module B can accept. Any suggestions on how to handle this type inconsistency effectively? I'm using Terraform version 1.2.0. Thanks in advance for any help! Thanks in advance!