|
| 1 | +--- |
| 2 | +title: Change storage account access keys |
| 3 | +titleSuffix: Azure Machine Learning service |
| 4 | +description: Learn how to change the access keys for the Azure Storage account used by your workspace. Azure Machine Learning service uses an Azure Storage account to store data and models. When you regenerate the access key for the storage account, you must update the Azure Machine Learning service to use the new keys. |
| 5 | +services: machine-learning |
| 6 | +ms.service: machine-learning |
| 7 | +ms.subservice: core |
| 8 | +ms.topic: conceptual |
| 9 | +ms.author: aashishb |
| 10 | +author: aashishb |
| 11 | +ms.reviewer: larryfr |
| 12 | +ms.date: 05/17/2019 |
| 13 | + |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +# Regenerate storage account access keys |
| 18 | + |
| 19 | +Learn how to change the access keys for Azure Storage accounts used by the Azure Machine Learning service. Azure Machine Learning can use storage accounts to store data or trained models. |
| 20 | + |
| 21 | +For security purposes, you may need to change the access keys for an Azure Storage account. When you regenerate the access key, Azure Machine Learning must be updated to use the new key. Azure Machine Learning may be using the storage account for both model storage and as a datastore. |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +* An Azure Machine Learning service workspace. For more information, see the [Create a workspace](setup-create-workspace.md) article. |
| 26 | + |
| 27 | +* The [Azure Machine Learning SDK](https://docs.microsoft.com/python/api/overview/azure/ml/install?view=azure-ml-py). |
| 28 | + |
| 29 | +* The [Azure Machine Learning CLI extension](reference-azure-machine-learning-cli.md). |
| 30 | + |
| 31 | +<a id="whattoupdate"></a> |
| 32 | + |
| 33 | +## What needs to be updated |
| 34 | + |
| 35 | +Storage accounts can be used by the Azure Machine Learning service workspace (storing logs, models, snapshots, etc.) and as a datastore. The process to update the workspace is a single Azure CLI command, and can be ran after updating the storage key. The process of updating datastores is more involved, and requires discovering what datastores are currently using the storage account and then re-registering them. |
| 36 | + |
| 37 | +> [!IMPORTANT] |
| 38 | +> Update the workspace using the Azure CLI, and the datastores using Python, at the same time. Updating only one or the other is not sufficient, and may cause errors until both are updated. |
| 39 | +
|
| 40 | +To discover the storage accounts that are used by your datastores, use the following code: |
| 41 | + |
| 42 | +```python |
| 43 | +import azureml.core |
| 44 | +from azureml.core import Workspace, Datastore |
| 45 | + |
| 46 | +ws = Workspace.from_config() |
| 47 | + |
| 48 | +default_ds = ws.get_default_datastore() |
| 49 | +print("Default datstore: " + default_ds.name + ", storage account name: " + default_ds.account_name + ", container name: " + ds.container_name) |
| 50 | + |
| 51 | +datastores = ws.datastores |
| 52 | +for name, ds in datastores.items(): |
| 53 | + if ds.datastore_type == "AzureBlob" or ds.datastore_type == "AzureFile": |
| 54 | + print("datastore name: " + name + ", storage account name: " + ds.account_name + ", container name: " + ds.container_name) |
| 55 | +``` |
| 56 | + |
| 57 | +This code looks for any registered datastores that use Azure Storage and lists the following information: |
| 58 | + |
| 59 | +* Datastore name: The name of the datastore that the storage account is registered under. |
| 60 | +* Storage account name: The name of the Azure Storage account. |
| 61 | +* Container: The container in the storage account that is used by this registration. |
| 62 | + |
| 63 | +If an entry exists for the storage account that you plan on regenerating access keys for, save the datastore name, storage account name, and container name. |
| 64 | + |
| 65 | +## Update the access key |
| 66 | + |
| 67 | +To update Azure Machine Learning service to use the new key, use the following steps: |
| 68 | + |
| 69 | +> [!IMPORTANT] |
| 70 | +> Perform all steps, updating both the workspace using the CLI, and datastores using Python. Updating only one or the other may cause errors until both are updated. |
| 71 | +
|
| 72 | +1. Regenerate the key. For information on regenerating an access key, see the [Manage a storage account](/azure/storage/common/storage-account-manage.md#access-keys) article. Save the new key. |
| 73 | + |
| 74 | +1. To update the workspace to use the new key, use the following steps: |
| 75 | + |
| 76 | + 1. To sign in to the Azure subscription that contains your workspace by using the following Azure CLI command: |
| 77 | + |
| 78 | + ```azurecli-interactive |
| 79 | + az login |
| 80 | + ``` |
| 81 | +
|
| 82 | + 1. To install the Azure Machine Learning extension, use the following command: |
| 83 | +
|
| 84 | + ```azurecli-interactive |
| 85 | + az extension add -n azure-cli-ml |
| 86 | + ``` |
| 87 | +
|
| 88 | + 1. To update the workspace to use the new key, use the following command. Replace `myworkspace` with your Azure Machine Learning workspace name, and replace `myresourcegroup` with the name of the Azure resource group that contains the workspace. |
| 89 | +
|
| 90 | + ```azurecli-interactive |
| 91 | + az ml workspace sync-keys -w myworkspace -g myresourcegroup |
| 92 | + ``` |
| 93 | +
|
| 94 | + This command automatically syncs the new keys for the Azure storage account used by the workspace. |
| 95 | +
|
| 96 | +1. To re-register datastore(s) that use the storage account, use the values from the [What needs to be updated](#whattoupdate) section and the key from step 1 with the following code: |
| 97 | +
|
| 98 | + ```python |
| 99 | + ds = Datastore.register_azure_blob_container(workspace=ws, |
| 100 | + datastore_name='your datastore name', |
| 101 | + container_name='your container name', |
| 102 | + account_name='your storage account name', |
| 103 | + account_key='new storage account key', |
| 104 | + overwrite=True) |
| 105 | + ``` |
| 106 | +
|
| 107 | + Since `overwrite=True` is specified, this code overwrites the existing registration and updates it to use the new key. |
| 108 | +
|
| 109 | +## Next steps |
| 110 | +
|
| 111 | +For more information on registering datastores, see the [`Datastore`](https://docs.microsoft.com/python/api/azureml-core/azureml.core.datastore(class)?view=azure-ml-py) class reference. |
0 commit comments