terraform-provider-null
terraform-provider-null copied to clipboard
Unable to use output variable as list
This issue was originally opened by @nusnewob as hashicorp/terraform#8048. It was migrated here as part of the provider split. The original body of the issue is below.
Terraform Version
Terraform v0.7.0
Affected Resource(s)
- aws_elb
- aws_db_subnet_group
Terraform Configuration Files
main.tf
module "subnets" {
source = "./modules/test"
}
resource "aws_elb" "elb" {
name = "test"
subnets = ["${module.subnets.subnets["public"]}"]
listener {
lb_port = 80
lb_protocol = "http"
instance_port = 80
instance_protocol = "http"
}
}
or
...
subnets = ["${split(",", join(",", module.subnets.subnets["public"]))}"]
...
modules/test/module.tf
data "null_data_source" "subnets" {
inputs = {
public = ["subnet-abcd1234","subnet-abcd1235","subnet-abcd1236"]
}
}
output "subnets" {
value = "${data.null_data_source.subnets.input}"
}
Debug Output
https://gist.github.com/nusnewob/b3230d0d83c40917516a068b3532092f
Expected Behavior
Should be able to pass the module output as list variable
Actual Behavior
Error refreshing state: 1 error(s) occurred:
* inputs: 1 error(s) decoding:
* '[public]' expected type 'string', got unconvertible type '[]interface {}'
Steps to Reproduce
-
terraform apply
orterraform plan
There are many scenarios where a null_data_source
output would be useful as a list rather than a simple string. Is there are specific technical reason to not allow this?
My specific use-case is taking a comma-delimited value from Consul (via consul_keys
, which is all string-based) and converting it to a list with a null_data_source
so I would only have to deserialize once rather than every place where that value is used.
I suppose I could write a module to do this process for me, but that seems overkill.
You can't do this because it's a list inside of a hash (inputs). There's a core TF bug around improper or incomplete nested type passing.
Do this instead:
data "null_data_source" "stringified" {
inputs = {
public = "subnet-1234,subnet-3456,subnet-5678"
}
}
output "subnets" {
value = "${split(",", data.null_data_source.stringified.inputs.public)}"
}
@in4mer This gives self reference not allowed error.
Terraform 1.4 will contain a new terraform_data
resource which can accept input data of any type, reflections of that data into an unknown output of the same type, and value updates. Terraform prereleases are available if anyone wants to try out the upcoming functionality.
Reference: https://github.com/hashicorp/terraform/blob/main/website/docs/language/resources/terraform-data.mdx
Terraform 1.4.0 has been released today with the new terraform_data
resource. It should not impose any type restrictions on input
or output
. If there are any feature requests or bug reports with the terraform_data
resource, please create an issue in the Terraform core issue tracker.
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.