Home > Software design >  Use Azure CLI command with Terraform "data source" in Windows is failing
Use Azure CLI command with Terraform "data source" in Windows is failing

Time:03-11

I have an Azure CLI command that returns Cognitive Search API Keys and need to use its ouput in terraform. The command reads

> az search admin-key show  -g <my resource group> --service-name <my search service>
{
  "primaryKey": "1...",
  "secondaryKey": "4..."
}

I am using this Terraform resource:

data "external" "search_admin_key" {
  count   = local.deploy_indexes ? 1 : 0
  program = [
    "az", "search admin-key show -g ${var.rg_name}  --service-name ${var.cognitive_search_name}"
  ]

}

and I can not get it working on windows:

  • when I use just one string in the array with the entire command, I get an error executable file not found in %PATH%
  • when I keep az as first element and everything else in second, I get an error Error Message: ERROR: 'search admin-key show -g <my resource group> --service-name <my search service>' is misspelled or not recognized by the system.

Note that I use az in null-resource / local-exec provisioners and it works fine there.

How can I fix the problem and run the command I need via external data source syntax?

CodePudding user response:

The external data source expects its program argument to contain a separate list element for each separate argument to the program.

I'm not familiar with the Azure CLI you are running here and so I can't be sure how exactly it interprets its command line, but guessing based on typical conventions for Windows CLI applications I would try the following:

data "external" "search_admin_key" {
  count   = local.deploy_indexes ? 1 : 0
  program = [
    "az", "search", "admin-key", "show", "-g", var.rg_name, "--service-name", var.cognitive_search_name,
  ]
}

When running the program, the data source will insert any necessary quotes and escapes automatically to deal with the possibility that var.rg_name and var.cognitive_search_name might contain spaces and other special characters. However, on Windows there is no single standard for what quoting and escaping is required, so it will always use the most typical convention that was originally introduced by the Microsoft Visual C runtime library. Most modern Windows CLI programs will accept that format, so I expect the above will work, but I mention it just in case az is a program with unusual requirements.

  • Related