# Azure Functions

The following steps show how to connect **Azure Functions** account to **CodeMash**. The steps consist of:

1. Setting up an **Azure** account.
2. Creating function apps.
3. Creating an **Azure Active Directory** app.
4. Connecting account into the **CodeMash** project.

## Setting up Microsoft Azure account

If you already have **Microsoft Azure Functions** app ready to use, skip this section.

* Go to <https://azure.microsoft.com/en-us/features/azure-portal/> and sign in with your Microsoft account.
* Now open the sidebar menu and navigate to **Create a resource**. Then select **Function App**. Here you might see different options if you haven't set up this yet.
  * If you are eligible for a free trial, sign up for free trial.
  * If you are not eligible for a free trial, you will have to add billing.

## Creating the first function

If you already have your functions ready, skip this section.

* From the management console, open the sidebar menu and select **Create a resource**.
* In the opened page, select **Function App**.
* Creating a function app. The following steps lead through the function app creation process.
  * Select your Azure **Subscription** and **Resource Group**. Create a new resource group if you want.
  * Select any available app name, for **Publish** select **Code**, then any runtime and region. Later you will be able to add more function apps under the same resource group but with different region and runtime.
  * For hosting, monitoring, and tags choose whatever you like.
* Once function app deployment is finished, go-to resource and you should see something like this.

![Function app dashboard](https://760328771-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwSkuCpTNI_AerL8J2a%2F-M7m1BZv_8dYS5MPaRbl%2F-M7m8eI8Nzfp5T8jcusf%2Fazure-functions-1.png?alt=media\&token=79d9f780-aceb-498f-a2a9-7c38d18978b9)

* Select **Functions** from the sidebar menu. Then press **Add**.
* Select **HTTP trigger** as a function template. In details section select **Function** for **Authorization level**.

## Function authentication

Function calls from CodeMash are authenticated using **Host** key. To get or create a new key go to your function app dashboard and select **App keys** from the sidebar menu.

![](https://760328771-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwSkuCpTNI_AerL8J2a%2F-M7m1BZv_8dYS5MPaRbl%2F-M7mCBQvjrMRUTmmlsiS%2Fazure-functions-2.png?alt=media\&token=3424d9d6-8b9a-4ae5-902f-86012ca84626)

## Adding AAD authentication

{% hint style="warning" %}
Do not skip this section. This is required to get your functions from Microsoft.
{% endhint %}

This authentication is required to get your functions from Microsoft API.

* Navigate to **Azure Active Directory** from the expandable sidebar menu.
* Then navigate to **App registrations** from the sidebar menu.
* Select **New registration**. Here give any name, for supported account types select the second option - **Accounts in any organizational directory (Any Azure AD directory - Multitenant)**.
* For redirect URI select **Single-page application (SPA)** and write this URI (without quotes) "<https://cloud.codemash.io/connections/azure/confirmation>".
* Once the app is created, navigate to **Authentication** from the sidebar menu.
* Here scroll down and select **Access tokens** under **Implicit grant** and save.

![Function AAD authentication settings](https://760328771-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwSkuCpTNI_AerL8J2a%2F-M7m1BZv_8dYS5MPaRbl%2F-M7mHVxN7MPphDuO4Rmi%2Fazure-functions-3.png?alt=media\&token=ce7f31cd-1529-4246-b5c1-a85fad546776)

* Now from the same sidebar menu go to **API permissions**. Here press **Add a permission** and select **Azure Service Management**. Now select **user\_impersonation** permission. Then press **Grant admin consent**.&#x20;
* Once done, the API permissions page should look something like the following image.

![Function AAD API permissions](https://760328771-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwSkuCpTNI_AerL8J2a%2F-M7m1BZv_8dYS5MPaRbl%2F-M7mJsCUL62tNSv_I6dW%2Fazure-functions-4.png?alt=media\&token=e8050cfc-49e0-412b-afb9-3c34ebc1bd38)

## Adding API credentials to CodeMash

Once you have your function apps and authentication ready, you can add **Azure Functions** account in **CodeMash**.

* Go to **CodeMash** project dashboard and navigate to Code > Account > Add New > Azure Functions.
* Fill the fields. Example with filled details shown below.
  * **Subscription ID** - your subscription ID. Can be found in the function app overview page.
  * **Resource group** - resource group in which you created your function app. Can be found in the function app overview page.
  * **Client ID** - your AAD authentication application ID. Can be found in the AAD application (created in the section above).
  * **Function Apps** - functions apps that you want to add. You can add multiple function apps, but they all have to be in the same resource group. Here specify the function app name and a host key for that app. Where to find host keys check [here](#function-authentication).

![](https://760328771-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwSkuCpTNI_AerL8J2a%2F-M7mK1G3tDo02GK4giS5%2F-M7mLx4jirQnS82Gn-4u%2Fazure-functions-5.png?alt=media\&token=8578edb5-f60b-4688-9b31-f1a3de748c84)

* After pressing connect, you will be redirected to login with Microsoft. Log in with your Microsoft Azure account. This will allow us to get your functions from Microsoft.
* After connecting, your functions will appear in the functions tab. This process might take a few moments to receive all functions from Microsoft services.
