Home > Back-end >  Terraform's external data source: STDOUT syntax unclear
Terraform's external data source: STDOUT syntax unclear

Time:11-28

I would like to use terraform's external data source to identify certain AWS EC2 instances:

data "external" "monitoring_instances" {
  program = ["bash", "${path.module}/../bash/tf_datasource_monitoring.sh"]

  query = {
    env = var.env_stage
  }
}

The bash script is using AWS CLI to return a list of instance IDs.

But I keep receiving this Error: command "bash" produced invalid JSON: json: cannot unmarshal array into Go value of type string

I don't understand what the expected syntax of my script's STDOUT would be for terraform to understand the result.

So let's assume the script is supposed to return 3 instance IDs i-1, i-2 and i-3.

What would be the correct JSON syntax to be returned to terraform?

Examples, that do NOT work:

{
"instances": [
  "i-1",
  "i-2",
  "i-3"
]
}
[
  "i-1",
  "i-2",
  "i-3"
]

CodePudding user response:

It is a known issue in Terraform for provider-external: https://github.com/hashicorp/terraform-provider-external/issues/2. It was opened a while ago, unfortunately is still present for latest version (Terraform v1.011).

You may want to avoid returning JSON objects which contain arrays.

  • Related