Azure Functions Basics with Python

Get Started. It's Free
or sign up with your email address
Azure Functions Basics with Python by Mind Map: Azure Functions Basics with Python

1. Hello World: First Azure Function in Python

1.1. Prerequisites:

1.1.1. An Azure account with an active subscription

1.1.2. Install the Azure Functions Core Tools

1.1.2.1. I installed the Core Tools v3.x Windows 64-bit installer

1.1.2.2. This provides a virtual environment for running and debugging your Azure functions on your local machine prior to deploying into an Azure Function App

1.1.3. Python 3.8, 3.7 or 3.6

1.1.3.1. Check for latest on Python versions supported by Azure Functions

1.1.3.2. To check for installed versions of Python in Windows run the following in PowerShell (or Windows cmd):

1.1.3.2.1. py -0p

1.1.4. Visual Studio Code

1.1.5. Python extension for Visual Studio Code

1.1.6. Azure Functions extension for Visual Studio Code

1.2. In VS Code, under Azure and Functions section, create a new project

1.2.1. Choose a local directory for your project

1.2.1.1. I created a new Azure DevOps Git repo named argento-azure-functions and I cloned this locally, putting the new project inside a subdirectory named hello-world

1.2.2. Set the language for the project to Python

1.2.3. Select the location for your Python interpreter, which creates an alias for a new virtual environment

1.2.4. Select HTTP trigger for the template

1.2.5. Name the function

1.2.5.1. I named mine HttpArgentoHelloWorld

1.2.6. Set authorization level to Anonymous

1.2.6.1. Anonymous allows anyone to call your function endpoint

1.2.6.2. For more private functions, we are more likely to choose the Function option, which sets up a function access key

1.2.6.2.1. However, using a function access key is a shared secret model that is not actually considered best practice, and there are multiple preferred options for securing Production HTTP endpoints, whilst still leaving your authorization level as anonymous

1.2.7. Choose option: Add to workspace

1.2.8. A new __ini__.py file should be created with boiler plate code from the template and you may see the "Waiting for Azure sign-in" prompt

1.2.8.1. After clicking on the Azure sign-it, it will launch a web browser window that prompts to authenticate to Azure and once completed, you should see your subscriptions listed

1.3. Debug project locally

1.3.1. Visual Studio Code integrates with Azure Functions Core tools to let you run this project on your local development computer before you publish to Azure

1.3.2. With your __init__.py script file the focus, press F5 to start the debugger (or start via Run menu) and check that the terminal output shows the worker process is started and initialized, and a local function endpoint URL is displayed

1.3.2.1. Note: the local function endpoint in this case is:

1.3.2.1.1. http://localhost:7071/api/HttpArgentoHelloWorld

1.3.2.2. If you have trouble running on Windows, make sure that the default terminal for Visual Studio Code isn't set to WSL Bash

1.3.3. Test the function locally

1.3.3.1. With the endpoint running locally, we can now test the function

1.3.3.2. With Core Tools running, go to the Azure: Functions area, expand Local Project | Functions, then right-click your function and choose: Execute Function Now...

1.3.3.2.1. The request body pops up in JSON format: press Enter to send it

1.3.4. Halt debugging

1.3.4.1. Click inside the Terminal window to ensure it has focus

1.3.4.1.1. Press Ctrl + C to stop Core Tools and disconnect the debugger

1.4. Publish Azure function

1.4.1. Choose the Azure icon in the Activity bar, then in the Azure: Functions area, choose the Deploy to function app... button

1.4.1.1. Note that the Deploy to function app button (the up arrow) only appears when you select the function or hover the mouse over it

1.4.1.2. Provide the following information at the prompts:

1.4.1.2.1. Select folder: Choose a folder from your workspace or browse to one that contains your function app

1.4.1.2.2. Select subscription: Choose the subscription to use

1.4.1.2.3. Select Function App in Azure: Choose + Create new Function App in Azure...

1.4.1.2.4. Enter a globally unique name for the function app: Type a name that is valid in a URL path

