|
| 1 | +--- |
| 2 | +title: Connect to Azure SQL Managed Instance using managed identity |
| 3 | +titleSuffix: Azure Cognitive Search |
| 4 | +description: Learn how to set up an Azure Cognitive Search indexer connection to an Azure SQL Managed Instance using a managed identity |
| 5 | + |
| 6 | +author: gmndrg |
| 7 | +ms.author: gimondra |
| 8 | +manager: liamca |
| 9 | + |
| 10 | +ms.service: cognitive-search |
| 11 | +ms.topic: conceptual |
| 12 | +ms.date: 05/03/2022 |
| 13 | +--- |
| 14 | + |
| 15 | +# Set up an indexer connection to Azure SQL Managed Instance using a managed identity |
| 16 | + |
| 17 | +This article describes how to set up an Azure Cognitive Search indexer connection to [SQL Managed Instance](/azure/azure-sql/managed-instance/sql-managed-instance-paas-overview) using a managed identity instead of providing credentials in the connection string. |
| 18 | + |
| 19 | +You can use a system-assigned managed identity or a user-assigned managed identity (preview). Managed identities are Azure AD logins and require Azure role assignments to access data in SQL Managed Instance. |
| 20 | + |
| 21 | +Before learning more about this feature, it is recommended that you have an understanding of what an indexer is and how to set up an indexer for your data source. More information can be found at the following links: |
| 22 | + |
| 23 | +* [Indexer overview](search-indexer-overview.md) |
| 24 | +* [SQL Managed Instance indexer](search-howto-connecting-azure-sql-database-to-azure-search-using-indexers.md) |
| 25 | + |
| 26 | +## Prerequisites |
| 27 | + |
| 28 | +* [Create a managed identity](search-howto-managed-identities-data-sources.md) for your search service. |
| 29 | + |
| 30 | +* Azure AD admin role on SQL Managed Instance: |
| 31 | + |
| 32 | + To assign read permissions on SQL Managed Instance, you must be an Azure Global Admin with a SQL Managed Instance. See [Configure and manage Azure AD authentication with SQL Managed Instance](/azure/azure-sql/managed-instance/authentication-aad-configure) and follow the steps to provision an Azure AD admin (SQL Managed Instance). |
| 33 | + |
| 34 | +* [Configure public endpoint and NSG in SQL Managed Instance](search-howto-connecting-azure-sql-mi-to-azure-search-using-indexers.md) to allow connections from Azure Cognitive Search. |
| 35 | + |
| 36 | +## 1 - Assign permissions to read the database |
| 37 | + |
| 38 | +Follow these steps to assign the search service system managed identity permission to read the SQL Managed database. |
| 39 | + |
| 40 | +1. Connect to your SQL Managed Instance through SQL Server Management Studio (SSMS) by using one of the following methods: |
| 41 | + |
| 42 | + - [Configure a point-to-site connection from on-premises](/azure/azure-sql/managed-instance/point-to-site-p2s-configure) |
| 43 | + - [Configure an Azure VM](/azure/azure-sql/managed-instance/connect-vm-instance-configure) |
| 44 | + |
| 45 | +1. Authenticate with your Azure AD account. |
| 46 | + |
| 47 | + :::image type="content" source="./media/search-index-azure-sql-managed-instance-with-managed-identity/sql-login.png" alt-text="Showing screenshot of the Connect to Server dialog."::: |
| 48 | + |
| 49 | +3. From the left pane, locate the SQL database you will be using as data source for indexing and right-click it. Select **New Query**. |
| 50 | + |
| 51 | + :::image type="content" source="./media/search-index-azure-sql-managed-instance-with-managed-identity/new-sql-query.png" alt-text="Showing screenshot of new SQL query."::: |
| 52 | + |
| 53 | + |
| 54 | +4. In the T-SQL window, copy the following commands and include the brackets around your search service name. Click on **Execute**. |
| 55 | + |
| 56 | + |
| 57 | + ```tsql |
| 58 | + CREATE USER [insert your search service name here or user-assigned managed identity name] FROM EXTERNAL PROVIDER; |
| 59 | + EXEC sp_addrolemember 'db_datareader', [insert your search service name here or user-assigned managed identity name]; |
| 60 | + ``` |
| 61 | +
|
| 62 | + :::image type="content" source="./media/search-index-azure-sql-managed-instance-with-managed-identity/execute-sql-query.png" alt-text="Showing screenshot of how to execute SQL query."::: |
| 63 | +
|
| 64 | +If you later change the search service system identity after assigning permissions, you must remove the role membership and remove the user in the SQL database, then repeat the permission assignment. Removing the role membership and user can be accomplished by running the following commands: |
| 65 | +
|
| 66 | + ```tsql |
| 67 | +sp_droprolemember 'db_datareader', [insert your search service name or user-assigned managed identity name]; |
| 68 | +
|
| 69 | +DROP USER IF EXISTS [insert your search service name or user-assigned managed identity name]; |
| 70 | +``` |
| 71 | + |
| 72 | +## 2 - Add a role assignment |
| 73 | + |
| 74 | +In this step you will give your Azure Cognitive Search service permission to read data from your SQL Managed Instance. |
| 75 | + |
| 76 | +1. In the Azure portal navigate to your SQL Managed Instance page. |
| 77 | +1. Select **Access control (IAM)**. |
| 78 | +1. Select **Add** then **Add role assignment**. |
| 79 | + |
| 80 | + :::image type="content" source="./media/search-index-azure-sql-managed-instance-with-managed-identity/access-control-add-role-assignment.png" alt-text="Showing screenshot of the Access Control page." lightbox="media/search-index-azure-sql-managed-instance-with-managed-identity/access-control-add-role-assignment.png"::: |
| 81 | + |
| 82 | + |
| 83 | +4. Select **Reader** role. |
| 84 | +1. Leave **Assign access to** as **Azure AD user, group or service principal**. |
| 85 | +1. If you're using a system-assigned managed identity, search for your search service, then select it. If you're using a user-assigned managed identity, search for the name of the user-assigned managed identity, then select it. Select **Save**. |
| 86 | + |
| 87 | + Example for SQL Managed Instance using a system-assigned managed identity: |
| 88 | + |
| 89 | + :::image type="content" source="./media/search-index-azure-sql-managed-instance-with-managed-identity/add-role-assignment.png" alt-text="Showing screenshot of the member role assignment."::: |
| 90 | + |
| 91 | +## 3 - Create the data source |
| 92 | + |
| 93 | +Create the data source and provide a system-assigned managed identity. |
| 94 | + |
| 95 | +### System-assigned managed identity |
| 96 | + |
| 97 | +The [REST API](/rest/api/searchservice/create-data-source), Azure portal, and the [.NET SDK](/dotnet/api/azure.search.documents.indexes.models.searchindexerdatasourceconnection) support system-assigned managed identity. |
| 98 | + |
| 99 | +When you're connecting with a system-assigned managed identity, the only change to the data source definition is the format of the "credentials" property. You'll provide an Initial Catalog or Database name and a ResourceId that has no account key or password. The ResourceId must include the subscription ID of SQL Managed Instance, the resource group of SQL Managed instance, and the name of the SQL database. |
| 100 | + |
| 101 | +Here is an example of how to create a data source to index data from a storage account using the [Create Data Source](/rest/api/searchservice/create-data-source) REST API and a managed identity connection string. The managed identity connection string format is the same for the REST API, .NET SDK, and the Azure portal. |
| 102 | + |
| 103 | +```http |
| 104 | +POST https://[service name].search.windows.net/datasources?api-version=2020-06-30 |
| 105 | +Content-Type: application/json |
| 106 | +api-key: [admin key] |
| 107 | +
|
| 108 | +{ |
| 109 | + "name" : "sql-mi-datasource", |
| 110 | + "type" : "azuresql", |
| 111 | + "credentials" : { |
| 112 | + "connectionString" : "Database=[SQL database name];ResourceId=/subscriptions/[subscription ID]/resourcegroups/[resource group name]/providers/Microsoft.Sql/managedInstances/[SQL Managed Instance name];Connection Timeout=100;" |
| 113 | + }, |
| 114 | + "container" : { |
| 115 | + "name" : "my-table" |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +## 4 - Create the index |
| 121 | + |
| 122 | +The index specifies the fields in a document, attributes, and other constructs that shape the search experience. |
| 123 | + |
| 124 | +Here's a [Create Index](/rest/api/searchservice/create-index) REST API call with a searchable `booktitle` field: |
| 125 | + |
| 126 | +```http |
| 127 | +POST https://[service name].search.windows.net/indexes?api-version=2020-06-30 |
| 128 | +Content-Type: application/json |
| 129 | +api-key: [admin key] |
| 130 | +
|
| 131 | +{ |
| 132 | + "name" : "my-target-index", |
| 133 | + "fields": [ |
| 134 | + { "name": "id", "type": "Edm.String", "key": true, "searchable": false }, |
| 135 | + { "name": "booktitle", "type": "Edm.String", "searchable": true, "filterable": false, "sortable": false, "facetable": false } |
| 136 | + ] |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## 5 - Create the indexer |
| 141 | + |
| 142 | +An indexer connects a data source with a target search index, and provides a schedule to automate the data refresh. Once the index and data source have been created, you're ready to create the indexer. |
| 143 | + |
| 144 | +Here's a [Create Indexer](/rest/api/searchservice/create-indexer) REST API call with an Azure SQL indexer definition. The indexer will run when you submit the request. |
| 145 | + |
| 146 | +```http |
| 147 | +POST https://[service name].search.windows.net/indexers?api-version=2020-06-30 |
| 148 | +Content-Type: application/json |
| 149 | +api-key: [admin key] |
| 150 | +
|
| 151 | +{ |
| 152 | + "name" : "sql-mi-indexer", |
| 153 | + "dataSourceName" : "sql-mi-datasource", |
| 154 | + "targetIndexName" : "my-target-index" |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +## Troubleshooting |
| 159 | + |
| 160 | +If you get an error when the indexer tries to connect to the data source that says that the client is not allowed to access the server, take a look at [common indexer errors](./search-indexer-troubleshooting.md). |
| 161 | + |
| 162 | +You can also rule out any firewall issues by trying the connection with and without restrictions in place. |
| 163 | + |
| 164 | +## See also |
| 165 | + |
| 166 | +* [SQL Managed Instance and Azure SQL Database indexer](search-howto-connecting-azure-sql-database-to-azure-search-using-indexers.md) |
0 commit comments