0

Using VS Code + "Azure Function" Extension, I generated the default python 3.7 timedTrigger function with the following settings:

// functions.json
{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "mytimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 0 */6 * * *"
    }
  ]
}

I have also set up two environment variables "USER" and "PASSWORD" which are set up in the Configuration of the app service.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": ****************,
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "USER": "********",
    "PASSWORD": "*********"
  }
}

Goal: I want to run two instances of the same function, but using two different Configs, i.e. Users+Passwords.

Problem: I believe that the Configuration/App Settings might not be sufficient for this. I can't find a way to run the function twice with multiple different parameters.

Question: What options do I have to reach my goal? One idea I had was to put the User/PW into the functions.json, but I could not figure out how to access that information from within the app function.

atpekbas
  • 31
  • 4

3 Answers3

1

You have two options:

  1. Read a custom json (not necessarily reading the value of function.json), you can add a custom json in the function app, and then read the value you want according to the hierarchy of the json file, Then use the value you read in the trigger.

  2. Use deployment slot. (This is the official method, I think it is completely suitable for your current needs)

enter image description here

In this newly created slot you can use completely different environment variables in Configuration Settings.

This is the doc:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-deployment-slots

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • The official way did work. However, free development slots are limited to two and I need more. So I ended up doing what I wrote in the solution comment. – atpekbas Jun 18 '20 at 20:15
0

I'd probably do it by having a single setting that holds a JSON array, viz

"Credentials": "[{'username':'***','password':'***'},{'username':'******','password':'******'}]"

Then, assuming you want to process them all at the same time, make a single function that parses the array and iterates over each username and password.

If you need to run them on different schedules, create a shared Python function DoTheThing(credentialIndex) that actually does the work and then multiple Azure Functions that simply call DoTheThing(0), DoTheThing(1), ...

(Security note: not immediately relevant to the problem at hand, but secrets are best kept in a secret store such as Key Vault rather than directly in the settings)

MarkXA
  • 4,294
  • 20
  • 22
0

EDIT/SOLUTION: I ended up having a the following keys in my environment variables:

"USERS": "[\"UserA\", \"UserB\"]"
"UserA_USER": "Username1"
"UserA_PW": "Password1"
"UserB_USER": "Username2"
"UserB_PW": "Password2"

Then I iterated over the USERS array and retrieved the keys for each user like so:

import os
import json

users = json.loads(os.environ["USERS"])
for u in users:
   user = os.environ[u + "_USER"]
   pw = os.environ[u + "_PW"]
   doStuff(user, pw)
atpekbas
  • 31
  • 4