1.4.1.2.5. Select a runtime: Choose the version of Python you've been running on locally

1.4.1.2.6. Select a location for new resources: For better performance, choose a region near you

1.4.1.3. Progress output should appear in bottom right of screen, and after a couple of minutes you should see a successful deployment message

1.4.1.4. When completed, the following Azure resources are created in your subscription, using names based on your function app name:

1.4.1.4.1. A resource group, which is a logical container for related resources

1.4.1.4.2. A standard Azure Storage account, which maintains state and other information about your projects

1.4.1.4.3. A consumption plan, which defines the underlying host for your serverless function app

1.4.1.4.4. A function app, which provides the environment for executing your function code

1.4.1.4.5. An Application Insights instance connected to the function app, which tracks usage of your serverless function

1.4.1.4.6. By default, the Azure resources required by your function app are created based on the function app name you provide, and also by default they are also created in the same new resource group with the function app

1.4.1.5. Select View Output in this notification to view the creation and deployment results, including the Azure resources that you created

1.4.1.5.1. If you miss the notification, select the bell icon in the lower right corner to see it again

1.4.1.5.2. The output should confirm successful deployment and also show the public endpoint for your newly deployed Azure function

1.5. Run function in Azure

1.5.1. In VS Code, go to Azure: Functions area and locate your function now inside your subscription, then right right and choose: Execute Function Now...

1.5.1.1. When prompted for the "name" input parameter, try changing it from "Azure" to another name and press enter

1.5.1.2. You should see a success response returned, which pops up in the bottom right of the screen

2. Triggers and bindings

2.1. Triggers are what cause a function to run

2.2. A trigger defines how a function is invoked and a function must have exactly one trigger

2.3. Triggers have associated data, which is often provided as the payload of the function

2.4. Binding to a function is a way of declaratively connecting another resource to the function

2.4.1. bindings may be connected as input bindings, output bindings, or both

2.4.2. A trigger is a special type of input binding

2.5. Data from bindings is provided to the function as parameters

2.6. You can mix and match different bindings to suit your needs

2.6.1. Bindings are optional and a function might have one or multiple input and/or output bindings

2.7. Bindings let you avoid hardcoding access to other services

2.8. Your function receives data (for example, the content of a queue message) in function parameters

2.9. You send data (for example, to create a queue message) by using the return value of the function

2.10. See attached screenshot for some examples of how you could implement different functions

2.11. Triggers and bindings are defined differently depending on the development language

2.11.1. For Python, triggers and bindings are defined in the function.json file

2.11.1.1. All triggers and bindings have a direction property in the function.json file

2.11.1.1.1. For triggers, the direction is always in

2.11.1.1.2. Input and output bindings use in and out

2.11.1.1.3. Some bindings support a special direction inout

2.11.1.2. You can connect your function to other services by using input or output bindings

2.11.2. You can create custom input and output bindings

2.11.2.1. Bindings must be authored in .NET, but can be consumed from any supported language

3. Extending the simple HTTP Trigger function

3.1. The Hello World function (auto created by choosing the HTTP trigger template) is very simple as it is triggered by a basic HTTP Get or Post request, accepts a single input parameter (name) and returns a single output parameter (a trivial text message) as an HTTP payload

3.1.1. To have your function interact with Azure resources, we will need additional bindings beyond the trigger and HTTP return payload

3.1.1.1. It's common for a binding to need a connection string in order to connect and authenticate to a bound Azure service, so we'll explore how to bind to a storage account as one of the most common examples

3.2. The connection settings for the storage account that we deployed as part of our function app is held securely for us in Application Settings

3.2.1. In VS Code, we can access the Application Settings for the function app and the connection details for the storage account are held in an application setting named: AzureWebJobsStorage

3.2.1.1. If you click the eye icon you can toggle visibility for the application setting on and off, which uses your Azure sign-in credentials for authorization, and you can see sensitive details here like the account name and account key

