4

I'm trying to figure out how to update the JSON config files in my .NET Core web service, based on the deployed resources using Terraform.

I have an existing Azure DevOps pipeline, which builds/deploys a .NET Core web service to an Azure App Service.

In moving to Terraform, I'll be creating a CosmosDb database, Azure Search service, Event Grid, etc. for dev/test/prod environments.

I have a handle on creating these in Terraform, but I'm not clear how to take the outputs from these resources (like the CosmosDb location, key, and database id) and inject these into my JSON config files in my deployed web service.

Has anyone done this sort of thing, and can show a Terraform example? Thanks!

2
  • Hi, is it successful for you by using Jamie's solution? Dis you still facing another issue? Feel free to leave comment here, thus we could continue to help. Commented Oct 24, 2019 at 10:15
  • Thanks Merlin. I think Jamie’s approach will work. Will be trying it soon. But if Microsoft has any other guidance on how to best integrate Terraform with ASP.NET Core web services hosted on Azure App Services or even other deployment methods like K8s, it would be great to have more documentation. Commented Oct 25, 2019 at 21:22

1 Answer 1

1

You don't actually inject those into your config file, you set those as app settings in your App Service and that will override those keys in your config file.

So if you have:

{
   CosmosDb: {
       Key: ""
   }
}

In your terraform you would do the following.

resource "azurerm_app_service" "test" {
  name                = "example-app-service"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
  app_service_plan_id = "${azurerm_app_service_plan.test.id}"

  app_settings = {
      "CosmosDb:Key" = "${azurerm_cosmosdb_account.db.primary_master_key}"
  }
}

So you would reference your other Terraform resources to pull out the values you need and put those in the app settings section of your App Service in Terraform.

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

2 Comments

Interesting thanks. So if I have this kind of config in appsettings.XYZ.json (per-environment), and I'm using ConfigurationBuilder.AddJsonFile, I could use RepositorySettings:Location in app_settings instead? I guess then I wouldn't need these in the separate config files, and just make it part of Terraform per-env deployment? "RepositorySettings": { "Location": "https://...documents.azure.com:443/", "Key": "...", "DatabaseId": ... }
Yep, that's correct. We don't use app settings files when deploying to Azure. We only use the app settings in the App Service. That way if you decide to use deployment slots you can adjust those separate from deployment.

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.