Search code examples
terraformterraform-provider-azureterraform-template-file

Properly using Terraform External Data Source


I am using Terraform from the bash cloud shell in Azure. I am trying to add an external data source to my Terraform configuration file that will use az cli to query for the virtualip object on a Microsoft.Web/hostingEnvironment the template deploys.

AZ CLI command line: az resource show --ids /subscriptions/<subscription Id>/resourceGroups/my-ilbase-rg/providers/Microsoft.Web/hos tingEnvironments/my-ilbase/capacities/virtualip

Output when run from command line:

   {
      "additionalProperties": {
        "internalIpAddress": "10.10.1.11",
        "outboundIpAddresses": [
          "52.224.70.119"
        ],
        "serviceIpAddress": "52.224.70.119",
        "vipMappings": []
      },
      "id": null,
      "identity": null,
      "kind": null,
      "location": null,
      "managedBy": null,
      "name": null,
      "plan": null,
      "properties": null,
      "sku": null,
      "tags": null,
      "type": null
    }

In my Terraform config I create a variable for the --ids value:

    variable ilbase_resourceId {
      default = "/subscriptions/<subscription Id>/resourceGroups/my-ilbase-rg/providers/Microsoft.Web/hostingEnvironments/my-ilbase/capacities/virtualip"
    }

I then have the data source structured this way:

    data "external" "aseVip" {
      program = ["az", "resource", "show", "--ids", "${var.ilbase_resourceId}"]
    }

When I execute my configuration, I get the error below:

    data.external.aseVip: data.external.aseVip: command "az" produced invalid JSON: json: cannot unmarshal object into Go value of type string

Any ideas what I am doing wrong?


Solution

  • I discovered the problem was that the Terraform External Data Source is not yet able to handle the complex structure of what gets returned by the command. I was able to get around this by adding an AZ CLI command block at the beginning of the script I use to deploy the Application Gateway that grabs the IP address and passes it into the Terraform config as a variable. Below is the script block I am using:

    ilbase_virtual_ip=$(
      az resource show \
      --ids "/subscriptions/$subscription_id/resourceGroups/$ilbase_rg_name/providers/Microsoft.Web/hostingEnvironments/$ilbase_name/capacities/virtualip" \
      --query "additionalProperties.internalIpAddress"
    )