3.3. As a further pre-requisite, we need to install the Azure Storage extension into VS Code

3.4. Whilst in the Azure: Functions section of VS Code, press F1 to open the command palette, then search for and run the command Azure Functions: Download Remote Settings....

3.4.1. Select subscription

3.4.2. Select the function app (i.e. the one we created in our "Hello World" deployment)

3.4.3. Select the appropriate local.settings.json file as the destination for the downloaded settings

3.4.3.1. Pay attention to the path to be sure you'll be overriding the appropriate local.settings.json file

3.4.3.2. In my case, I had two local Azure projects, so needed to choose the one for Azure Functions

3.4.4. Confirm local overwrite by clicking "Yes to all"

3.5. After downloading the remote settings into our local.settings.json file, we can see all the storage account details in our local file

3.5.1. We can see that this includes secrets, which is an obvious concern when we're going to be publishing from our local machines and committing code to Git repos that are pushed up to Azure DevOps

3.5.1.1. However, because it contains secrets, the local.settings.json file never gets published, and is excluded from source control

3.5.1.1.1. We can satisfy ourselves about this by inspecting two other project files:

3.5.1.1.2. The drawback is that when other developers clone the Git repo and need to do work on the Azure functions for that function app, they will need to repeat the running of the Azure Functions: Download Remote Settings command

3.6. As a prerequisite for adding a storage output binding to our function, we need the Storage bundles extension to be installed

3.6.1. Because our host.json project file has been configured to use extension bundles, this automatically installs a predefined set of extension packages, including the required Storage bundles extension

3.7. In VS Code, right click the function.json project file and choose the option: Add binding...

3.7.1. Select binding direction = out

3.7.2. Select binding = Azure Queue Storage

3.7.3. Set name to identify binding in code = msg

3.7.3.1. Note: this could be any valid name for a Python variable

3.7.4. Set queue name = outqueue

3.7.4.1. Note: this could be any valid name for an Azure Storage Account queue

3.7.5. Select setting from local.setting.json = AzureWebJobsStorage

3.7.6. Now our function.json project file has the new output binding for the storage queue

3.7.6.1. The extension bundles made the process of adding this binding nice and simple

3.8. Now we need to add some code to our __init__.py script file that uses the new binding

3.8.1. Here's our __init__.py script before the changes

3.8.2. Here's our __init__.py script after the changes

3.8.2.1. We added msg to the main function definition as a 2nd parameter (output), typed as a queue message

3.8.2.1.1. msg: func.Out[func.QueueMessage]

3.8.2.2. We changed the main function annotated return type from an HTTP Response object to a string (str)

3.8.2.2.1. I'm not sure if this technically makes any difference, but is probably done to reflect the fact that the primary output of the function is now a string destined for a storage queue

3.8.2.3. We add an invocation of the set method on the msg object to write the supplied name argument to the storage queue

3.8.2.3.1. msg.set(name)

3.8.2.3.2. The msg parameter is an instance of the azure.functions.InputStream class

3.8.2.3.3. Its set method writes a string message to the queue, in this case the name passed to the function in the URL query string

3.9. Test locally

3.9.1. Start the debugger by pressing F5

3.9.1.1. You can also start the debugger via Run menu

3.9.1.2. This causes botht eh function app project to start and also Core Tools

3.9.1.2.1. Remember that Core Tools is the thing that let's you run Azure project code in your local machine environment (i.e. it provides a virtual environment for running the code that is integrated with VS Code)

3.9.2. Go to Azure: Functions and under Local Project, right click your function and choose the option: Execute Function Now...

3.9.2.1. Set name when prompted

3.9.2.1.1. I set it to "Ian"

3.9.2.2. Confirmation should pop up in bottom right of screen to confirm the function executed

3.9.3. Press Ctrl+C to halt debugger (and stop Core Tools)

3.9.4. Using Storage Explorer, check that the message was written correctly to the queue

3.10. Deploy to Azure and re-test function

3.10.1. In VS Code, press F1 (to open the command palette) and run command:

