2
votes

I have created an Azure function using Visual Studio 2017 and it uses local.settings.json file to store database connection string which works fine for my test environment.

Now I need to add database connection string for the staging environment. How can I create a separate staging.settings.json file and associate that to my staging build in Visual Studio, so that it will take the staging.settings.json file's settings during staging deployment?

I created a staging.settings.json and copied the settings file from local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "sql_conn": "Server=tcp:SERVER,1433;Initial Catalog=DBNAME;Persist Security Info=False;User ID=;Password=;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
   }
}
3
You should have separate azure functions for each environment with corresponding setting assigned via azure portal. - Slava Utesinov

3 Answers

1
votes

local.setting.json is the file which is used for local development only. This file is not deployed to Azure: the settings are taken from Environment while running in the cloud.

So, if you have beta/production Function App, you will configure their settings in App Service Settings.

In my opinion best approach would be to have ARM template based deployment,where you will have two files.

  • ARM template file for azure function which include generic variable
  • Template parameter file which will have the actual value for the variable declared in the template

For every environment you will have separate function and once you run the build for the function, it will replace the existing settings.

Hope it helps.

1
votes

The staging Function is hosted in Azure, the local.settings.json file won't be uploaded to Azure. It's only for local dev.

In Azure portal, your pro Function and staging Function have different Application settings page, so you could directly set your values.

enter image description here

When we publish Functions with VS, there's a friendly dialog to update Application settings. You could also configure the connection strings there. The Remote setting will be upload to Azure.

enter image description here

enter image description here

0
votes

See my response here: https://stackoverflow.com/a/57944352/3563013 Basically, create other tasks to copy to local.settings.json