3

Does anybody know if its possible to pass / inject a const connection string (or other configuration value) from the Azure Functions Configuration blade?

Take this binding signature for instance:

[Function("APIHttpInput")]
[CosmosDBOutput("%CosmosDb%", "%CosmosContainerOut%", ConnectionStringSetting = "CosmosDBConnection", CreateIfNotExists = true)]
public static async Task<object> RunAsync(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "cosmosin")] HttpRequestData req,
         FunctionContext executionContext)
{
    var logger = executionContext.GetLogger("APIHttpInput");
    logger.LogInformation("C# HTTP trigger function processed a request.");
    // etc...
}

It gets the connection string from local.settings.json, which is fine it works that way file but they're not overwritten in the default CI/CD pipeline and isn't meant to be pushed to production.

I've read the official documentation: https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#customizing-configuration-sources but this doesn't allow utilise them in the binding because they're passed in through DI (which is great, but not quite).

An alternative is to manually write them to Cosmos and use DI. This isn't really what I want because I've got the following function that consumes data:

[Function("APICosmosTrigger")]
//[CosmosDBOutput("%CosmosDb%", "%CosmosContainerOut%", ConnectionStringSetting = "CosmosDBConnection", CreateIfNotExists = true)]
public static void RunCosmos([CosmosDBTrigger(
            databaseName: "%CosmosDb%",
            collectionName: "%CosmosContainerOut%",
            ConnectionStringSetting = "CosmosDBConnection",
            LeaseCollectionName = "leases")] IReadOnlyList<Record> leads,
            FunctionContext executionContext,
            IRestClient client)
{
     // do stuff...
}

The end result would hopefully be that the binding fields (below) can be configured through the configuration blade:

  • "%CosmosDb%"
  • "%CosmosContainerOut%"
  • ConnectionStringSetting = "CosmosDBConnection"

PS: I've seen a few questions very close to this, but aren't dealing with the binding attribute issue.

1 Answer 1

3

All Functions binding (Cosmos, Storage, EventHub, etc) are governed by the same replacement mechanics for the values in the configs, also known as the binding expressions.

Normally the local.settings.json file is not pushed/published, because it's local to the environment.

You could, in your DevOps pipeline, have Environment Variables that override the local.settings.json values for example.

The article you link is initializing the config like so:

builder.ConfigurationBuilder
                .AddJsonFile(Path.Combine(context.ApplicationRootPath, "appsettings.json"), optional: true, reloadOnChange: false)
                .AddJsonFile(Path.Combine(context.ApplicationRootPath, $"appsettings.{context.EnvironmentName}.json"), optional: true, reloadOnChange: false)
                .AddEnvironmentVariables();

So if you define an Environment Variable in DevOps, you should be able to override whatever is defined in the json file.

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

5 Comments

Unfortunately, I'd rather not use Azure DevOps pipeline and trying to keep it simple with direct CI/CD but I did consider that. Secondly, those appsettings files arent going to work like I mentioned because the values arent const (i.e. try to use it in the same binding, VS will complain its not const). Thanks for the answer though, its such a shame that MS is so close yet so far.
Not sure I understand. Where are you running your CI/CD? You should be able to define Environment Variables in your CI/CD pipeline just fine. I used DevOps as an example of one CI/CD pipeline, but most of the ones I'm aware of allow this.
Specifically Azure CI/CD (i.e. straight integration from the repository). Theres no way, that I know of unless its the built in replacement(i.e. the appsettings token replacement is done by default on there) - which doesnt work on local.settings.json (and production.settings.json doesnt exist). I'm really just trying to keep it simple and work with the built in tools without requiring a lot of manual pipeline work. And again, this wont work because they're computed at runtime and not consts.
Function binding connection configurations (for any trigger/binding) simply don't work on consts, the value you put there is afterwards, read from configuration sources (environment, files, settings on the Function app). I am not sure on Azure CI/CD and it's support for environment variables, if what you are doing is using the link to repo (from App Service for example) you could have a Staging Slot, or if deploying from GitHub, customize that flow (learn.microsoft.com/azure/app-service/…)
I'm sorry I really don't understand what you're saying and I'm unsure if you understand what I'm trying to write. Thanks for trying though, I'll see if someone else has any idea to how to solve it within the bounds of the question.

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.