3.10.1.1. Azure Functions: Deploy to function app...

3.10.1.1.1. Select the folder

3.10.1.1.2. Select subscription

3.10.1.1.3. Select function app

3.10.1.1.4. Confirm the deployment when prompted

3.10.2. Deployment should be confirmed via message in bottom right of screen

3.10.3. Repeat test in the same way as for local test, with only difference being that you don't start the VS Code debugger first and you browse to your Azure subscription under Azure: Functions, not the local project

3.10.3.1. I decided to execute my function and pass the name as "Favorita"

3.10.3.1.1. Storage Explorer confirms that the Azure function wrote the new message to the queue successfully!

4. Azure Functions development process

4.1. Development of an Azure Function typically happens on a developer's local machine using VS Code

4.2. The pre-requisites and are covered via the Hello World notes

4.3. The process to create a new project is also covered via the Hello World notes

4.3.1. The only thing to note is that whilst the HTTP trigger is the most common, others are available such as Azure Blob Storage trigger, Azure Queue Storage trigger and Timer trigger

4.4. Generated project files

4.4.1. A number of project files are generated when you create a new Azure Functions project in Python (some common to all languages, some specific to Python):

4.4.1.1. host.json

4.4.1.1.1. Common to all languages, this file lets you configure the Functions host

4.4.1.1.2. These settings apply when you're running functions locally and when you're running them in Azure

4.4.1.2. local.settings.json

4.4.1.2.1. Common to all languages, this file maintains settings used when you're running functions locally

4.4.1.2.2. These settings are used only when you're running functions locally

4.4.1.2.3. Because the local.settings.json file can contain secrets, you need to exclude it from your project source control

4.4.1.3. requirements.txt

4.4.1.3.1. Python specific, this is a project-level file that lists packages required by Functions

4.4.1.4. function.json

4.4.1.4.1. Python specific, this file is placed inside a project subfolder named after your function

4.4.1.5. __init__.py

4.4.1.5.1. Python specific, this file is placed together with the function.json file in the function subfolder of the project and is referenced by function.json

4.4.1.5.2. function.json + __init__.py together represent the Azure function code

4.4.2. Folder structure for a Python Azure Function app

4.4.2.1. As well as the project level files host.json, local.settings.json and requirements.txt, and the function subfolder, there are a few additional project files and subfolders

4.4.2.1.1. .vscode/

4.4.2.1.2. .venv/

4.4.2.1.3. .funcignore

4.4.2.1.4. .gitignore

4.4.2.1.5. proxies.json

4.5. Install bindings

4.5.1. Except for HTTP and timer triggers, bindings are implemented in extension packages

4.5.2. You must install the extension packages for the triggers and bindings that need them

4.5.3. The process for installing binding extensions depends on your project's language

4.5.3.1. For Python, the easiest way to install binding extensions is to enable extension bundles

4.5.3.1.1. When you enable bundles, a predefined set of extension packages is automatically installed

4.5.3.1.2. To enable extension bundles, open the host.json file and update its contents to match the following code:

4.6. Add a function to your project

4.6.1. You can add a new function to an existing project by using one of the predefined Functions trigger templates

4.6.2. To add a new function trigger, select F1 to open the command palette, and then search for and run the command:

4.6.2.1. Azure Functions: Create Function

4.6.3. Follow the prompts to choose your trigger type and define the required attributes of the trigger

4.6.4. If your trigger requires an access key or connection string to connect to a service, get it ready before you create the function trigger

4.6.5. In Python, the result is a new subfolder in your project

4.6.5.1. The folder contains a new function.json file and the new Python code file (__init__.py)

4.7. Connect to services

4.7.1. You can connect your function to other Azure services by adding input and output bindings

4.7.2. Bindings connect your function to other services without you having to write the connection code

4.7.3. Visual Studio Code lets you add bindings to your function.json file by following a convenient set of prompts

4.7.3.1. Remember that function.json is specific to Python

