Azure function ZIP Deployment using terraform - azure

I am looking to deploy Azure function by using Terraform, in the same way which is mentioned here in the Ms docs.
https://learn.microsoft.com/en-us/azure/azure-functions/deployment-zip-push
One of my need to store and Storage SAS key to Key Vault. I have seen some exmaple where we can below setting is used, but I can't use sas key without storing to Key Vault.
WEBSITE_RUN_FROM_ZIP = "https://${azurerm_storage_account.this.name}.blob.core.windows.net/${azurerm_storage_container.this.name}/${azurerm_storage_blob.this.name}${data.azurerm_storage_account_sas.this.sas}"
Any suggestion?
Thank you !

For this requirement, you can use the Key Vault reference to get the secrets to the Azure Function like this:
#Microsoft.KeyVault(SecretUri=https://myvault.vault.azure.net/secrets/mysecret/ec96f02080254f109c51a1f14cdb1931)
Or
#Microsoft.KeyVault(VaultName=myvault;SecretName=mysecret;SecretVersion=ec96f02080254f109c51a1f14cdb1931)
But this means only support system-assigned managed identities. So you need to enable the system-assigned managed identity and assign the right role of the Key Vault to it. Here is more details.

Related

How do we create and Manage Azure Keyvault Secrets using Azure Service Operators?

I am trying to create and manage Azure Keyvault Secrets using the Azure Service Operators.
I can see the options to create and manage the Keyvault certificate using the kind: KeyVaultKey. However, I am wondering how I can create and manage secrets using the Azure Service Operators.
Is there any lead on this?
Appreciate the help..!
The Azure Key Vault operator suite consists of the following operators:
• KeyVault - Deploys an Azure Key Vault given the location and resource group
• KeyVaultKey - Deploys an Azure Key Vault key given the location and resource group
The KeyVaultKey operator serves as an operator that allows for declarative management of Key Vault keys - one of the three resources available for storage and management in Key Vault; keys, secrets, and certificates. Keys can be leveraged for various use cases.
You can find sample code Here
Reference for Azure service operator
Note: Azure service operator is still under development

Azure function event hub integration with key vault reference

I am trying to integrate my Azure function to event hub and for that I need connection string. It works fine if I put the connection string directly into my application settings. However, I would like to keep my secrets in key vault and just use reference.
When I change the secret to reference I can see that the KV reference is fine and there is small green mark on top of it. However, when I go and look into function integrations it is completely broken and missing the integration. Also I cannot change to that application setting where my connection string is referenced in KV.
Is there any way to get the event hub integration working while referencing secrets from key vault?
Please check if you have followed to create an azure event hub triggered function with the environment variable and connection string .
Some app settings are to be made related to key vault.
Create a system-assigned Managed Identity of your Function.
(Function - > Identity -> Enable System-Assigned Managed Identity)
Assign this Managed Identity GET access to your Key Vault in the
Access Policies.
Create a key vault identity - Use Key Vault references - Azure App
Service | Microsoft Docs
Create another app settings which are similar to above app settings
which refer the key vault secrets as reference.
A Key Vault reference is of the form
#Microsoft.KeyVault({referenceString}), where {reference String} can
be vault name or vault uri -Use Key Vault reference
Or check the bindings of event hub trigger in functions.json file Azure function with Event hub trigger
Refer: Getting Key Vault Secrets in Azure Functions For
detailed information about the same.

Fetch value from Azure Key Vault reference (Managed identity)

I am trying to fetch secret values from azure key vault in Azure App Service.
My App Service has a managed identity
In the Azure Key vault, this managed identity is added under 'Role assignments' as 'Key vault contributor'
Also access policies has been added in azure key vault to give 'Get' permission to the AppServices's managed identity
Now in my c# code, I am trying to get the value of the AppSetting element using the code
ConfigurationManager.AppSettings['something'];
In my azure app service, under app settings, I have added the a key with name 'something' and it has the value pointing to the reference of the Key Vault as below:
#Microsoft.KeyVault(SecretUri=https://myvault.vault.azure.net/secrets/mysecret/ec96f02080254f109c51a1f14cdb1931)
Now I am able to retrieve the value from the appsettings, but instead of the actual value, it is pulling the output as #Microsoft.KeyVault(SecretUri=https://myvault.vault.azure.net/secrets/mysecret/c96f02080254f109c51a1f1cdb1931)
I am expecting the output to be value that I have set, not the keyvault reference string itself.
I am using .Net MVC 4.7 web app
As junnas said, you just add webapp's MSI in key vault Access policy.
1.Turn on webapp System-assign managed identity.
2.Add the identity into keyvault Access policy with secret Get permission.
3.Add the reference of the Key Vault into webapp Application settings.
4.Use Environment.GetEnvironmentVariable("AppsettingName"); to get the secret vaule.
Here is the output:
Actually i figured out that even using ConfiguraionManager.AppSettings["keyname"] also works fine. In my case, i had done everything as listed above , but had enabled "Slot Deployment" option in Connection string configuration in App Service. Once i enabled, now i am able to access my key vault secrets.

Managed Identity w/Azure Functions and Storage accounts

What is the appropriate way to use managed identity with Azure Functions and Storage accounts if the the AzureWebJobsStorage app setting is required by the function host? Is there a managed identity version of the storage account connection string that doesn't include the access key?
While you can't use Managed Identity to authenticate to the storage account directly, you can store the access key in Key Vault and fetch it from there using Key Vault References using Managed Identity.
Just wanted to share this because I believe its great to use KeyVault References instead of directly using access keys in the app settings.

Is it possible to create a key vault in Azure via c#

Is it possible to create a Key Vault in Azure via c#? I'm able to manipulate secrets and keys via the KeyVaultClient but I need to
A). See if a key vault already exists and
B). Create a key vault if it doesn't exist.
Yes, you can use the KeyVault management client to list where a key vault exists in an Azure subscription and create the KeyVault if necessary. There is C# SDK for these operations which are against Azure Resource Manager.
https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.management.keyvault.vaultsoperationsextensions?view=azure-dotnet

Resources