|
| 1 | +--- |
| 2 | +title: Use identity-based connections instead of secrets with triggers and bindings |
| 3 | +titleSuffix: Azure Functions |
| 4 | +description: Learn how to use identity-based connections instead of connection strings with a Service Bus queue trigger |
| 5 | +ms.topic: tutorial |
| 6 | +ms.date: 10/20/2021 |
| 7 | +#Customer intent: As a function developer, I want to learn how to use managed identities so that I can avoid having to handle connection strings in my application settings. |
| 8 | +--- |
| 9 | + |
| 10 | +# Tutorial: Use identity-based connections instead of secrets with triggers and bindings |
| 11 | + |
| 12 | +This tutorial shows you how to configure Azure Functions to trigger off of Azure Service Bus queues using managed identities instead of secrets stored in the function app settings. The tutorial is a continuation of the [Create a function app without default storage secrets in its definition][previous tutorial] tutorial. To learn more about identity-based connections, see [Configure an identity-based connection.](functions-reference.md#configure-an-identity-based-connection). |
| 13 | + |
| 14 | +While the procedures shown work generally for all languages, this tutorial currently supports C# class library functions on Windows specifically. |
| 15 | + |
| 16 | +This tutorial has two parts: |
| 17 | + |
| 18 | +1. [Create a function app without default storage secrets in its definition][previous tutorial] |
| 19 | +2. Use identity-based connections instead of secrets with triggers and bindings (this article) |
| 20 | + |
| 21 | +In Part 2, you'll learn how to: |
| 22 | + |
| 23 | +> [!div class="checklist"] |
| 24 | +> |
| 25 | +> * Create a Service Bus namespace and queue. |
| 26 | +> * Configure your function app with managed identity |
| 27 | +> * Create a role assignment granting that identity permission to read from the Service Bus queue |
| 28 | +> * Create and deploy a function app with a Service Bus trigger. |
| 29 | +> * Verify your identity-based connection to Service Bus |
| 30 | +
|
| 31 | +## Prerequisite |
| 32 | + |
| 33 | ++ An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio). |
| 34 | + |
| 35 | ++ The [.NET Core 3.1 SDK](https://dotnet.microsoft.com/download) |
| 36 | + |
| 37 | ++ The [Azure Functions Core Tools](functions-run-local.md#v2) version 3.x. |
| 38 | + |
| 39 | ++ Have an existing function app with a system-assigned identity. For steps on how to do this follow part 1 of the tutorial: [Create a function app with identity-based connections][previous tutorial]. |
| 40 | + |
| 41 | +## Create a service bus and queue |
| 42 | + |
| 43 | +1. In the [Azure portal](https://portal.azure.com), choose **Create a resource (+)**. |
| 44 | + |
| 45 | +1. On the **Create a resource** page, select **Integration** > **Service Bus**. |
| 46 | + |
| 47 | +1. On the **Basics** page, use the following table to configure the Service Bus namespace settings. Use the default values for the remaining options. |
| 48 | + |
| 49 | + | Option | Suggested value | Description | |
| 50 | + | ------------ | ---------------- | ---------------- | |
| 51 | + | **Subscription** | Your subscription | The subscription under which your resources are created. | |
| 52 | + | **[Resource group](../azure-resource-manager/management/overview.md)** | myResourceGroup | The resource group you created with your function app. | |
| 53 | + | **Namespace name** | Globally unique name | The namespace of your instance from which to trigger your function. Because the namespace is publicly accessible, you must use a name that is globally unique across Azure. The name must also be between 6 and 50 characters in length, contain only alphanumeric characters and dashes, and can't start with a number. | |
| 54 | + | **[Location](https://azure.microsoft.com/regions/)** | myFunctionRegion | The region where you created your function app. | |
| 55 | + | **Pricing tier** | Basic | The basic Service Bus tier. | |
| 56 | + |
| 57 | +1. Select **Review + create**. After validation finishes, select **Create**. |
| 58 | + |
| 59 | +1. After deployment completes, select **Go to resource**. |
| 60 | + |
| 61 | +1. In your new Service Bus namespace, select **+ Queue** to add a queue. |
| 62 | + |
| 63 | +1. Type `myinputqueue` as the new queue's name and select **Create**. |
| 64 | + |
| 65 | +Now, that you have a queue, you will add a role assignment to the managed identity of your function app. |
| 66 | + |
| 67 | +## Configure your Service Bus trigger with a managed identity |
| 68 | + |
| 69 | +To use Service Bus triggers with identity-based connections, you will need to add the **Azure Service Bus Data Receiver** role assignment to the managed identity in your function app. This role is required when using managed identities to trigger off of your service bus namespace. You can also add your own account to this role, which makes it possible to connect to the service bus namespace during local testing. |
| 70 | + |
| 71 | +> [!NOTE] |
| 72 | +> Role requirements for using identity-based connections vary depending on the service and how you are connecting to it. Needs vary across triggers, input bindings, and output bindings. For more details on specific role requirements, please refer to the trigger and binding documentation for the service. |
| 73 | +
|
| 74 | +1. In your service bus namespace that you just created, select **Access Control (IAM)**. This is where you can view and configure who has access to the resource. |
| 75 | + |
| 76 | +1. Click **Add** and select **add role assignment**. |
| 77 | + |
| 78 | +1. Search for **Azure Service Bus Data Receiver**, select it, and click **Next**. |
| 79 | + |
| 80 | +1. On the **Members** tab, under **Assign access to**, choose **Managed Identity** |
| 81 | + |
| 82 | +1. Click **Select members** to open the **Select managed identities** panel. |
| 83 | + |
| 84 | +1. Confirm that the **Subscription** is the one in which you created the resources earlier. |
| 85 | + |
| 86 | +1. In the **Managed identity** selector, choose **Function App** from the **System-assigned managed identity** category. The label "Function App" may have a number in parentheses next to it, indicating the number of apps in the subscription with system-assigned identities. |
| 87 | + |
| 88 | +1. Your app should appear in a list below the input fields. If you don't see it, you can use the **Select** box to filter the results with your app's name. |
| 89 | + |
| 90 | +1. Click on your application. It should move down into the **Selected members** section. Click **Select**. |
| 91 | + |
| 92 | +1. Back on the **Add role assignment** screen, click **Review + assign**. Review the configuration, and then click **Review + assign**. |
| 93 | + |
| 94 | +You've granted your function app access to the service bus namespace using managed identities. |
| 95 | + |
| 96 | +## Connect to Service Bus in your function app |
| 97 | + |
| 98 | +1. In the portal, search for your pre-existing function app. If you completed the [previous tutorial], use that app. You can also browse to it in the **Function App** page. |
| 99 | + |
| 100 | +1. In your function app, select **Configuration** under **Settings**. |
| 101 | + |
| 102 | +1. In **Application settings**, select **+ New application setting** to create the new setting in the following table. |
| 103 | + |
| 104 | + | Name | Value | Description | |
| 105 | + | ------------ | ---------------- | ----------- | |
| 106 | + | **ServiceBusConnection__fullyQualifiedNamespace** | <SERVICE_BUS_NAMESPACE>.servicebus.windows.net | This setting connections your function app to the Service Bus use identity-based connections instead of secrets. | |
| 107 | + |
| 108 | +1. After you create the two settings, select **Save** > **Confirm**. |
| 109 | + |
| 110 | +Now that you've prepared the function app to connect to the service bus namespace using a managed identity, you can add a new function that uses a Service Bus trigger to your local project. |
| 111 | + |
| 112 | +## Add a Service Bus triggered function |
| 113 | + |
| 114 | +1. Run the `func init` command, as follows, to create a functions project in a folder named LocalFunctionProj with the specified runtime: |
| 115 | + |
| 116 | + ```csharp |
| 117 | + func init LocalFunctionProj --dotnet |
| 118 | + ``` |
| 119 | + |
| 120 | +1. Navigate into the project folder: |
| 121 | + |
| 122 | + ```console |
| 123 | + cd LocalFunctionProj |
| 124 | + ``` |
| 125 | + |
| 126 | +1. In the root project folder, run the following commands: |
| 127 | + |
| 128 | + ```command |
| 129 | + dotnet remove package Microsoft.Azure.Webjobs.Extensions.ServiceBus |
| 130 | + dotnet add package Microsoft.Azure.Webjobs.Extensions.ServiceBus --prerelease |
| 131 | + ``` |
| 132 | + |
| 133 | + This replaces the default version of the Service Bus extension package with a version that supports managed identities. |
| 134 | + |
| 135 | +1. Run the following command to add a Service Bus triggered function to the project: |
| 136 | + |
| 137 | + ```csharp |
| 138 | + func new --name ServiceBusTrigger --template ServiceBusQueueTrigger |
| 139 | + ``` |
| 140 | + |
| 141 | + This adds the code for a new Service Bus trigger and a reference to the extension package. You need to add a service bus namespace connection setting for this trigger. |
| 142 | + |
| 143 | +1. Open the new ServiceBusTrigger.cs project file and replace the `ServiceBusTrigger` class with the following code: |
| 144 | + |
| 145 | + ```csharp |
| 146 | + public static class ServiceBusTrigger |
| 147 | + { |
| 148 | + [FunctionName("ServiceBusTrigger")] |
| 149 | + public static void Run([ServiceBusTrigger("myinputqueue", |
| 150 | + Connection = "ServiceBusConnection")]string myQueueItem, ILogger log) |
| 151 | + { |
| 152 | + log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}"); |
| 153 | + } |
| 154 | + } |
| 155 | + ``` |
| 156 | + |
| 157 | + This code sample updates the queue name to `myinputqueue`, which is the same name as you queue you created earlier. It also sets the name of the Service Bus connection to `ServiceBusConnection`. This is the Service Bus namespace used by the identity-based connection `ServiceBusConnection__fullyQualifiedNamespace` you configured in the portal. |
| 158 | + |
| 159 | +> [!NOTE] |
| 160 | +> If you try to run your functions now using `func start` you'll receive an error. This is because you don't have an identity-based connection defined locally. If you want to run your function locally, set the app setting `ServiceBusConnection__fullyQualifiedNamespace` in `local.settings.json` as you did in [the previous section](#connect-to-service-bus-in-your-function-app). In addition, you'll need to assign the role to your developer identity. For more details, please refer to the [local development with identity-based connections documentation](./functions-reference.md#local-development-with-identity-based-connections). |
| 161 | + |
| 162 | +## Publish the updated project |
| 163 | + |
| 164 | +1. Run the following command to locally generate the files needed for the deployment package: |
| 165 | + |
| 166 | + ```console |
| 167 | + dotnet publish --configuration Release |
| 168 | + ``` |
| 169 | + |
| 170 | +1. Browse to the `\bin\Release\netcoreapp3.1\publish` subfolder and create a .zip file from its contents. |
| 171 | + |
| 172 | +1. Publish the .zip file by running the following command, replacing the `FUNCTION_APP_NAME`, `RESOURCE_GROUP_NAME`, and `PATH_TO_ZIP` parameters as appropriate: |
| 173 | + |
| 174 | + ```azurecli |
| 175 | + az functionapp deploy -n FUNCTION_APP_NAME -g RESOURCE_GROUP_NAME --src-path PATH_TO_ZIP |
| 176 | + ``` |
| 177 | + |
| 178 | +Now that you have updated the function app with the new trigger, you can verify that it works using the identity. |
| 179 | + |
| 180 | +## Validate your changes |
| 181 | + |
| 182 | +1. In the portal, search for `Application Insights` and select **Application Insights** under **Services**. |
| 183 | + |
| 184 | +1. In **Application Insights**, browse or search for your named instance. |
| 185 | + |
| 186 | +1. In your instance, select **Live Metrics** under **Investigate**. |
| 187 | + |
| 188 | +1. Keep the previous tab open, and open the Azure portal in a new tab. In your new tab, navigate to your Service Bus namespace, select **Queues** from the left blade. |
| 189 | + |
| 190 | +1. Select your queue named `myinputqueue`. |
| 191 | + |
| 192 | +1. Select **Service Bus Explorer** from the left blade. |
| 193 | + |
| 194 | +1. Send a test message. |
| 195 | + |
| 196 | +1. Select your open **Live Metrics** tab and see the Service Bus queue execution. |
| 197 | + |
| 198 | +Congratulations! You have successfully set up your Service Bus queue trigger with a managed identity! |
| 199 | + |
| 200 | +[!INCLUDE [clean-up-section-portal](../../includes/clean-up-section-portal.md)] |
| 201 | + |
| 202 | +## Next steps |
| 203 | + |
| 204 | +In this tutorial, you created a function app with identity-based connections. |
| 205 | + |
| 206 | +Use the following links to learn more Azure Functions with identity-based connections: |
| 207 | + |
| 208 | +- [Managed identity in Azure Functions](../app-service/overview-managed-identity.md) |
| 209 | +- [identity-based connections in Azure Functions](./functions-reference.md#configure-an-identity-based-connection) |
| 210 | +- [Functions documentation for local development](./functions-reference.md#local-development-with-identity-based-connections) |
| 211 | + |
| 212 | +[previous tutorial]: ./functions-identity-based-connections-tutorial.md |
0 commit comments