Skip to content

Commit b0cba66

Browse files
restructuring tutorial
1 parent 02b6de2 commit b0cba66

File tree

5 files changed

+464
-114
lines changed

5 files changed

+464
-114
lines changed

articles/azure-functions/TOC.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@
9393
href: functions-create-private-site-access.md
9494
- name: Use an outbound NAT gateway
9595
href: functions-how-to-use-nat-gateway.md
96-
- name: Use identity-based connections
96+
- name: Use identity for host connections
9797
href: functions-identity-based-connections-tutorial.md
98+
- name: Use identity for trigger and binding connections
99+
href: functions-identity-based-connections-tutorial-2.md
98100
- name: Image resize with Event Grid
99101
href: ../event-grid/resize-images-on-storage-blob-upload-event.md?toc=%2fazure%2fazure-functions%2ftoc.json
100102
- name: Create a serverless web app
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
---
2+
title: Tutorial - Use identity-based connections instead of secrets with triggers and bindings
3+
description: Learn how to use identity-based connections instead of connection strings with a Service Bus queue trigger, and how to use managed identities locally.
4+
ms.topic: tutorial
5+
ms.date: 10/20/21
6+
#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.
7+
---
8+
9+
# Tutorial: Use identity-based connections instead of secrets with triggers and bindings
10+
11+
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 [Functions managed identity tutorial](./functions-managed-identity-tutorial.md). To learn more about identity-based connections, see [Configure an identity-based connection.](functions-reference.md#configure-an-identity-based-connection).
12+
13+
While the procedures shown work generally for all languages, this tutorial currently supports C# class library functions on Windows specifically.
14+
15+
This tutorial has two parts:
16+
17+
1. [Create a function app without default storage secrets in its definition][previous tutorial]
18+
2. Use identity-based connections instead of secrets with triggers and bindings (this article)
19+
20+
In Part 2, you'll learn how to:
21+
22+
> [!div class="checklist"]
23+
>
24+
> * Create a Service Bus namespace and queue.
25+
> * Configure your function app with managed identity
26+
> * Create a role assignment granting that identity permission to read from the Service Bus queue
27+
> * Create and deploy a function app with a Service Bus trigger.
28+
> * Verify your identity-based connection to Service Bus
29+
30+
## Prerequisite
31+
32+
> [!div class="checklist"]
33+
>
34+
> * 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].
35+
36+
## Create a service bus and queue
37+
38+
1. In the [Azure portal](https://portal.azure.com), choose **Create a resource (+)**.
39+
40+
1. On the **Create a resource** page, select **Integration** > **Service Bus**.
41+
42+
1. On the **Basics** page, use the following table to configure the Service Bus namespace settings. Use the default values for the remaining options.
43+
44+
| Option | Suggested value | Description |
45+
| ------------ | ---------------- | ---------------- |
46+
| **Subscription** | Your subscription | The subscription under which your resources are created. |
47+
| **[Resource group](../azure-resource-manager/management/overview.md)** | myResourceGroup | The resource group you created with your function app. |
48+
| **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. |
49+
| **[Location](https://azure.microsoft.com/regions/)** | myFunctionRegion | The region where you created your function app. |
50+
| **Pricing tier** | Basic | The basic Service Bus tier. |
51+
52+
1. Select **Review + create**. After validation finishes, select **Create**.
53+
54+
1. After deployment completes, select **Go to resource**.
55+
56+
1. In your new Service Bus namespace, select **+ Queue** to add a queue.
57+
58+
1. Type `myinputqueue` as the new queue's name and select **Create**.
59+
60+
Now, that you have a queue, you will add a role assignment to the managed identity of your function app.
61+
62+
## Configure your Service Bus trigger with a managed identity
63+
64+
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.
65+
66+
> [!NOTE]
67+
> 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.
68+
69+
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.
70+
71+
1. Click **Add** and select **add role assignment**.
72+
73+
1. Search for **Azure Service Bus Data Receiver**, select it, and click **Next**.
74+
75+
1. On the **Members** tab, under **Assign access to**, choose **Managed Identity**
76+
77+
1. Click **Select members** to open the **Select managed identities** panel.
78+
79+
1. Confirm that the **Subscription** is the one in which you created the resources earlier.
80+
81+
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.
82+
83+
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.
84+
85+
1. Click on your application. It should move down into the **Selected members** section. Click **Select**.
86+
87+
1. Back on the **Add role assignment** screen, click **Review + assign**. Review the configuration, and then click **Review + assign**.
88+
89+
You've granted your function app access to the service bus namespace using managed identities.
90+
91+
## Connect to Service Bus in your function app
92+
93+
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.
94+
95+
1. In your function app, select **Configuration** under **Settings**.
96+
97+
1. In **Application settings**, select **+ New application setting** to create the new setting in the following table.
98+
99+
| Name | Value | Description |
100+
| ------------ | ---------------- | ----------- |
101+
| **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. |
102+
103+
To learn more, see [Connection properties](functions-reference.md#connection-properties).
104+
105+
1. After you create the two settings, select **Save** > **Confirm**.
106+
107+
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.
108+
109+
## Add a Service Bus triggered function
110+
111+
1. Run the `func init` command, as follows, to create a functions project in a folder named LocalFunctionProj with the specified runtime:
112+
113+
```csharp
114+
func init LocalFunctionProj --dotnet
115+
```
116+
117+
1. Navigate into the project folder:
118+
119+
```console
120+
cd LocalFunctionProj
121+
```
122+
123+
1. In the root project folder, run the following commands:
124+
125+
```command
126+
dotnet remove package Microsoft.Azure.Webjobs.Extensions.ServiceBus
127+
dotnet add package Microsoft.Azure.Webjobs.Extensions.ServiceBus --prerelease
128+
```
129+
130+
This replaces the default version of the Service Bus extension package with a version that supports managed identities.
131+
132+
1. Run the following command to add a Service Bus triggered function to the project:
133+
134+
```csharp
135+
func new --name ServiceBusTrigger --template ServiceBusQueueTrigger
136+
```
137+
138+
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.
139+
140+
1. Open the new ServiceBusTrigger.cs project file and replace the `ServiceBusTrigger` class with the following code:
141+
142+
```csharp
143+
public static class ServiceBusTrigger
144+
{
145+
[FunctionName("ServiceBusTrigger")]
146+
public static void Run([ServiceBusTrigger("myinputqueue",
147+
Connection = "ServiceBusConnection")]string myQueueItem, ILogger log)
148+
{
149+
log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
150+
}
151+
}
152+
```
153+
154+
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.
155+
156+
> [!NOTE]
157+
> 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#local-development-with-identity-based-connections).
158+
159+
## Publish the updated project
160+
161+
1. Run the following command to locally generate the files needed for the deployment package:
162+
163+
```console
164+
dotnet publish --configuration Release
165+
```
166+
167+
1. Browse to the `\bin\Release\netcoreapp3.1\publish` subfolder and create a .zip file from its contents.
168+
169+
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:
170+
171+
```azurecli
172+
az functionapp deploy -n FUNCTION_APP_NAME -g RESOURCE_GROUP_NAME --src-path PATH_TO_ZIP
173+
```
174+
175+
Now that you have updated the function app with the new trigger, you can verify that it works using the identity.
176+
177+
## Validate your changes
178+
179+
1. In the portal, search for `Application Insights` and select **Application Insights** under **Services**.
180+
181+
1. In **Application Insights**, browse or search for your named instance.
182+
183+
1. In your instance, select **Live Metrics** under **Investigate**.
184+
185+
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.
186+
187+
1. Select your queue named `myinputqueue`.
188+
189+
1. Select **Service Bus Explorer** from the left blade.
190+
191+
1. Send a test message.
192+
193+
1. Select your open **Live Metrics** tab and see the Service Bus queue execution.
194+
195+
Congratulations! You have successfully set up your Service Bus queue trigger with a managed identity!
196+
197+
[!INCLUDE [clean-up-section-portal](../../includes/clean-up-section-portal.md)]
198+
199+
## Next steps
200+
201+
In this tutorial, you created a function app with identity-based connections.
202+
203+
Use the following links to learn more Azure Functions with identity-based connections:
204+
205+
- [Managed identity in Azure Functions](../app-service/overview-managed-identity.md)
206+
- [identity-based connections in Azure Functions](./functions-reference.md#configure-an-identity-based-connection)
207+
- [Connecting to host storage with an Identity](./functions-reference.md#connecting-to-host-storage-with-an-identity)
208+
- [Functions documentation for local development](./functions-reference.md#local-development-with-identity-based-connections)
209+
210+
[previous tutorial]: functions-managed-identity-tutorial.md

0 commit comments

Comments
 (0)