4.7.4. To add a binding, open the command pallet (F1) and type:

4.7.4.1. Azure Functions: Add Binding

4.7.5. Choose the function for the new binding, and then follow the prompts, which vary depending on the type of binding being added to the function

4.7.5.1. The type of bindings you can add are limited, and depend on the selected direction

4.7.5.1.1. in options include:

4.7.5.1.2. out options include:

4.7.5.2. Here's an example of a JSON object added to the bindings array of the function.json file by the add binding command:

4.7.5.2.1. { "type": "queue", "direction": "out", "name": "msg", "queueName": "outqueue", "connection": "AzureWebJobsStorage" }

4.7.5.3. The binding may prompt the creation of a connection string in local.settings.json

4.7.5.3.1. For example, AzureWebJobsStorage references an application setting that will be added to local.settings.json

4.7.5.3.2. Remember that the local.settings.json file is not published, so you'll need to set these values up using the Azure Portal after deploying the function app

4.7.6. Update the Main definition (in the __init__.py file) to add the input or output parameter corresponding to the binding

4.7.6.1. To continue with the out(put) queue storage binding example, our main definition would change to:

4.7.6.1.1. def main(req: func.HttpRequest, msg: func.Out[func.QueueMessage]) -> str:

4.7.6.1.2. Note that the output parameter added is:

4.7.6.1.3. Note also that we've given the output parameter the name of "msg" in this example

4.7.7. Within __init__.py, add one or more appropriate method calls to the bound input/output object

4.7.7.1. To continue with the out(put) queue storage binding example, we can add this to the function:

4.7.7.1.1. msg.set(name)

4.7.7.1.2. Note that set() is an available method for objects typed func.Out[func.QueueMessage] and name references a string parameter fetched as part of the HTTP in(put) trigger

4.7.8. Follow link for more on how to learn more in general about which bindings can be added to a function

4.8. Sign in to Azure

4.8.1. Before you can publish your app, you must sign in to Azure

4.8.2. If you aren't already signed in, choose the Azure icon in the Activity bar, then in the Azure: Functions area, choose Sign in to Azure....

4.8.2.1. When prompted in the browser, choose your Azure account and sign in using your Azure account credentials

4.8.3. The subscriptions that belong to your Azure account are displayed in the Side bar

4.9. Publish to Azure

4.9.1. Visual Studio Code lets you publish your Functions project directly to Azure

4.9.2. In the process, you create a function app and related resources in your Azure subscription

4.9.3. The function app provides an execution context for your functions

4.9.4. When you publish from Visual Studio Code to a new function app in Azure, you can choose either a quick function app create path using defaults or an advanced path

4.9.4.1. The advanced path gives you more control over the remote resources created

4.9.4.1.1. Press F1 to bring up the command pallet, and enter:

4.9.5. For republishing, you can simply repeat the process from Visual Studio Code, but for team projects it is recommended to set up a CI/CD pipeline so that any pull requests into your collaboration branch trigger and automated build and release process

4.10. Get URL for function

4.10.1. To call an HTTP-triggered function from a client, you need the URL of the function when it's deployed to your function app

4.10.1.1. For example, to call an Azure Function from Azure Data Factory, I will need the appropriate URL

4.10.1.2. This URL includes any required function keys

4.10.1.3. You can use the extension to get these URLs for your deployed functions

4.10.1.3.1. Press F1 to open the command palette, and then search for and run the command:

5. POC: Using Azure Function with Azure SDK: ADLS Gen2 Storage

5.1. The idea for this POC is to use the Azure SDK to have our Azure Function enumerate a list of containers (file systems) held within an ADLS Gen2 storage account

5.1.1. The Azure SDK for Python is divided into two library types: Client libraries and Management libraries

5.1.1.1. In our case, we are interested in the following client library: Storage - Files Data Lake

5.2. Additional prerequisites

5.2.1. All the same prerequisites apply as for our Hello World function, but we have one more:

5.2.1.1. Azure CLI Tools extension for VS Code

