azure api management,

How to Secure Azure Functions App with Azure API Management

Sven Malvik Sven Malvik Connect Feb 02, 2021 · 3 mins read
How to Secure Azure Functions App with Azure API Management

How to use an Azure Managed Identity to authenticate against an Azure Functions app that is exposed through Azure API Management. Our Function App is by default public available to everyone. There are two things we can do to prevent this. Either by enabling Azure AD authentication or by IP whitelisting. This post discusses authentication with Azure AD authentication with Managed Identities.

Public available Azure Function AppPublic available Azure Function App

Enabling Azure AD authentication on a Functions App means Azure API Management (APIM) needs to authenticate itself. This is where Managed Identities comes into play.

A system-assigned Managed Identity is enabled directly on the Azure resource. When the identity is enabled, Azure creates an identity for the instance in the Azure AD tenant. After the identity is created, the credentials are provisioned onto the instance. The lifecycle of a system-assigned identity is directly tied to the Azure resource that it’s enabled on. If the resource is deleted, Azure automatically cleans up the credentials and the identity in Azure AD. There’s no need to save passwords, no need to rotate credentials etc. Everything is done automatically for your in the background. Let’s make this happen now.

Setup

I created an instance of Azure API Management with the Consumption tier and an Azure Function App that I can access as you can see in the picture above.

Azure API Management and Function App setupAzure API Management and Function App setup

Enabling Managed Identity on Azure API Management

Enabling a Managed Identity for Azure API Management takes about 5 minutes. Click on Managed Identity and then enable it as shown below.

Enabling Managed Identity in Azure API ManagementEnabling Managed Identity in Azure API Management

Enabling AAD Authentication in Azure Functions

We can now continue and enable Azure AD authentication in the Functions App. Follow the steps as shown.

Enabling AAD Authentication in Azure Functions 1Enabling AAD Authentication in Azure Functions 1

Select Azure Active Directory as the authentication provider, not Facebook :)

Enabling AAD Authentication in Azure Functions 2Enabling AAD Authentication in Azure Functions 2

Now we try to access the Function App once again … and we can see that we need to authenticate.

Azure Function App enabled authentication with AADAzure Function App enabled authentication with AAD

Azure API Management Policy using Managed Identity

For Azure API Management to authenticate against AAD and receive a bearer token we need add some code in the inbound section of the API or Operation (Endpoint) of the Function App that I already have in APIM. We will use the authentication-managed-identity policy to authenticate with our Azure Functions App using the managed identity of APIM. This policy uses the managed identity to obtain an access token from AAD for accessing the specified resource. After successfully obtaining the token, the policy will set the value of the token in the Authorization header using the Bearer scheme.

First, we get the Application ID of the Function App in AAD as shown below.

Application ID of Function App in AADApplication ID of Function App in AAD

Second, we open the policy we want to change. In this example I chose the API level policy.

Opening Editor of Azure API Management API PolicyOpening Editor of Azure API Management API Policy

Third, we add authentication-managed-identity to the inbound section and pointing to the application for the Function App. That’s the Application ID of Function App in AAD.

Editing Azure API Management PolicyEditing Azure API Management Policy

Now we can test the endpoint from within the portal directly … Azure API ManagementAzure API Management

… or from a browser. I disabled the requirement of a subscription key to make it simpler.

Azure API ManagementAzure API Management

Conclusion

Managed Identities make life easier and more secure.

Join Newsletter
Get the latest updates right in your inbox. I never spam!
Sven Malvik
Written by Sven Malvik

Latest Stories

How to Reference Key Vault Secrets in Azure API Management

In an enterprise, an Azure API Management instance is often shared by many teams and many developers. The developers may all have access ...

Feb 05, 2021

Event-Driven Infrastructure with App Configuration

Azure App Configuration is great for externalizing application configurations. But what if an application is our infrastructure? How coul...

Sep 12, 2020

Understanding Policies in Azure API Management

Policies are the heart of Azure API Management. They let us change the behavior of our APIs in a very flexible manner. Before I dive in t...

Apr 18, 2020

Introduction to Azure API Management

