Deploy an API to Azure Functions through GitHub Actions

What is Azure Functions? Azure Functions is a serverless option in Azure. It is very appealing for fast, efficient, and scalable on-demand deployment scenarios without needing to manage practically any of the underlying infrastructure. The

AI-assisted translation, approved by Mateo.

What is Azure Functions?

Azure Functions is a serverless option in Azure. It is very appealing for fast, efficient, and scalable on-demand deployment scenarios without needing to manage practically any of the underlying infrastructure.

The main idea of this service is that "you provide the code and Azure takes care of the rest". Although this sometimes comes close to reality, in most cases we must adapt the code so it can run as a function app.

Functions supports languages such as C#, Java, JavaScript, PowerShell, Python, and others such as Rust and GO.

We have different types of plans for Azure Functions:

  • Consumption plan:
    • Pay-as-you-go; billing will only be incurred for the time the code is running.
    • Automatic scalability.
    • Maximum execution time: 5 min.
  • Premium plan:
    • More connectivity options (vnet integration).
    • Better performance.
    • No time limit.
    • Billing for reserved instances.
  • Dedicated (App Service):
    • We will use an App Service Plan as a base.
    • Greater control over the infrastructure.
    • Billing based on reserved infrastructure, regardless of function executions.

It is an event-based service, and it has several "use case" scenarios. Here are some of them:

  • Process file uploads: Runs code when a file is uploaded or changes in Blob Storage.
  • Run scheduled tasks: Runs data-cleanup code at predefined time intervals.
  • Respond to database changes: Runs custom logic when a document is created or updated in Azure Cosmos DB.
  • Create reliable messaging systems: Processes message queues using Queue Storage, Service Bus, or Event Hubs.
  • Build a scalable web API: Implements a set of REST endpoints for your web applications using HTTP triggers.

In this example, we will use Azure Functions to implement a scalable API.

Python FastAPI framework

We will be deploying a Python FastAPI. We will use the code hosted in the repo madsblog-fastapi-on-azure-functions:

  • As the information in this Fork indicates, it is an Azure Sample of a Python FastAPI.
  • To run a FastAPI in Azure Functions, it is necessary to make some declarations at the code level. In this case, the code is already updated to run in the App.
  • azure-functions is added to the prerequisites file:
  • host.json file:
  • The following are configured in this file:
    • Application Insights logging aspects for the function.
    • Extensions: the HTTP Trigger for the / route is configured here. No additional prefix is added.
  • The root folder contains the function_app.py file, which initializes/calls the AsgiFunctionApp using the imported FastApi App.
  • In the WrapperFunction folder, the _init_.py file defines the FastApi that we call in our function_app.py file.

Authorization Configuration

We will make only one change to our API code: modify the Authorization Level. There are 3 types of Authorization Levels for APIs:

  1. Anonymous:
    • Anyone can invoke the function; no key is required.
    • This is useful for public functions that do not require any type of authentication or authorization.
  1. Function:
    • A function-specific key is required to invoke the function.
    • Each function has its own key, which means that one function's key cannot be used to invoke another function, even if both functions are in the same Azure Functions application.
    • This is useful when you want to restrict access to a specific function and/or when you want to be able to rotate keys by function.
  1. Admin:
    • A master key (also known as an administrator key) is required to invoke the function.
    • This key provides access to any function within the Azure Functions application, so it should be handled with an extra level of care.
    • This is useful when an elevated level of control or administrative access to all functions is needed.
  1. System (specific to certain triggers such as Timer and Durable Functions):
    • It is similar to Admin, but it also provides access to internal Azure systems.
    • It is not typically used for HTTP-based functions.
  1. User (specific to Azure Functions with EasyAuth):
    • It requires a user to be authenticated to invoke the function.
    • It works together with the Azure App Service authentication service (also known as EasyAuth).

In this case, we will change the Authorization Level to Function:

  • We will modify the following line of code in the file function_app.py:
  • Push the changes to the repo:

To deploy an Azure Function, we can follow the instructions in the following video

Creating a Workflow in GitHub Actions

To create our Workflow in GitHub Actions, we must:

  • Make sure Actions is enabled in our repository:
  • In the .github/workflows/ directory, we will find the azure-dev.yaml file. This file will contain the GitHub Workflow.
  • This Workflow will allow us to perform CI/CD with our Azure Function App using the Public Profile.
  • It is worth clarifying that there are other methods for implementing CI/CD from GitHub to Azure Functions. One requirement of this method is that the Function be exposed to the internet and have Public Access enabled.
  • If the Function is private and is not exposed to the internet, we can use other methods that are beyond the scope of this post.
  • In this yaml file (credits to Azure-Samples) an alternative deployment is shown through a workflow that performs an 'Az Login' and deploys to Azure, rather than doing it directly in Azure Functions without needing to connect to Azure.

Obtain the Publish Profile for our Azure Function

To obtain the Publish Profile for our Function App, we must:

  • Go to the Azure portal.
  • Go to our Function App.
  • On the first "Overview" screen, we must click "Get publish profile". This will download the publish profile file.
  • Open the file and copy the content.

Create a Publish Profile secret in GitHub

In one of the final activities of this Demo, we will generate the secret for the publish profile in GitHub Actions. This will allow our Workflow to access our Function App through this method. It is worth remembering that this method is one of those used (and one of the simplest) to perform CI/CD on a function app. We can use other methods that, depending on the scenario, may be more or less convenient.

  • Go to our repo, enter Settings, and click Secrets and variables > Actions:
  • Create a new Repository Secret:
  • We will name it AZURE_FUNCTIONAPP_PUBLISH_PROFILE, and in the secret content we will insert the publish profile obtained from the function app.

Run the Workflow in GitHub Actions

We now have all the parameters of our Workflow configured. Thanks to the "workflow_dispatch:" parameter, we have the option to run our Workflow manually.

  • We must go to the Repo, open the Actions tab, and click the Workflow (below All workflows in the left sidebar). Finally, run "Run Workflow".
  • When we run it, we can verify that the Job is running:
  • Successful execution!

Verify deployment in Azure Functions

We can verify the correct deployment in the Azure portal:

  • Files in Azure Functions:
  • In the Deployment Center logs:
  • We see our Function deployed within the Function App:

Test API

To test our API deployed in the Function App, we will enter our "http_app_func" function from the Portal:

  • Enter "Code + Test" and click "Test/Run":
  • We will choose the following parameters for the test:
    • Method: GET.
    • Key: Default (function key)
    • *route: /sample:
  • We verify the expected response from the App :)
  • We will now perform a test with the following parameters:
    • Method: GET.
    • Key: Default (function key)
    • *route: /hello/{nombre deseado en el output}.
  • We see the output result:

Thanks for reading. The learning continues in the next article.