Tutorial: Use a Linux VM system-assigned managed identity to access Azure Storage
|Managed identities for Azure resources is a feature of Azure Active Directory. Each of the Azure services that support managed identities for Azure resources are subject to their own timeline. Make sure you review the availability status of managed identities for your resource and known issues before you begin.|
This tutorial shows you how to use a system-assigned managed identity for a Linux virtual machine (VM) to access Azure Storage. You learn how to:
- Create a storage account
- Create a blob container in a storage account
- Grant the Linux VM's Managed Identity access to an Azure Storage container
- Get an access token and use it to call Azure Storage
Azure Active Directory authentication for Azure Storage is in public preview.
- If you're not familiar with the managed identities for Azure resources feature, see this overview. If you don't have an Azure account, sign up for a free account before you continue.
- To perform the required resource creation and role management, your account needs "Owner" permissions at the appropriate scope (your subscription or resource group). If you need assistance with role assignment, see Use Role-Based Access Control to manage access to your Azure subscription resources.
- Sign in to Azure portal
- Create a virtual machine with system-assigned identity enabled
To run the CLI script examples in this tutorial, you have two options:
- Use Azure Cloud Shell either from the Azure portal, or via the Try It button, located in the top right corner of each code block.
- Install the latest version of CLI 2.0 (2.0.23 or later) if you prefer to use a local CLI console.
Create a storage account
In this section, you create a storage account.
Click the + Create a resource button found on the upper left-hand corner of the Azure portal.
Click Storage, then Storage account - blob, file, table, queue.
Under Name, enter a name for the storage account.
Deployment model and Account kind should be set to Resource manager and Storage (general purpose v1).
Ensure the Subscription and Resource Group match the ones you specified when you created your VM in the previous step.
Create a blob container and upload a file to the storage account
Files require blob storage so you need to create a blob container in which to store the file. You then upload a file to the blob container in the new storage account.
Navigate back to your newly created storage account.
Under Blob Service, click Containers.
Click + Container on the top of the page.
Under New container, enter a name for the container and under Public access level keep the default value .
Using an editor of your choice, create a file titled hello world.txt on your local machine. Open the file and add the text (without the quotes) "Hello world! :)" and then save it.
Upload the file to the newly created container by clicking on the container name, then Upload
In the Upload blob pane, under Files, click the folder icon and browse to the file hello_world.txt on your local machine, select the file, then click Upload.
Grant your VM access to an Azure Storage container
You can use the VM's managed identity to retrieve the data in the Azure storage blob.
Navigate back to your newly created storage account.
Click the Access control (IAM) link in the left panel.
Click + Add role assignment on top of the page to add a new role assignment for your VM.
Under Role, from the dropdown, select Storage Blob Data Reader.
In the next dropdown, under Assign access to, choose Virtual Machine.
Next, ensure the proper subscription is listed in Subscription dropdown and then set Resource Group to All resource groups.
Under Select, choose your VM and then click Save.
Get an access token and use it to call Azure Storage
Azure Storage natively supports Azure AD authentication, so it can directly accept access tokens obtained using a Managed Identity. This is part of Azure Storage's integration with Azure AD, and is different from supplying credentials on the connection string.
To complete the following steps, you need to work from the VM created earlier and you need an SSH client to connect to it. If you are using Windows, you can use the SSH client in the Windows Subsystem for Linux. If you need assistance configuring your SSH client's keys, see How to Use SSH keys with Windows on Azure, or How to create and use an SSH public and private key pair for Linux VMs in Azure.
In the Azure portal, navigate to Virtual Machines, go to your Linux virtual machine, then from the Overview page click Connect. Copy the string to connect to your VM.
Connect to the VM with the SSH client of your choice.
In the terminal window, using CURL, make a request to the local Managed Identity endpoint to get an access token for Azure Storage.
curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fstorage.azure.com%2F' -H Metadata:true
Now use the access token to access Azure Storage, for example to read the contents of the sample file which you previously uploaded to the container. Replace the values of
<CONTAINER NAME>, and
<FILE NAME>with the values you specified earlier, and
<ACCESS TOKEN>with the token returned in the previous step.
curl https://<STORAGE ACCOUNT>.blob.core.windows.net/<CONTAINER NAME>/<FILE NAME> -H "x-ms-version: 2017-11-09" -H "Authorization: Bearer <ACCESS TOKEN>"
The response contains the contents of the file:
Hello world! :)
In this tutorial, you learned how enable a Linux VM system-assigned managed identity to access Azure Storage. To learn more about Azure Storage see:
Send feedback about: