Saturday, November 26, 2022

Azure/Visual Studio Code: Installing Azure Tools Extension and Overcoming Azure Login Issues

Visual Studio Code's Azure Tools Extension can be invaluable when developing for Azure. This extension installs a package of Azure-relate extensions including (this list was update November 13, 2023 to include an additional subordinate extension that was added to the Azure Tools Extension namely Azure Container Apps):

  • Azure App Service
  • Azure Static Web Apps
  • Azure Functions
  • Azure Storage
  • Azure Databases
  • Azure Virtual Machines
  • Azure Resources
  • Azure Developer CLI
  • Azure Container Apps

For instructions on how to install the Azure Tools extension for Visual Studio Code, see Appendix A at the end of this post.

To use the above extensions, such as Azure Resources, a successful Azure login is required. This login can be problematic because at times even with the correct credentials, the extension does not recognize the Azure login. This post describes how to get a Visual Studio Azure-related extension to recognize a successful Azur login.

To run the Azure Resources extension, which of course requires Azure login, click on the Azure icon Visual Studio Code's activity bar:

Using to the keyboard to simaltanously select the following keys, SHIFT-ALT-A, will also display the Azure Resources extension as follows:

The above instance of the Azure Resources extension is not currently logged into Azure.

Most of the time clicking on "Sign in to Azure..." above and entering the correct credentials logs in for the Azure Resources extension. There are times when nothing happens. The authentication is handled by a browser and the browser fails to communicate the successful login to the Azure Resources extension. Successfully logging in via the browser which launches one, two, three, or ten times fails to have Azure Resources login.

The steps I have found to get around this are to use PowerShell to perform the following steps from a Visual Studio Code terminal window running PowerShell as the shell: 

  1. clear the Azure context
  2. sign in to Azure
  3. set the Azure context to the desired subscription 

The process is straightforward and requires the name of the Azure subscription to be signed into.

To run PowerShell from Visual Studio, select the Terminal window and make sure that PowerShell is the shell used by the terminal windows. The keyboard sequence CTRL-` (CTRL-backtick) will also launch the Terminal window:

In the terminal windows clear the current Azure context by invoking the Clear-AzContext cmdlet as follows:

Clear-AzContext -Force -ErrorAction:Stop 

Run the Connect-AzAccount cmdlet which will launch a browser that will allow the Azure credentials to be provided:

Connect-AzAccount -ErrorAction:Stop 

Using the subscription name, set the current Azur context using the Set-AzContext cmdlet:

Set-AzContext -Subscription 'enter subscription name here' -ErrorAction:Stop | Out-Null

Set-AzContext -Subscription 'Dev-Test' -ErrorAction:Stop | Out-Null

An example of all the cmdlets run in the terminal window is as follows:

Once successfully logged in the Azure Resources extension appears as follows:

Obviously, where above it says "Dev-Test" a user will see the name of the subscription used to log in.

Appendix A: Installing Visual Studio Code's Azure Tools Extension

To install Azure Tools (or any extension) in Visual Studio, click on the extensions icon in the activity bar (the four boxes):

From the search box enter the name of the desired extension, Azure Tools:

Clicking on the Azure Tools entry displays the following:

Click on the Install button to install. Once install the activity bar will include an A-shaped-icon will appear at the bottom of the activity bar (see below):

No comments :

Post a Comment