Azure function nested configuration - azure

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 .

Related

Why I am not seeing all properties available in local.settings.json file after deploying Azure function

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.

C# Azure Functions project with ServiceBusTrigger connection issue

I have creating some Azure Functions in a C# project that are working fine locally. An example of the definition of a function is the following:
[FunctionName("createBankTransactionFromServiceBus")]
public async Task Run(
[ServiceBusTrigger("vspan.sbus.xerobanktransaction.requests", "requests",
Connection = "AccountingServiceBusConnection")] string myQueueItem)
{
}
Nothing different than usual. The problem is when I deploy this function on Azure. On Azure, the Azure Functions can't find the connection string. So, I added a new one in the local.settings.json but now I have two AccountingServiceBusConnection with the same value, one for my local machine and one for Azure.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"AccountingServiceBusConnection": "connectionString"
},
"AccountingServiceBusConnection": "connectionString"
}
I tried to replace the connection in the signature of the function like:
[FunctionName("createBankTransactionFromServiceBus")]
public async Task Run(
[ServiceBusTrigger("vspan.sbus.xerobanktransaction.requests", "requests",
Connection = "%Values:AccountingServiceBusConnection%")] string myQueueItem)
{
}
but locally I have a warning (with or without %).
Warning: Cannot find value named
'Values:AccountingServiceBusConnection' in local.settings.json that
matches 'connection' property set on 'serviceBusTrigger' in
'C:\Projects\fun\bin\Debug\netcoreapp3.1\createBankTransactionFromServiceBus\function.json'.
You can run 'func azure functionapp fetch-app-settings
' or specify a connection string in
local.settings.json.
Also, I tried to move AccountingServiceBusConnection under ConnectionStrings with the same result.
Update
Screenshot of Kudu and local.settings.json
Screenshot of Azure Functions configuration
How can you configure a pipeline in DevOps? How do you store the configuration from DevOps in the configuration in your Azure Functions?
There's no local.settings.json on Azure, you must add the settings to your Azure App Services settings:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings
EDIT:
for Key Vault Integration you must assign a managed identity to your function:
Then use Key Vault Ingegration:
#Microsoft.KeyVault(SecretUri={theSecretUri})
More info:
https://medium.com/statuscode/getting-key-vault-secrets-in-azure-functions-37620fd20a0b
By default, the local.settings.json file is NOT deployed with your code to an Azure Function. See the documentation here:
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 have a few options:
Explicitly publish your local.settings.json file with the aforementioned command line arg.
Add this setting (and any other settings needed) to your Azure Function's Configuration. Those values defined in your app settings in the Azure Portal take precedence over everything else.
I don't recommend option #1, because it requires you to place production values in your source code, which is in general a bad idea.
Updated - how to configure with Azure DevOps
For Azure DevOps, we've taken a two pronged approach.
We place the bare minimum key/value pairs in the Azure Function configuration. These are added into our yaml deployment pipeline. Some variable values (like connection strings) are read from other resources at deploy time so that sensitive info isn't included in our yaml script that is checked into revision control. Here's some example yaml for deploying an Azure Function:
{
"apiVersion": "2016-03-01",
"type": "Microsoft.Web/sites",
"name": "FooBarFunction",
"location": "[resourceGroup().location]",
"kind": "functionapp",
"dependsOn": [
"[resourceId('Microsoft.Web/serverfarms', "YourHostingPlanName")]"
],
"properties": {
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', "YourHostingPlanName)]",
"siteConfig": {
"appSettings": [
{
"name": "WEBSITE_CONTENTSHARE",
"value": "FooBarFunctionContentShare"
},
{
"name": "FUNCTIONS_WORKER_RUNTIME",
"value": "dotnet"
}
]
}
}
}
We use Azure App Configuration service to hold all of our other app settings. This gives us the advantage of defining different config profiles, and also having hot reload of our app settings without having to recycle the Azure Function. It also plays nicely with Keyvault for sensitive settings.

Azure Functions- Publish local.settings.json

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

Azure WebApp Containers and AppSettings/Environment Variables

I've created an AzureWebApp running a docker image. The app starts, but it does not appear to be getting a connection string. I've defined the connection string as an AppSetting but I am not seeing that setting passed as an environment variable.
Should I expect to see my AppSetting on the container output? Something like
docker run -e CONNSTR=FOO
The docs imply that it should just be passed automatically, but I feel like I'm missing something.
Thanks
Joe
Turns out it was working all along. I had a legit issue with authenticating against the db. I needed some logging in the service to verify that it was getting the right connection string.
It appears that the app settings are passed to the container implicitly without showing up in the logs as an -e param.
I run into a similar issues when deploying an .NET Core 2 Azure Function, which was reading settings and connection strings using System.Configuration.ConfigurationManager. The root cause in my case was that ASP.NET Core introduced a new configuration API.
Find more details at https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.0&tabs=basicconfiguration.
Assuming you're deploying an ASP.NET Core application deployed into Web Apps for Containers, you may be running into a similar issue. Please try to the following:
a) Initialize the configuration as follows:
using Microsoft.Extensions.Configuration;
...
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("local.settings.json")
.AddEnvironmentVariables()
.Build();
b) Read connection strings as follows:
configuration.GetConnectionString("StorageAccountConnectionString");
c) Read settings as follows:
configuration["ContainerName"];
Here is a sample configuration file for my Azure Function:
{
"IsEncrypted": true,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"ContainerName": "container"
},
"Host": {
"LocalHttpPort": 7071
},
"ConnectionStrings": {
"StorageAccountConnectionString": "UseDevelopmentStorage=true"
}
}

Azure Functions: Configuration file for referenced assembly

We have a referenced project in azure function app project. The referenced assembly is a data service project which is referred to by web api project too.
When referenced in web-api project the data service project automatically refers to web.config file for connection strings and app settings. While in azure functions app the data service project is not able to locate the connection strings stored in local.settings.json file.
How to address this issue locally?
How to address the issue in production?
NOTE: Would like to have DRY approach here.
As Jan V said, you could add a data connection string in json file. Besides, you could set a break point to see whether you get the 'str' value (Debug).
var str = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
Code in local.settings.jons file:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage":
"your storage account connection string",
"AzureWebJobsDashboard":
"your storage account connection string"
},
"ConnectionStrings": {
"ConnectionStringName": "Data Source=tcp:database server name,1433;Initial Catalog=database name;Integrated Security=False;User Id=user name;Password= your Password;Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True" // Refer to Azure portal>SQL database> connection string
}
}
For more details about how to use Azure Functions to connect to an Azure SQL Database ,you could read this article.

Resources