Azure API Management (APIM) is a way to create consistent and modern API gateways for existing backend services. It provides an interface...

Jan 25, 2021

Logging in Azure API Management

This post is a complete step-by-step guide on how to send logs from Azure API Management to Azure Event Hub with PowerShell. We start by ...

Apr 11, 2020

How To Debug Policies in Azure API Management. A Step-by-Step Guide.

In this post I want to briefly go through the Azure API Management extension for VSCode and how we can debug policies. It’s one of the qu...

Jan 16, 2021

AZ-303 Self-Study Guide for Becoming an Azure Solution Architect

Microsoft updated it’s role based exam for AZ-300. It’s now called AZ-303 and launched last year. This certification is a great proof for...

Feb 01, 2021

AZ-900 Self-Study Guide for Azure Fundamentals

There are many good reasons to become Microsoft Azure Certified and take the Microsoft Azure AZ-900 exam. If you are reading this blog po...

Feb 03, 2021

Azure API Management from the Portal

We will provision Azure API Management. Creating an instance of it is just a click in the Azure Portal. We’ll do it once to show how easy...

Mar 07, 2020

Serving Website Images from Azure CDN with SSL

In this post I will show you step by step how to serve images on a website from Azure CDN with SSL enabled. My blog has a couple of Azure...

Apr 25, 2020

How To Manage Azure Virtual Machines

I will go through the first steps for managing Virtual Machines. We will create a Windows VM, start the Internet Information Service IIS,...

Dec 26, 2020

Latest Stories

How to Reference Key Vault Secrets in Azure API Management

How to Reference Key Vault Secrets in Azure API Management

In an enterprise, an Azure API Management instance is often shared by many teams and many developers. The developers may all have access ...

Feb 05, 2021

Event-Driven Infrastructure with App Configuration

Event-Driven Infrastructure with App Configuration

Azure App Configuration is great for externalizing application configurations. But what if an application is our infrastructure? How coul...

Sep 12, 2020

Understanding Policies in Azure API Management

Understanding Policies in Azure API Management

Policies are the heart of Azure API Management. They let us change the behavior of our APIs in a very flexible manner. Before I dive in t...

Apr 18, 2020

Introduction to Azure API Management

Introduction to Azure API Management

Azure API Management (APIM) is a way to create consistent and modern API gateways for existing backend services. It provides an interface...

Jan 25, 2021

Logging in Azure API Management

Logging in Azure API Management

This post is a complete step-by-step guide on how to send logs from Azure API Management to Azure Event Hub with PowerShell. We start by ...

Apr 11, 2020

How To Debug Policies in Azure API Management. A Step-by-Step Guide.

How To Debug Policies in Azure API Management. A Step-by-Step Guide.

In this post I want to briefly go through the Azure API Management extension for VSCode and how we can debug policies. It’s one of the qu...

Jan 16, 2021

AZ-303 Self-Study Guide for Becoming an Azure Solution Architect

AZ-303 Self-Study Guide for Becoming an Azure Solution Architect

Microsoft updated it’s role based exam for AZ-300. It’s now called AZ-303 and launched last year. This certification is a great proof for...

Feb 01, 2021

AZ-900 Self-Study Guide for Azure Fundamentals

AZ-900 Self-Study Guide for Azure Fundamentals

There are many good reasons to become Microsoft Azure Certified and take the Microsoft Azure AZ-900 exam. If you are reading this blog po...

Feb 03, 2021

Azure API Management from the Portal

Azure API Management from the Portal

We will provision Azure API Management. Creating an instance of it is just a click in the Azure Portal. We’ll do it once to show how easy...

Mar 07, 2020

Serving Website Images from Azure CDN with SSL

Serving Website Images from Azure CDN with SSL

In this post I will show you step by step how to serve images on a website from Azure CDN with SSL enabled. My blog has a couple of Azure...

Apr 25, 2020

How To Manage Azure Virtual Machines

How To Manage Azure Virtual Machines

I will go through the first steps for managing Virtual Machines. We will create a Windows VM, start the Internet Information Service IIS,...

Dec 26, 2020