Azure Functions (PS), Managed Identities and Exchange Online
A short guide on how to use Azure functions with Exchange module, using managed identity for authentication
EXO V3 (more info here) has recently become generally available, and with it comes support for Managed Identity sign in from Azure Functions
I would like to share how you can utilize Azure Functions on linux with a user-assigned managed identity and how to set everything up from scratch.
Here’s what the setup will look like:
- Create and set up the function app (Linux, PowerShell 7.2)
- Create managed identity (instead of system-assigned, user assigned can be used with multiple azure resources)
- Add permissions to the managed identity
- Test Exchange connectivity in a sample function
Create the function app
- Go to the azure portal, type “function app” in the search, then click function app
- Click Create, choose a name and resource group. For details we will be using the following runtimes:
- Runtime stack: PowerShell Core
- Operating system: Linux
- Plan type: Consumption
After creating, you’ll now need to set it up so it has the right modules.
- Open the function app in the Azure portal
- Click on “App files”
- Switch the active file to “requirements.psd1”
- Uncomment the Az line (or, use Az.Accounts with version 2.*) and add this: ‘ExchangeOnlineManagement’ = ‘3.0.0’. It should look like this:
Save the file, then restart the function app from the overview pane.
Create the managed identity
These steps involve a mixture of using the Azure portal and PowerShell.
- From the azure portal, search for “managed identities” and click the first result
- From the list, click Create. Choose a region matching the function app and give it a name and resource group, then create it.
- Go back to the function app, click Identity -> User assigned -> Add, then add your new resource.
Next you’ll need to add the appropriate Exchange permissions.
- Go to Azure AD -> Roles and administrators -> Exchange administrator
- Add the managed identity as an exchange administrator
- Now you’ll need to add Exchange.ManageAsApp to the managed identity.
Here’s an example script snippet for adding Exchange.ManageAsApp using AzureAD powershell (deprecated, I should update this!)
Now that you’ve added the required permissions, give it a few minutes before moving on. Exchange takes a little while to catch up with new permissions.
In your function app’s profile (function app -> app files -> profile), use the following code:
In the above, change “YourManagedIdentityClientId” and “YourTenantDomain” with your relevant details. You can find the managed identity client id in azure portal -> Managed Identities -> managed identity resource -> overview -> Client Id
Create the function
Go to the Functions pane in your function app, then create a new timer function. Replace the original code with the following:
Save the function, click “Test/Run”, then watch the logs. You should see message trace data appear in the logs.
That’s it! With this technique, you could extend your Exchange security in a variety of ways. For example, if you’d like to have message trace data queryable from Log analytics, you can now do this fairly easily (it can almost work like an alternative to Microsoft Defender for Office P2 license!).
Troubleshooting
If you have trouble getting the function app to run, there’s one likely problem: It’s taking a really long time to install the depedencies. Here are a few solutions:
- First option: Manually upload the function app depedencies:
- Install the required modules: Az, ExchangeOnlineManagement on another machine (do it on Linux if you’re using a Linux function app, Windows if on Windows etc.)
- Copy the module files from userfolder/PowerShell/Modules into the function apps file share in this folder: StorageAccountName/FileShareName/.local/share/powershell/Modules
- Second option: Wait a really long time. I have seen it take 2-3 hours.
- Scope the Az module in your function app’s requirements.psd1 to something smaller:Az.Accounts instead of Az.*