0

There are similar questions to this one on Stackoverflow, but none of them addressing my issue in using Terraform when an Azure Storage account is used to retain outputs.

Here is my scenario, which may sound familiar:

My terraform script provisions an Azure HTTP-triggered function with a function key. Also, this terraform script provisions a Web App that calls the mentioned HTTP-triggered function. The HTTP-triggered function's key is stored in the Web App's appsettings.json file to include it in the HTTP headers' calls to the HTTP-triggered function.

The following code snippet illustrates how the HTTP-triggered function is provisioned in terraform:

resource "azurerm_function_app" "myhttpfunc" {
  name                      = var.funcname
  location                  = "${azurerm_resource_group.rc.location}"
  resource_group_name       = "${azurerm_resource_group.rc.name}"
  app_service_plan_id       = "${azurerm_app_service_plan.funcsserviceplan.id}"
  storage_account_name      = "${azurerm_storage_account.funcstorage.name}"
  storage_account_access_key = "${azurerm_storage_account.funcstorage.primary_access_key}"
}

The output variables to access the function's keys is per below:

output "funchostkeys" {
  value = data.azurerm_function_app_host_keys.myhttpfunc
  sensitive = true
}

I assume that this output and others will appear in terraform.tfstate hosted on the dedicated Azure Storage account at some point down the road.

My question is that how one can get that particular output in an Azure Release pipeline to manipulate the appsettings.json file by replacing the configuration entry with what terraform has produced in the output variable?

For example this post suggests producing terraform's output to a file per the following line but that does not seem to be an option in my case because my terraform script leverages Azure storage to retain outputs.

terraform output -json > outputs.json

1 Answer 1

1

Based on your requirement, you could try to output the variable with the following command:

terraform output -raw funchostkeys

Then you could use the logging command to set the value as pipeline variable.

For example: Powershell Script

- powershell: |
   echo ##vso[task.setvariable variable=varname]$(terraform output -raw funchostkeys)
   
  displayName: 'PowerShell Script'

Then you could use the Replace token task to use the pipeline variable to replace the value in appsettings.json.

You could define #{variablename}# in appsettings.json. When you run the pipeline, the output value will be set in the target place.

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

1 Comment

I am unable to use Replace Token Task because that is apparently applicable to the build pipelines. In my scenario, terraform is running in a Release pipeline whose next step is to deploy the Azure Function. Therefore the variables are determined in release step where appsettings.json file already deployed to the azure function app. It's impossible to access the function's appsettings.json as it's residing in the zip file.

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.