Contents

Deploy Functions to Azure

After creating a local Azure Functions environment and local Functions project on Mac(M1), I create Azure resources and deploy the Functions.

Environment settings

python-on-rosetta2-m1mac

  • MacOS Monterey 12.3, M1 Apple silicon
  • Rosetta 2
    1
    2
    
    % uname -a
    Darwin xxx 21.4.0 Darwin Kernel Version 21.4.0: Mon Feb 21 20:35:58 PST 2022; root:xnu-8020.101.4~2/RELEASE_ARM64_T6000 x86_64
    
  • pyenv 2.3.18
  • Python 3.10.0
  • Homebrew 4.0.20
  • azure-functions-core-tools@4/4.0.5198

Create a local Azure Functions project

create-local-azure-functions-project

Install Azure cli

Azure CLI requires python3.10. After checking python as 3.10, install over brew.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
% arch
arm64

% pyenv versions
  system
  3.7.9
  3.9.1
* 3.10.0 (set by /Users/xxx/.python-version)
  3.10.9

% python --version

% brew update && brew i

Check cli ersion.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
% az --version
azure-cli                         2.49.0

core                              2.49.0
telemetry                          1.0.8

Dependencies:
msal                              1.20.0
azure-mgmt-resource               22.0.0

Python location '/opt/homebrew/Cellar/azure-cli/2.49.0/libexec/bin/python'
Extensions directory '/Users/tato/.azure/cliextensions'

Python (Darwin) 3.10.11 (main, Apr  7 2023, 07:24:47) [Clang 14.0.0 (clang-1400.0.29.202)]

Legal docs and information: aka.ms/AzureCliLegal

Your CLI is up-to-date.

Login

1
2
3
4
5
% az login
A web browser has been opened at https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
The following tenants require Multi-Factor Authentication (MFA). Use 'az login --tenant TENANT_ID' to explicitly login to a tenant.
xxxxxxxxx-xxxxxx-xxxxx-xxxxxxxxx '既定のディレクトリ'
No subscriptions found for xxxxxxxx@gmail.com.

If you cannot complete login, you may assign Azure role to your account resource group.

Add Azure role for the resource

Ref. : Assign Azure roles using the Azure portal

At portal.azure.com, open the Add role assignment page.

  1. Click Access Control
  2. Click the Role assignments tab to view the role assignments at this scope.
  3. Click Add > Add role assignment.
  4. Select “Privileged administrator roles”
  5. In the Details column, click View to get more details about a role.
  6. Select who needs access

After assigning role, you can login successfully.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
 % az login
A web browser has been opened at https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
[
  {
    "cloudName": "AzureCloud",
    "homeTenantId": "8ce5d0a4-xxxxxx-xxxxxxx-xxxxxxxx",
    "id": "b8b18287-xxxxxx-xxxxxxx-xxxxxxxx",
    "isDefault": true,
    "managedByTenants": [],
    "name": "Azure サブスクリプション 1",
    "state": "Enabled",
    "tenantId": ""8ce5d0a4-xxxxxx-xxxxxxx-xxxxxxxx",
    "user": {
      "name": "beijing.bros@gmail.com",
      "type": "user"
    }
  }
]

Create resource group

Create a resource group “AzureFunctionsQuickstart-rg” on location “Japan East”.

1
% az group create --name AzureFunctionsQuickstart-rg --location "Japan East"

Create storage account

Create generic storage accounts “azstrorage01” within resource groups and regions.

1
az storage account create --name azstrorage01 --location "Japan East" --resource-group AzureFunctionsQuickstart-rg --sku Standard_LRS

Create Functions in Azure

Check functionapp consumption location

1
2
3
% az functionapp list-consumption-locations | grep -i japan 
    "name": "japanwest"
    "name": "japaneast"

Create Functions

  • Resource group: AzureFunctionsQuickstart-rg
  • consumption location: japaneast
  • Python runtime-version: 3.9
  • functions-version: 4
  • name: FunctionProjDemo
  • storage-account: azstrorage01
1
% az functionapp create --resource-group AzureFunctionsQuickstart-rg --consumption-plan-location japaneast --runtime python --runtime-version 3.9 --functions-version 4 --name FunctionProjDemo --os-type linux --storage-account azstrorage01

Once you create the Functions app successfully, you will see the Functions App and Application insight at the Azure portal.

../../../../images/azure/azure-functions-portal.png

Deploying a Function Project to Azure

Once you have successfully created a function app to Azure, you can deploy your local function project using the func azure functionapp publish command.

Check the remote project name:

1
2
3
4
5
6
% az functionapp list --query "[].name"

[
  "FunctionProjDemoHLB"
]

Run the below command using func on Rosetta terminal in my case. At the local project directory (in my case, the directory name is FunctionProjDemo),

1
2
3
4
5
% func azure functionapp publish FunctionProjDemoHLB
...
Remote build succeeded!
Syncing triggers...
Functions in FunctionProjDemoHLB:

To use the Python v2 model in your function app, you must add the value EnableWorkerIndexing to your new application configuration in Azure with the name AzureWebJobsFeatureFlags.

1
az functionapp config appsettings set --name FunctionProjDemoHLB --resource-group AzureFunctionsQuickstart-rg --settings AzureWebJobsFeatureFlags=EnableWorkerIndexing

Then you will see the configured endpoint to be invoked.

1
2
3
4
5
6
7
% func azure functionapp publish FunctionProjDemoHLB
...
Remote build succeeded!
Syncing triggers...
Functions in FunctionProjDemoHLB:
    HttpTrigger1 - [httpTrigger]
        Invoke url: https://functionprojdemohlb.azurewebsites.net/api/hello

Using Function keys

The Invoke URL above still returns 401 Unauthorization error.

Below list the function keys

1
2
3
4
5
6
7
8
% az functionapp keys list -g AzureFunctionsQuickstart-rg -n FunctionProjDemoHLB
{
  "functionKeys": {
    "default": "xxx"
  },
  "masterKey": "xxx",
  "systemKeys": {}
}

Or you can check the same on Azure portal - All resources > (your function) > App key.

For accessing with key, you need specify it by “code” request parameter.

https://functionprojdemohlb.azurewebsites.net/api/hello?code="your function key"

Or you can set it by x-functions-key header.

Then finally, you can get response from deployed function on Azure.

Furthermore, you can use decorator function parameter(@app.route.auth_level) and configure authlevel. If Authlevel is ANONYMOUS, client does not need to use function keys for accessing to the function.

1
@app.route(route="hello", auth_level=func.AuthLevel.ANONYMOUS)

Confirmation on Application Insight

Azure portal’s Application Insights displays real-time streaming logs

1
% func azure functionapp logstream FunctionProjDemoHLB --browser

Reference