I have performed the following
Requirement is to Relplace azure blob connection string with function app Managed identity
Can you please help to identify
I also found this blog which resonates the requirement however I am not sure how to perform this in VS and sync to azure portal for my current function app
thanks
I successfully ran the Blob trigger function locally and in the Azure Function App using DefaultAzureCredentials
and Managed Identity
.
As mentioned in this MS DOC.
The Blob Trigger manages failures after multiple retries by writing poison blobs to a queue. When using the serviceUri
format, the AzureWebJobsStorage
connection is required.
If blobServiceUri
is specified, you must also include the queueServiceUri
in the local.settings.json
.
You can use the Blob and Queue service URIs in place of the connection string in the local environment (e.g., Visual Studio).
local.settings.sjon :
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
"BlobConnection__blobServiceUri": "https://<storagename>.blob.core.windows.net/",
"BlobConnection__queueServiceUri": "https://<storagename>.queue.core.windows.net/"
}
}
Function1.cs :
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace FunctionApp4
{
public class Function1
{
private readonly ILogger<Function1> _logger;
public Function1(ILogger<Function1> logger)
{
_logger = logger;
}
[Function(nameof(Function1))]
public async Task Run([BlobTrigger("kamcontainer/{name}", Connection = "BlobConnection")] Stream stream, string name)
{
using var blobStreamReader = new StreamReader(stream);
var content = await blobStreamReader.ReadToEndAsync();
_logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {content}");
}
}
Program.cs :
using Microsoft.Azure.Functions.Worker.Builder;
using Microsoft.Extensions.Hosting;
var builder = FunctionsApplication.CreateBuilder(args);
builder.ConfigureFunctionsWebApplication();
builder.Build().Run();
I have created a Service principle in Azure AD and added the clientID
, clientSecret
and TenantID
to the System Environment Variables
to run the function using DefaultAzureCredentials as shown below.
Add below to your System Environment Variables :
AZURE_CLIENT_ID = <clientID>
AZURE_CLIENT_SECRET = <clientSecret>
AZURE_TENANT_ID = <TenantID>
I have Enabled the Manged Identity in the Azure Function App as shown below.
I have assigned the Storage Blob Data Owner role to the Service Principal
and the Storage Blob Data Contributor role to the Function App
under Access Control (IAM) in the Storage account, as shown below.
Local Output :
I started running the Blob trigger function and upload a file in the Blob storage as shown below.
The Blob Trigger function ran successfully and retrieved the blob details, as shown below.
I have updated the below in the function app > Environment Variables > App settings and published the Blob trigger function to the Azure Function App, as shown below.
"BlobConnection__blobServiceUri": "https://<storagename>.blob.core.windows.net/",
"BlobConnection__queueServiceUri": "https://<storagename>.queue.core.windows.net/"
Azure Function App Output :
I successfully ran the Blob Trigger function in the Azure Function App and retrived the blob details, as shown below.