5.2.1.1.1. In VS Code, go to Extensions and search for Azure CLI Tools, then click the Install button

5.2.1.1.2. Go to the page for installing Azure CLI locally and following the link for the Windows installation

5.2.1.1.3. Once the Azure CLI installer is complete, we can verify the installation via Windows command or Powershell

5.2.1.1.4. The reason we need Azure CLI locally is primarily for some later steps to create a user service principle in Azure that represents the developer running an application from his own machine, and this service principle will be used for the local debugging of our Azure Function

5.3. In our local hello-world project, we'll create a new function

5.3.1. Go to Azure : Functions, and click the option to Create Function...

5.3.1.1. Follow the steps to create a new HTTP trigger function but for authorization level, we'll choose Function this time instead of Anonymous

5.4. As per the SDK documentation, we need to create a DataLakeServiceClient object in order for the function to interact with one of our ADLS Gen2 storage accounts

5.4.1. To create a new DataLakeServiceClient object, we need two input parameters

5.4.1.1. URI for the storage account, passed as a string

5.4.1.2. A credential, which can be any of the following:

5.4.1.2.1. SAS token string

5.4.1.2.2. Account shared access key

5.4.1.2.3. Token credential from the azure.identity client library

5.4.2. We will create a token credential, so we need to install two Python libraries using the pip command:

5.4.2.1. pip install azure-identity

5.4.2.2. pip install azure-storage-file-datalake --pre

5.4.2.3. Let's do this from inside VS Code by adding a new terminal (via the Terminal menu)

5.4.2.3.1. When prompted, let's choose our hello-world project folder as the working directory for the new terminal

5.4.2.3.2. If we switch the terminal to PowerShell Integrated, we can check our installed version(s) of Python and the version of pip installed using the following commands:

5.4.2.3.3. We install the Azure Identity client library first

5.4.2.3.4. Then we install the Azure Datalake client library

5.5. Add an environment setting for the storage account, and replicate this in our local.settings.json file

5.5.1. In Azure portal, navigate to our Azure Function App and under Configuration | Application settings, click the button: New application setting

5.5.1.1. We add a new entry, which I chose to name: StorageAccountName

5.5.1.1.1. To get the value for the data lake service, we can go to the Properties of the storage account in Azure portal

5.5.2. We add the same setting to our local.settings.json file inside VS Code

5.6. Setup for local authentication and authorization in Azure

5.6.1. We will need a service principal for our Azure Function when running it locally in VS Code

5.6.1.1. Note: when the function runs in Azure, we'll be using managed identity, not the service principal

5.6.2. In VSCode, we start a terminal and run the following Azure CLI commands:

5.6.2.1. az login

5.6.2.2. az ad sp create-for-rbac --name ian-bradshaw-az-func-sp --skip-assignment

5.6.2.2.1. Note: every developer will need their own service principal and a common convention is to include the developer name in the name of the new sp

5.6.2.2.2. If you don't give a name to your service principal, it will get a system assigned named that begins "azure-cli" followed by a timestamp

5.6.2.3. We get some important values returned to us in the terminal window for the new service principal, which we need to copy

5.6.2.3.1. appId

5.6.2.3.2. password

5.6.2.3.3. tenant

5.6.3. In VS Code, we'll edit our local.settings.json file and add 4 entries in the values section:

5.6.3.1. AZURE_SUBSCRIPTION_ID

5.6.3.1.1. Get the value for this from Azure portal | Subscriptions

5.6.3.2. AZURE_TENANT_ID

5.6.3.2.1. Copy this from the tenant value returned by the az ad sp command

5.6.3.3. AZURE_CLIENT_ID

5.6.3.3.1. Copy this from the appId value returned by the az ad sp command

5.6.3.4. AZURE_CLIENT_SECRET

5.6.3.4.1. Copy this from the password value returned by the az ad sp command

5.6.4. Assign service principal an RBAC role for the storage account using Azure portal

