简体   繁体   中英

How to set environment variables or inputs in timerTrigger Azure Functions?

I am trying to setup a timerTrigger azure function

My function.json :

{
    "disabled": false,
    "bindings": [
        {
            "type": "timerTrigger",
            "direction": "in",
            "name": "sampleCronTrigger",
            "schedule": "*/5 * * * * *",
        }
    ],
    "entryPoint": "sampleCron",
    "scriptFile": "index.js"
}

In this I need to set an environment variable, but I am not able to do so. I tried looking for some documentation but couldn't find anything which doesn't require some setup on the Azure console?

I can I define environment variables? Or If here is any way I can pass an input to the function, that works too.

App settings in a function app contain global configuration options that affect all functions for that function app. When you run locally, these settings are accessed as local environment variables .

Local settings file The file local.settings.json stores app settings, connection strings, and settings for Azure Functions Core Tools. Settings in the local.settings.json file are only used by Functions tools when running locally. 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.

In Functions, app settings, such as service connection strings, are exposed as environment variables during execution. You can access these settings using process.env, as shown here in the GetEnvironmentVariable function:

module.exports = function (context, myTimer) {
    var timeStamp = new Date().toISOString();

    context.log('Node.js timer trigger function ran!', timeStamp);
    context.log(GetEnvironmentVariable("AzureWebJobsStorage"));
    context.log(GetEnvironmentVariable("WEBSITE_SITE_NAME"));

    context.done();
};

function GetEnvironmentVariable(name)
{
    return name + ": " + process.env[name];
}

There are several ways that you can add, update, and delete function app settings:

When running locally, app settings are read from the local.settings.json project file.

References:

Additionally for retrieving values from local.settings.json, another way is to create an IConfigurationRoot object using ExecutionContext executionContext .

ExecutionContext can be added to function definition:

[FunctionName("FunctionName")]
        public static async Task Run(
            [ServiceBusTrigger(...)]
            SomeMessage msg,
            ILogger log,
            ExecutionContext executionContext)
{
}

After that, you can instantiate an IConfigurationRoot instance, which you instruct to optionally load local.appsettings.json.

  var configurationRoot = new ConfigurationBuilder()
            .SetBasePath(executionContext.FunctionAppDirectory)
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();

With the configurationRoot object, you can retrieve configuration values:

 var value = configurationRoot["SomeKey"];

Example local.settings.json:

{
 "IsEncrypted": false,
 "Values": {
  "AzureWebJobsStorage": "...",
  "FUNCTIONS_WORKER_RUNTIME": "dotnet",
  "SomeKey": "Value",
},
"Host": {
 "LocalHttpPort": "7071"
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM