Tired of having to load client tools on your computer every time you want to manage something in Microsoft Azure Cloud? Look no further! Using the Azure Cloud Shell, all you need is a browser to manage Azure from the command line.
Command-line tools like Terraform, Azure CLI the Azure PowerShell module, and more are freely available right in your browser. In this article, you will learn how to set up and configure Azure Cloud Shell and see what tools it has in store.
Let’s get into it!
Table of Contents
- An Azure Cloud account (Free Tier will work)
- An Azure Storage Account which will incur a small cost for the storage of your files.
- Visual Studio Code – This tutorial is using v1.58.
- NodeJS – This tutorial is using v14.17.3.
What is Azure Cloud Shell?
The Azure Cloud Shell is a browser-accessible shell for managing Azure resources in Bash or PowerShell.
Azure Cloud Shell runs in a container or a lightweight image containing application software that can run on top of an operating system. When you start up a new Azure Cloud Shell, Azure creates a Linux container image and stores that container image stored in your storage account.
Since containers are transient, to store files over multiple sessions, Azure Cloud Shell creates an Azure file share to securely store your data files. Azure Cloud Shell mounts this file share in your Cloud Shell home directory or mounts the file share as a separate drive labeled “clouddrive”.
Setting Up The Azure Cloud Shell
Now that you know the basics of what Cloud Shell is and how it works, let’s get into spinning one up for the first time!
- Open your favorite web browser, navigate to the Azure portal, http://shell.azure.com and log into your Azure account, if required.
2. The first time you launch Cloud Shell, you’ll see the confirmation pop-up below. Set PowerShell as your default shell by clicking on PowerShell. You can optionally choose the Bash shell also but this tutorial will be using PowerShell.
3. Now, click on Show advanced settings in the dialog box, as shown below.
You could just select Create Storage in this box, but in this tutorial, you’re going to drill a bit deeper into how Cloud Shell is configured and also how it works.
Fill in the required fields as explained below and click on Create Storage.
- Subscription – The Azure subscription you’re creating all of the Cloud Shell resources under.
- Cloud Shell Region – The region you’d like to deploy the Cloud Shell resources to.
- Show vNet isolation settings – Allows the cloud shell to access virtual machines and other resources behind a firewall or using private addresses within Azure.
- Resource Group, Storage Account, and File Share – Select any existing items here or select the Create new option and provide a name for each. This file share will be automatically mounted in your Cloud Shell session and called clouddrive.
Once you’ve provided the required parameters, you’ll be dropped into your Azure Cloud Shell session. Since you previously set PowerShell as your default shell, you’ll be taken into a PowerShell environment, as shown below.
If you prefer working in a Bash environment, click on the drop-down option at the top-left corner of the shell and select Bash, as shown below.
Getting Cloud Drive Information
Now that you have Azure Cloud Shell running, it’s time to run some commands. You’re in a real shell with thousands of commands and utilities available but this tutorial is going to only focus on a few.
All commands will be run in the PowerShell console, but Bash provides similar functionality using the Azure CLI.
Let’s first inspect the clouddrive by running the PowerShell cmdlet
Get-CloudDrive, as shown below. You’ll see that running the
Get-CloudDrive command returns various information about your clouddrive including the FileSharePath where you can access your clouddrive.
The FileSharePath property is where Azure Cloud Shell mounted the Azure file shared created when you were setting up the Cloud Shell.
Accessing the Azure Drive to Navigate Azure Resources
The Azure Cloud Shell comes pre-loaded with various tools for you; one of those tools is the Azure drive. The Azure drive is a handy PowerShell drive that exposes your Azure resources as a file system based on Simple Hierarchy in PowerShell (SHiPS).
1. Navigate to your Azure PS Drive by changing to the drive just as you would your file system. You can tell that you are now in the Azure drive from the Azure:\> prompt below.
2. Next, view your current Azure subscription by running Get-ChildItem or the PowerShell alias dir. Notice below that the current subscription is Pay-As-You-Go.
3. Drill down deeper by changing to the Pay-As-You-Go “folder” and run dir again to view its contents.
cd ./Pay-As-You-Go/ dir
In the image below, you’ll see all Azure resources available in your subscription.
For more examples of navigating around Azure resources, look at the Azure PSDrive repository on GitHub.
4. Now, navigate to any of your Azure resources by running the command below, replacing <resources> with your preferred resources name.
For example, you can navigate to the Cloud Shell’s Azure Files share by going through the storage account the file share is created on, as shown below.
cd ./StorageAccounts/<storageaccountname>/Files/cloudgroup dir
All Resources in Azure
Uploading A File To Azure via Cloud Shell
You have a clouddrive ready to store files so let’s now upload some to it and see how that works.
- Pick or create a file on your local machine. This tutorial will use a file called mycloudshelltextfile.txt.
2. Click on the upload/download icon in the Cloud Shell toolbar, click Upload and select a file on your local machine (this tutorial will use mycloudshelltextfile.txt). By default, when you upload a file, it’s saved in your home folder (/home/%USERNAME%), not on your clouddrive.
At the bottom-right of the page, you’ll see a confirmation saying the file upload process is complete.
3. Next, copy the file (mycloudshelltextfile.txt) to your clouddrive with the Copy-Item PowerShell command.
The command below specifies the file’s name to upload (./mycloudshelltextfile.txt) and defines the destination path to /home/<username>/clouddrive. Add the optional Verbose parameter to display a detailed output of the result.
Copy-Item ./mycloudshelltextfile.txt /home/<username>/clouddrive -Verbose
4. Finally, run the Get-ChildItem command to view the contents of your clouddrive (/home/<username>/clouddrive). You’ll notice below that the mycloudshelltextfile.txt file is present on the clouddrive which will now persist over different Cloud Shell sessions.
Accessing Command Line Tools Available in Cloud Shell
Learning the basics is fun but let’s take this Azure Cloud Shell experience up a notch. Cloud Shell supports a wide variety of tools that are accessible from both PowerShell and Bash. You’ll get to experience some of these tools first hand, so let’s dive in!
Cloning a Git Repository
If you’re a Git fan, then you’re in luck. The Azure Cloud Shell container comes with Git installed by default. Let’s go through one of the basic Git commands (
git-clone) to clone a repository to your clouddrive.
Maybe you have a favorite GitHub repository you’d like to work on in Cloud Shell. No problem, just run
git clone and provide the repository’s URL. The example below is navigating to the clouddrive first to ensure the cloned repo is available when the current Cloud Shell session is ended.
cd clouddrive git clone <https://github.com/Nick-Rimmer2016/Terraform.git>
Now see if the repository was cloned successfully by running the ls -l command.
Creating Resource Groups via Azure CLI
One of the handiest tools in Azure Cloud Shell, other than Git, is the Azure Command Line Interface (Azure CLI). Azure CLI lets you manage Azure resources from the command line across multiple platforms—talk about flexibility. As an example, let’s go over how to create a resource group in Azure CLI.
To create a resource group with the Azure CLI, run the az group create command. When you run the az group command below, you’re creating (create) a –resource-group named TESTGrp in the northeurope region and getting optional –verbose output.
az group create --location northeurope --resource-group TESTGrp --verbose
Last on the list of tools available in Azure Cloud Shell is Terraform, the infrastructure as code automation tool by Hashicorp.
This tutorial is not about Terraform but you can easily confirm Terraform is available in Azure Cloud Shell by checking the version.
You can see below that Microsoft has not yet updated to the latest version. The current version, 1.0.1, only came out last Jul 26, 2021.
Using The File Editor
The Azure Cloud Shell is not just a simple shell to run commands in. You can even edit files in a full-fledged file editor built from the open-source Monaco Editor!
- Open the editor by clicking on the curly braces in the title bar, as shown below. You’ll be presented with two panes, on the left-hand side are your files and on the right-hand side the actual editor.
2. Open a file in the editor by navigating to a file in the left pane and clicking on it. You’ll see the file is then opened in the right-hand pane, as shown below.
You can alternatively open a file directly from the command line by typing code <path to the file to open>.
3. If you’ve made some changes to the file and would like to save it, click on the ellipses shown below by the red arrow and click on Save.
4. Once done, close the editor by clicking on the ellipsis on the right-hand corner of the editor, as shown below, then select Close Editor.
Alternatively, you could hit Ctrl+Q to quit the editor.
Using the File Editor Command Palette
Just like Visual Studio Code, the built-in Azure Cloud Shell editor has a Command Palette. To access it, click on the ellipses in the upper right corner and click on Command Palette… This action will show you the options available in the command palette for editing your files.
Testing Web Applications with the Web Preview
This feature can be great for those times when you need to preview a web application before you actually deploy it into production.
To demonstrate the Web Preview feature, let’s create a web server with Python and see how to view the page it serves with Web Preview.
The following demonstration will cover Web Preview in a Bash shell but the exact same commands can be run in PowerShell also.
1. Start a simple web server with Python by running the below command. This command tells Python to bring up a web server that’s listening on port 9001.
python -m http.server 9001
There’s no need to install Python. It comes installed by default!
2. To view the page that the web server is providing, click on the Web Preview icon and click on Configure.
3. Now, provide the port that the webserver is listening on (9001 in this case) and click on Open and Browse. This action will render the web page that web server is providing.
You should now see a directory listing for the Azure Cloud Shell’s container filesystem.
4. Close the web page and go back to the shell and you’ll notice HTTP GET requests being logged from opening the web page rendered by the http server.
5. Finally, to shut down, the web server, type Ctrl-Z.
Miscellaneous Cloud Shell Actions
Not to forget some of the still useful but smaller features, let’s now cover some of the options you’ll find in the Cloud Shell’s toolbar.
If you need to restart or create a new session to bring up a new container in the background, the Cloud Shell toolbar is the way to go.
Note that the Cloud Shell has a timeout of 20 minutes. If you are idle for more than 20 minutes, the Cloud Shell will terminate the session and you will be forced to reconnect.
Restarting the Current Session
As mentioned earlier, Azure Cloud Shell spins up a new container when you start it. If, for some reason, you’d like to start over without closing and reopening your browser, click the restart icon.
Remember to save any of your work before doing a restart, as the container is destroyed and new one created!
Starting a New Session
Maybe you need to start a new session to work on a new project but want to use the environment you’re in. No problem. Click on the new session icon, as shown below, to start a new Cloud Shell session in another web browser tab.
Starting a new session via the toolbar does not create another container in the background. All of your files and state will still be available.
Changing Font and Font Size
Is the font or font size not up to your liking? Change it by clicking the gear icon, as shown below and selecting the Text size or Font.
Integrating Cloud Shell with Visual Studio Code
If you regularly find yourself in the Azure Cloud Shell but are also an avid VS Code user, there’s no need to switch back and forth. Instead, integrate the Cloud Shell right in to VS Code! VS Code is a popular code editor and lets you leverage its features while taking advantage of Azure extensions‘ extensive library for VS Code at the same time.
Let’s now see what it takes to integrate the Cloud Shell in VS Code.
1. Open VS Code.
2. Next, install the Azure Account extension by clicking on the extensions icon at the Activity Bar, searching for ‘Azure Account,’ and clicking Install.
3. Open the Command Palette by clicking on the View menu —> Command Palette. Here you can find the command to initiate an Azure Cloud Shell.
4. Now launch Cloud Shell in a PowerShell shell, for example, by clicking on the Azure: Open PowerShell in Cloud Shell in the Command Palette.
You must have to type ‘Azure: open’ to filter the list of available commands.
5. If you are not signed in through the Azure Account extension, VS Code will prompt you to authenticate. To do so, click on Sign in, and you’ll be taken to a webpage where you’ll sign in with your Azure account.
6. In the web browser, click on the account to sign in and enter your password. Once you provide your credentials, VS Code will see this and should be authenticated.
7. Finally, move back to VS Code, and you will see that your VS Code terminal has changed to an active Cloud Shell session. You can now run any command you wish just as if you were in your browser.
8. To close the session, select Azure: Sign Out from the Command Palette, as shown in the screenshot below:
This article aims to whet your appetite and see what you can achieve with Cloud Shell. And having walked through this tutorial, you now understand what Azure Cloud Shell is. You’ve also learned how to access powerful tools within Cloud Shell to manage your Azure resources.
Now it’s time to leverage those tools to build your projects! Which Azure Cloud Shell feature piqued your interest the most?
More from Adam The Automator & Friends