5.6.4.1. In this case, we choose the built-in role: Storage Blob Data Contributor

5.7. Edit our function code in the __init__.py script file using VS Code

5.7.1. First, we'll add the import statements for creating the credential object and the data lake client object

5.7.1.1. from azure.identity import DefaultAzureCredential

5.7.1.2. from azure.storage.filedatalake import DataLakeServiceClient

5.7.2. Add credential

5.7.2.1. credential = DefaultAzureCredential()

5.7.3. Run debug (press F5 or go via the Run menu) to verify that code runs without error

5.7.3.1. Gotcha! You might get a failure: No module named 'azure.identity'

5.7.3.1.1. This is probably because the Azure package installations we did earlier went into a general Python distribution on your local machine and not into the Python 3 virtual environment (.venv) that VS Code is using for your Azure Functions

5.7.3.2. If no errors are displayed in the terminal window, you will be able to navigate to Azure : Functions and under the Local Project, right-click your function and choose: Execute Function Now...

5.7.4. Add data lake service client

5.7.4.1. We need to leverage environment variables in order to pull application setting references, so we must import the os module for this at the top of __init__.py

5.7.4.1.1. import os

5.7.4.2. Below our credential object creation line, we add the line to create the data lake object, using the following to set the account_url parameter: os.environ["<application_seting_name>"]

5.7.4.2.1. data_lake = DataLakeServiceClient(account_url=os.environ["StorageAccountName"], credential=credential)

5.7.5. Run debug (press F5 or go via the Run menu) to verify that code runs without error

5.7.5.1. All being well, we will simply execute the function from our local project and see a successful message returned (pops up temporarily in bottom right of VS Code):

5.7.5.1.1. Executed function "ArgentoAzureSDKTestADLS". Response: "Hello, Azure. This HTTP triggered function executed successfully."

5.7.6. Add code to use data lake service client to get a list of file systems and convert this into a JSON format

5.7.6.1. To prepare a response in JSON format, we will capture the file systems in a list that we'll put inside a dictionary and then convert to JSON

5.7.6.1.1. We need the json module for this, so we add it to our imports at the top of __ini__.py

5.7.6.2. Call the list_file_systems() function on the data lake service client, which returns a generator that we can enumerate

5.7.6.2.1. file_systems = data_lake.list_file_systems()

5.7.6.3. Initialise a couple of variables for preparing the result, one dictionary and one list (array)

5.7.6.3.1. result_dict = {} result_arr = []

5.7.6.4. Iterate over file_systems, capturing the name property for each object and appending that to result_arr

5.7.6.4.1. for f in file_systems: result_arr.append(f.name)

5.7.6.5. Add the name input parameter and the result_arr variable to result_dict

5.7.6.5.1. result_dict["Requested by"] = name result_dict["File systems"] = result_arr

5.7.6.6. Convert result_dict to JSON format using json.dumps()

5.7.6.6.1. result_json = json.dumps(obj=result_dict, indent=4)

5.7.6.7. For debugging purposes, we can add a print statement

5.7.6.7.1. print(result_json)

5.7.7. Change return statement to pass result_json as the body for the HttpResponse()

5.7.7.1. return func.HttpResponse(result_json)

5.7.8. Run debug and then under Azure : Functions, run the function from the local project

5.7.8.1. After entering the name input parameter at the prompt, we should see the response printed out to the debug console (courtesy of the print statement we added), and the actual HTTP response will pop up in the bottom right

5.7.8.2. Here's the response, formatted as JSON:

5.7.8.2.1. Executed function "ArgentoAzureSDKTestADLS". Response: "{ "Requested by": "Azure", "File systems": [ "archive", "avq-dw-src-files", "avq-parquet", "data-flow-no-schema-files", "delta", "internetsales", "logs", "raw", "synapse", "test-files", "wrangling-data-flows" ] }"

5.8. Deploy to Azure and re-test function

5.8.1. These are the same deployment steps we've done before, but before running the Azure function, we need to take care of some security setup via Azure Portal

