Logging your command line into Azure
13 Mar 2023
To rent resources from Microsoft Azure’s public cloud, we need to be logged into it.
Prerequisites
- Have an account in Azure. You can get one:
- From Microsoft for free but with a credit card on file.
- From Microsoft for free without a credit card as a student.
- From Microsoft without a credit card through a Visual Studio subscription if you have one at work.
- For 4 hours at a time through an A Cloud Guru subscription (personally, I’d rather get 1 yearly charge from ACG than give Microsoft my credit card and expose myself to getting hacked and running up an “infinity” bill).
- You must have the Azure command line interface tool (“Azure CLI”) or PowerShell installed onto your local computer.
- If you’re on Windows, there’s a good chance PowerShell came with your computer.
- If using PowerShell, you must also have the
Az
PowerShell module installed onto your local computer.- If you don’t have admin rights to your computer, make sure you use the “
-Scope CurrentUser
” parameter as part of your installation process so that you don’t get prompted for an admin password to the computer.
- If you don’t have admin rights to your computer, make sure you use the “
Log PowerShell into Azure
- At the PowerShell command prompt, execute the following command:
Connect-AzAccount -UseDeviceAuthentication
- Look at PowerShell’s response to you. It should say something like the following message, only with a unique code in place of “
XXXXXX
.” Copy the code where “XXXXXX
” would be onto your computer’s clipboard:“WARNING: To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code XXXXXX to authenticate.”
- Open a brand new incognito/private web browser tab in a browser you don’t already have incognito/private sessions open in and use it to visit https://microsoft.com/devicelogin.
- At the “Enter code” prompt in your incognito web browser, paste the “
XXXXXX
“-style code and click “Next.” - Enter the username and password to the Azure account you’d like to log into and click “Sign in.”.
- Tip: Don’t worry that the username prompt warns you that you’re signing into PowerShell “on another device.” In this case, you know you’re doing using PowerShell and logging into Azure on the same computer.
- (That said, this same technique can be used to use a totally different computer for PowerShell vs. the browser you’re logging in with, should you ever find yourself needing to do such a thing.)
- Tip: If you just spun up a playground from A Cloud Guru, the browser tab where you asked to do so should show you a username and password to use to log into your playground.
- Tip: Don’t worry that the username prompt warns you that you’re signing into PowerShell “on another device.” In this case, you know you’re doing using PowerShell and logging into Azure on the same computer.
- Click “Continue” when asked, in your incognito/private web browser tab:
“Are you trying to sign in to Microsoft Azure PowerShell? Only continue if you downloaded the app from a store or website that you trust.”
- You can leave this web browser open if you’re doing an exercise where you’ll also be playing around in the Azure portal. Alternatively, if you’re a neat freak, feel free to close your incognito/private web browser session when you see the following message:
“Microsoft Azure PowerShell: You have signed in to the Microsoft Azure PowerShell application on your device. You may now close this window.”
- Back in PowerShell, you should see output indicating an “
Account
,” a “SubscriptionName
,” and a “TenantId
” that feel appropriate, given the nature of the Azure account you chose to sign into. - Double-check that your PowerShell session is really logged into the Azure account you intended to be logged into by executing the following command and validating that the “
Name
” property of its output matches “SubscriptionName
” from above and that the “TenantId
” value also matches:Get-AzContext
- If they don’t match, substituting the first “
TenantId
” value from when you ranConnect-AzAccount -UseDeviceAuthentication
(not the second one fromGet-AzContext
), execute the following command:Set-AzContext -Tenant "your_tenant_id_goes_here"
- Now things should match the original
Connect-AzAccount -UseDeviceAuthentication
output when you rerun this command:Get-AzContext
- If they don’t match, substituting the first “
Log the Azure CLI into Azure
- At your computer’s command prompt, execute the following command:
az login --use-device-code
- Look at your computer’s command prompt’s response to you. It should say something like the following message, only with a unique code in place of “
XXXXXX
.” Copy the code where “XXXXXX
” would be onto your computer’s clipboard:“To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code XXXXXX to authenticate.”
- Open a brand new incognito/private web browser tab in a browser you don’t already have incognito/private sessions open in and use it to visit https://microsoft.com/devicelogin.
- At the “Enter code” prompt in your incognito web browser, paste the “
XXXXXX
“-style code and click “Next.” - Enter the username and password to the Azure account you’d like to log into and click “Sign in.”
- Tip: Don’t worry that the username prompt warns you that you’re signing into Microsoft Azure Cross-platform Command Line Interface “on another device.” In this case, you know you’re doing using the Azure CLI tool and logging into Azure on the same computer.
- (That said, this same technique can be used to use a totally different computer for the CLI tool vs. the browser you’re logging in with, should you ever find yourself needing to do such a thing.)
- Tip: If you just spun up a playground from A Cloud Guru, the browser tab where you asked to do so should show you a username and password to use to log into your playground.
- Tip: Don’t worry that the username prompt warns you that you’re signing into Microsoft Azure Cross-platform Command Line Interface “on another device.” In this case, you know you’re doing using the Azure CLI tool and logging into Azure on the same computer.
- Click “Continue” when asked, in your incognito/private web browser tab:
“Are you trying to sign in to Microsoft Azure CLI? Only continue if you downloaded the app from a store or website that you trust.”
- You can leave this web browser open if you’re doing an exercise where you’ll also be playing around in the Azure portal. Alternatively, if you’re a neat freak, feel free to close your incognito/private web browser session when you see the following message:
“Microsoft Azure Cross-platform Command Line Interface: You have signed in to the Microsoft Azure Cross-platform Command Line Interface application on your device. You may now close this window.”
- Back in your computer’s command prompt, you should see JSON-formatted output indicating, among other things, a “
name
,” a “tenantId
,” and a “user.name
” property that feel appropriate, given the nature of the Azure account you chose to sign into. - Double-check that your command-line session is really logged into the Azure account you intended to be logged into by executing the following command and validating that the output matches the output from
az login --use-device-code
az account show
- If they don’t match, substituting the “
name
” value from when you ranaz login --use-device-code
(not the second one fromaz account show
), execute the following command:az account set --name "your subscription name here"
- Now things should match the original
az login --use-device-code
output when you rerun this command:az account show
- If they don’t match, substituting the “
Posts in this series
- Part 1 - Source code that builds locally into a Node.js Hello World webapp
- Part 2 - Locally unit-testing source code for a Node.js Hello World webapp
- Part 3 - Protecting Git branches in Azure DevOps repositories
- Part 4 - Making Azure DevOps Pipelines build a Hello World webapp from Git-tracked source code changes
- Part 5 - Failing Azure DevOps Pipeline builds if unit tests fail
- Part 6 - This Article
- Part 7 - Provisioning an Azure Resource Group
- Part 8 - Provisioning Azure App Services to host your Hello World webapp
- Part 9 - Provisioning Azure AD Service Principals that can deploy built webapps onto your Azure App Service resources
- Part 10 - Provisioning Azure DevOps Service Connections that let ADO Release Pipelines leverage Azure AD Service Principals for sensitive CI/CD tasks
- Part 11 - Deploying a built webapp onto Azure App Service with ADO Release Pipelines