0

I have the below Terraform code.

I am trying to get the 'application id' of an Azure Enterpise Application.

# Get the Application_Id of the Enterprise Application having the same name as the Synapse Workspace
data "external" "enterprise_app" {
  program = ["bash", "-c", <<EOT
    az ad sp list --display-name "${var.enterprise_application_name}" --query "[0].appId" -o json
  EOT
  ]
}

output "application_id" {
  value = replace(data.external.enterprise_app.result, "\"", "") # remove double quotes
}

On the terraform plan I get the below error...

Error: Unexpected External Program Results │ │ with module.get_enterprise_app_application_id.data.external.enterprise_app, │ on ../../resource_modules/get_enterprise_app_application_id/main.tf line 3, in data "external" "enterprise_app": │ 3: program = ["bash", "-c", <<EOT │ 4: az ad sp list --display-name "${var.enterprise_application_name}" --query "[0].appId" -o json │
5: EOT │ 6: ] │ │ The data source received unexpected results after executing the program. │ │ Program output must be a JSON encoded map of string keys and string values. │ │ If the error is unclear, the output can be viewed by enabling Terraform's │ logging at TRACE level. Terraform documentation on logging: │ https://www.terraform.io/internals/debugging │ │ Program: /usr/bin/bash │ Result Error: json: cannot unmarshal string into Go value of type │ map[string]string

I am not sure how I can fix my code?

1
  • I would follow the error message's recommendation to enable logging at TRACE level to view the actual parsed stdout and inspect it for debugging to ensure it is the expected format. Commented Apr 4 at 19:48

1 Answer 1

0

Fetching the Enterprise Application ID at the output using external Data in terraform.

The command youre using inside the Terraform's external data source expected to be JSON with key value pair but that command is giving single JSON string which is the reason for the blocker youre facing.

To overcome this you need to represent the external data source to key value format. I tried a demo confirguration which matches the requriement to fetch the entirprise application ID at the output without any issue.

demo configuration:

data "external" "enterprise_app" {
  program = ["bash", "-c", <<EOT
    app_id=$(az ad sp list --display-name "${var.enterprise_application_name}" --query "[0].appId" -o tsv)
    echo "{\"app_id\": \"$app_id\"}"
  EOT
  ]
}

output "application_id" {
  value = data.external.enterprise_app.result["app_id"]
}

Deployment:

enter image description here

enter image description here

Refer:

https://learn.microsoft.com/en-us/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-list

https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.