5.8.2. Go to Azure Portal and find the Identity page for your function app: we need to enable the system assigned identity (managed identity) for the app

5.8.2.1. After following the prompts, we'll see the identity becomes enabled (and registered in Azure Active Directory), and the object ID is revealed

5.8.3. In Azure Portal, go to the storage account and under Access Control (IAM), assign the function app managed identity to the Storage Blob Data Contributor role

5.8.4. In VS Code, go to Azure: Functions area and locate your function now inside your subscription, then right right and choose: Execute Function Now...

5.8.4.1. Gotcha! The function did not succeed because the expected response was not returned

5.8.4.1.1. Go to Azure Functions | Functions and click on the function in question

5.8.4.1.2. Under Function | Monitor | Invocations, click on the hot-linked date entry for the failed function call

5.8.4.1.3. The solution to this was simple: add the external packages to requirements.txt in VS Code and re-publish the function app

5.8.4.2. Once the gotcha is resolved, a re-run of the Azure function produces the expected HTTP response as per the local debug

6. POC: Call Azure Function from Data Factory

6.1. Developing Azure Functions that do something with the Azure SDK is fine but to be really useful, we need to be able to call the function as part of some automated process; calling from a Data Factory pipeline is a perfect example

6.2. In this POC, we use the ArgentoAzureSDKTestADLS function previously developed, calling it from an ADF pipeline and record the results in an Azure SQL Database table

6.3. Step 1: Azure SQL DB setup

6.3.1. We'll create a table to hold the list of file systems (containers) returned by the function:

6.3.1.1. CREATE TABLE dbo.AzureFunctionTest ( FileSystem VARCHAR(100) NOT NULL );

6.3.2. We'll create a stored proc that truncates the table, and another proc that adds a record to it

6.3.2.1. CREATE PROC dbo.InitialiseAzureFunctionTest AS BEGIN TRUNCATE TABLE dbo.AzureFunctionTest; END;

6.3.2.2. CREATE PROC dbo.UpdateAzureFunctionTest @FileSystem VARCHAR(100) AS BEGIN INSERT INTO dbo.AzureFunctionTest(FileSystem) VALUES (@FileSystem); END;

6.4. Step 2: Develop ADF pipeline

6.4.1. Add pipeline parameter to hold the POST request body for the function

6.4.1.1. We add a placeholder for the name parameter, <pipeline_name>, which we'll substitute with the actual ADF pipeline name shortly

6.4.1.2. { "name":"<pipeline_name>" }

6.4.1.3. We will be prompted to create a linked service for the Azure Function

6.4.1.3.1. We had to copy and paste in the function token as we previously configured the function to use this authentication

6.4.1.3.2. We can retrieve the function key via the Azure Portal by navigating to the function and under Function Keys, clicking the "Show values" option, which then gives us a copy button

6.4.2. Add stored procedure activity that calls: InitialiseAzureFunctionTest

6.4.3. Add Azure function activity that invokes our function, setting the method to POST and entering an expression for the (POST) Body

6.4.3.1. The POST Body expression is:

6.4.3.1.1. @replace(string(pipeline().parameters.RequestBody),'<pipeline_name>',pipeline().Pipeline)

6.4.4. Add ForEach activity that iterates over the output from the Azure Function activity, specifically the output.file_systems

6.4.4.1. Before specifying the expression for the ForEach Items, we can run the pipeline and check the output of the ForEach

6.4.4.2. The ForEach Items expression is:

6.4.4.2.1. @activity('Get File System List').output.file_systems

6.4.4.3. Inside the ForEach activity, we add a second stored procedure activity that calls: UpdateAzureFunctionTest

6.4.4.3.1. We map item(), from the ForEach activity, to the FileSystem input parameter

6.5. Step 3: Test ADF pipeline

6.5.1. After clicking Debug, we should see all steps complete successfully

6.5.2. And when we check the Azure SQL DB table contents, we should see all the file system names retrieved from the Azure function