- Use Azure Bastion as a jump host for RDP and SSH - Tue, Apr 18 2023
- Azure Virtual Desktop: Getting started - Fri, Apr 14 2023
- Understanding Azure service accounts - Fri, Mar 31 2023
I hope by now you've moved from the Windows PowerShell Integrated Scripting Environment (PowerShell ISE) to Visual Studio Code for your administrative automation. Accordingly, your business might rely on the Microsoft Azure cloud a lot more than it has in years past.
If you've thought to yourself, "I wonder how VS Code integrates with Microsoft Azure?" then this tutorial is for you. By the end of this lesson, you'll be familiar with the major touchpoints between VS Code and Azure that can help you out regardless of whether your work is more operations- or development-focused. Let's begin!
Build your Azure development environment
Besides Visual Studio Code itself, the following are the baseline dependencies that I think you should have installed on your workstation to facilitate your work in Azure:
- Git client for source code version control
- Azure Command-Line Interface (CLI) for Azure control plane administration and automation
- Azure Developer CLI to accelerate Azure cloud development
- Azure PowerShell modules so you can continue to hone your PowerShell automation skills
- Bicep to support your Azure infrastructure-as-code (IaC) deployments
If you use the Chocolatey package manager, like I do, you can install all of the above (with the exception of the Azure Developer CLI) by running a single command:
choco install -y git vscode azure-cli az.powershell bicep
Until Microsoft uploads the Azure Developer CLI to the Chocolatey community package repository, you can run the following PowerShell command to install it:
powershell -ex AllSigned -c "Invoke-RestMethod 'https://aka.ms/install-azd.ps1' | Invoke-Expression"
You might already know that your flexibility with Visual Studio Code is dramatically improved by the use of extensions. Specifically, I'd like to introduce you to some key Microsoft Azure extensions that represent the "secret sauce" to VS Code-based Azure productivity.
Azure account management
The Azure Account extension should be the first VS Code extension you install. The reason is simple: you should be able to use VS Code for your Azure work without having to reauthenticate to your Azure subscription again and again and again.
Follow these steps if you haven't installed a VS Code extension yet:
- Open VS Code, and click the Extensions button in the Activity Bar.
- In the Search Extensions in Marketplace text box, search for the string @ext:azure. You could search only for azure; the @ext: prefix simply filters for only extensions and leaves behind stuff like color themes, icon packs, snippets, and the like.
- Select the Azure Account extension, and click Install. Some VS Code extensions require you to reload the application prior to their first use.
You can easily get to a particular extension's options by loading up the extension's Marketplace page, opening the gear menu, and selecting Extension settings, as shown in the following screenshot.
Visual Studio Code relies on keyboard shortcuts and the text-based Command Palette for most navigation and administration tasks. To get signed into Azure, click View > Command Palette, and type Azure:Sign in.
You'll find that every extension includes a command library, with each command prefixed by a fixed value. For the Azure account extension, that prefix is, simply enough, Azure.
Once you complete the browser-based authentication flow, you can use the Azure: Select subscriptions command from the Command Palette to ensure you're connected to the correct subscription.
As the following screenshot demonstrates, the VS Code status bar reminds you of which Azure subscription you're currently attached to.
Azure infrastructure as code
After you've done so, you now have full language support for both ARM JSON and the Bicep domain-specific language (DSL). You also get full template validation against the ARM deployment schemas.
Now create two new files, one with the .json extension and the other with the .bicep extension. Because you have the language extensions loaded, VS Code should report the correct file types on the status bar.
If, for whatever reason, VS Code auto-detects the file type association incorrectly, fix it by clicking the Select Language Mode status bar button and making the appropriate correction. I'll show you this in the next screenshot.
Of course, now that you have Azure PowerShell, the Azure CLI, and the Bicep CLI installed, you can deploy your Azure templates directly from the VS Code terminal. Here's an example of deploying a Bicep template using Azure PowerShell:
New-AzResourceGroupDeployment -Name 'bicep-deploy' -ResourceGroupName '4sysops' -TemplateFile './main.bicep'
If you're a Terraform shop, don't worry—Visual Studio Code has you covered. Install Terraform and then load up one of the VS Code language extensions:
What's the difference between these two extensions? My understanding is that the Microsoft one is essentially a wrapper over the Terraform CLI. Personally, I'm comfortable enough with the Terraform CLI and HashiCorp Configuration Language (HCL) that I use only HashiCorp's own extension to give me stuff like IntelliSense code completion and configuration validation. You know the saying: "Your mileage may vary."
Work with other Azure services
Thus far, our VS Code-based Azure work has shown the following high-level workflow:
- Install the appropriate binary dependencies and VS Code extensions.
- Configure the extension to suit your preferences.
- Ensure file types are associated with the correct extensions.
Let me wrap up this tutorial with a list of VS Code extensions to suit additional Microsoft Azure-based work situations:
- Azure DevOps: Give the Azure Pipelines extension a try for writing your YAML pipeline definitions.
- GitHub: GitHub's own GitHub Repositories, GitHub Pull Requests and Issues, and GitHub Codespaces extensions are go-tos for me. Also, GitHub Copilot can teach you some new tricks.
- Azure Virtual Machines: Microsoft's own Azure Virtual Machines extension's got you covered.
- Azure App Service: There's a VS Code extension for most Azure platform services, including App Service Web Apps, Functions, and databases.
- General: The Microsoft Azure Resources extension extends your view in VS Code to inventory and work with your entire Azure environment.
What's especially cool about the Microsoft Azure Resources extension is that VS Code will prompt you to install the appropriate extensions if you're missing one as you browse your Azure inventory.
Subscribe to 4sysops newsletter!
Well, that's it! I hope I gave you a head start in getting productive with your Azure cloud work in Visual Studio Code.
Want to write for 4sysops? We are looking for new authors.
I’ve been thinking of installing VS code on my machine to move away from ISE as I am slowly progressing in my DevOps journey. You saved me from a ton of research, thank you for this awesome post.
You’re welcome, Surender! I’m glad you liked the post. -Tim
Tim is a synonym for Lucidity.