How Can I publish my Azure Function's local.settings.json?
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"CosmoDbAuthKey": "***********************************",
"CosmoDbEndpoint": "https://**************************:443/",
"CosmosDbId": "***Notifications"
}
}
This answer is not condoning the practice of publishing the local.settings.json file, since it is intended to be used for local development and debugging only. Others have already emphasized that, but they are apparently mistaken that there is "no way to publish". There is indeed a method to publish it and it's specified in Microsoft's online docs:
By default, these settings are not migrated automatically when the project is published to Azure. Use the --publish-local-settings switch when you publish to make sure these settings are added to the function app in Azure. Note that values in ConnectionStrings are never published.
You cannot publish local.settings.json file to Azure. This file is for local development only.
To add settings for your deployed function:
Go to https://portal.azure.com
Navigate to your function app
In the left hand side menu, look for Settings
Select Configuration
The default loaded tab should be Application Settings
Select New application setting
Add Name and Value. Name should be the same as your local.setting.json entry i.e. CosmosDbId. Value may change depending on your environment.
You cannot publish local.settings.json, what you need to do is to add them under the "Application settings" tab of the published function app in the Azure Portal
Related
I created an Azure function using Visual Studio. Local.setting.json file had following properties:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"ServiceBusConnString": "Endpoint=sb://sb-new-two.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=9FygKPHH2eJVp3GmAiUxtT7sGnddsaddadNIrciM0=",
"Test": "sb-new-two.servicebus.windows.net"
}
}
This is how my function looks:
[FunctionName("Function1")]
public void Run([ServiceBusTrigger("topic-one", "sub-one", Connection = "ServiceBusConnString")] string mySbMsg)
{
_logger.LogInformation("Processing message");
_logger.LogInformation($"Message : {mySbMsg}");
Console.WriteLine(mySbMsg);
}
After deploying the azure function, I do not see the test property. I am not using it in my code. But wondering why is this property missing?
Check in your .gitignore file if it includes the local.settings.json. Better yet add the value of your Test config manually in the Configuration section of your Azure function. Go to your function app in Azure, under Settings > Configuration > New Application setting, then add your Test config.
One of the workarounds to publish the app settings from local.settings.json to the Azure Portal Function App Configuration is:
Before publishing the function project to the Azure Portal, below is the configuration of my Function App:
Azure Functions Core Tools cmdlet:
func azure functionapp publish KrishSbFunApp01 --publish-local-settings -i
Before running this cmdlet, you have to change the value of AzureWebJobsStorage to the Azure Storage account connection string.
Also, you can overwrite the app settings by using the parameter --overwrite-settings -y, available in MS Doc Source.
One can use app settings AzureWebJobs.<FUNCTION_NAME>.Disabled to disable individual Functions in an Azure Function. However, that of course requires that you know all the Function names.
Is there a way to disable all Functions in a similar way? (and no, just Stopping the function app is not an option).
In Local Azure Function Project, we can add the disabled attribute for the multiple functions we need to disable:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobs.BlobTriggerFunction.Disabled": true,
"AzureWebJobs.SingleTonTimerFunction.Disabled": true
}
}
As you're adding the custom attribute using C# class library in every function code like [Disable("FUNCTION_DISABLED")] which is also considerable but using application settings is recommended as specified in this MS Doc.
I have 3 different functions in which 2 are disabled using the above code.
One of the similar issues is in open state in GitHub regarding disabling all the functions at a time is a feature request and there are some temporary workarounds given which is possible using App Settings in the Portal Azure Function Configuration Menu but that is regarding to specific slot.
In my local.settings I have nested settings like this
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"Email:Email": "test",
"Email:Password": "*******",
},
}
I am reading the values like this
config.GetValue<string>("Email:Email")
But when I am adding azure settings in azure function app (after deploying) I cannot add : into the name. Any suggestions for it?
One of the workaround you can follow,
When we are creating azure function in local and deploy to azure our localsettings.json file not upload .We need to update them manually by adding it on portal.
In localsettings.json file you have used : which is accepted by local environment. But When deploying we need to use __ instead.
Followed by this MICROSOFT DOCUMENTATION:-
The app setting name like ApplicationInsights:InstrumentationKey
needs to be configured in App Service as
ApplicationInsights__InstrumentationKey for the key name. In other
words, any : should be replaced by __ (double underscore).
Likewise you can set something like below e.g;
"Email__Email": "test",
"Email__Password": "*******",
For more information please refer this SO THREAD| azure application settings - how to add nested item & Nested object from local.settings.json in Azure function v3 settings .
We have an Azure Function project which requires parameters. At the moment, these parameters are stored in Azure Function's application Setting.
To run the azure function locally, we used to do this:
var id = GetEnvironmentVariable("Id");
///var id=12345;
Basically on local machine, we will just uncomment the hard coded line to get the value;
I don't really like it, so I did this:
var config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("settings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
I put all settings into a settings.json and read it from there.
However, as these settings are already available in azure function's appsetting section, I will have to manually delete them from azure portal (or change the arm template).
What is the preferred way to store settings for azure function? In appsetting section or in a config file like I did? Or maybe some different way?
In fact, as Carlos Alves Jorge says, the function app will read settings from local.settings.json on local by default. And on azure it will read settings from configuration settings by default.
By default, publication neither upload local.settings.json to Azure, nor makes modification on Application settings based on that local file, hence we need to update them manually on Azure portal.
If you are using VS to publish your azure function, there is a easy way to change the settings from local to azure.(edit the publish profile.)
Here is the connection string I copied from my Azure portal:
Server=tcp:xxxxxxxxxxx.database.windows.net,9999;Initial Catalog=DbNameDB;Persist Security Info=False;User ID={my_username};Password={my_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
Here is my appsettings.json file:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=HeroesDB;Integrated Security=True;MultipleActiveResultSets=True"
}
}
Now that I want my app to work on a production server, how do I set it up?
Should I ...
...create an appsettings.production.json file in my project (next to my appsettings.json file) and set it's DefaultConnectionString to Azure server's connection string? If I did so, would Azure automatically know which of the two files to use?
...change my original appsettings.json file's connection string to Azure server connection string?
...look for a totally different solution? If yes, what would you suggest?
I'm leaning towards solution no 1., but I want to be sure.
In general appsettings.json is the main settings file, i.e. the production file if you want. All settings that are not found in other child files (e.g. appsettings.development.json) are taken from this appsettings.json.
So it should be the reverse: your appsettings.json should contain your production connection string, and appsettings.development.json a connection string to your local development DB.
Now how do you pick the right settings file. The key is ASPNETCORE_ENVIRONMENT setting. If it is not set, than appsettings.json is picked automatically. If it's set, the app looks for appsettings.{environment}.json file.
If your app runs in Azure portal, you set environment in the settings of your deployed web app. Locally (and I assume you use Visual Studio) there is a launchSettings.json file, found under Properties folder of your project in Solution explorer. There you can create a local launch configuration with the right environment, e.g.:
"my-local": {
"commandName": "Project",
"launchBrowser": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5003/"
}
This will create a "my-local" start option in the dropdown of the "play" button in VS. If you select "my-local" it will pick this settings, from them the "Development" environment, and from it take appsettings.Development.json and from there your connection string.
I hope I got your question right. To sum up again, locally use an evironment configured for appsettings.development.json and local DB. Deploy to Azure and it will pick up the normal appsettings.json file and run against production DB.
In your Azure portal, go to your App, and under Settings, find Configuration.
In the configuration, you can add a connection string. This will override the connection string your json file provided it's the same name.