Refactored - Top Rated Cloud Training

View Original

Understanding the Azure Cloud Shell

What is the Cloud Shell?

The Azure Cloud Shell is a command line run in a web browser for managing Azure Resources. It provides the flexibility of either Bash or PowerShell, allowing you to use the scripting language you are more familiar with or better for the job.

Open Cloud Shell in Azure Portal

With traditional az cli or PowerShell modules you need to run these from a local computer, but the cloud shell allows the scripts and commands to be run from a authenticated browser.

Microsoft manages the cloud shell themselves updating the supported version az cli and PowerShell ensuring you are using the latest versions. Your scripts, access keys and other files can also be stored with a storage account linked to the Cloud Shell. Any changes to the files will be immediately saved to the storage account.

Microsoft offers a variety of tools to streamline resource management in Azure, beyond just the AZ CLI and PowerShell. These include command-line utilities like Bash, Dig, and other Linux tools; AzCopy for efficient data transfers; and the Azure Functions CLI for working with serverless functions. Text editors such as Vim, Nano, and VS Code make file editing easier, while Git supports version control. For container orchestration, there are tools like kubectl and Helm. Additionally, package managers and infrastructure-as-code tools like npm, pip, Terraform, and Ansible facilitate code management and automation, giving developers a powerful toolkit for cloud resource management.

Azure Cloud Shell offers a secure, browser-based command-line session that you can access from any device. It allows interaction with Azure resources without the need for additional plug-ins or installations. Files are stored persistently between sessions, so you can access and reuse them later. You can choose between Bash and PowerShell, depending on your preference, and even edit files like scripts directly within the Cloud Shell editor.

However, Azure Cloud Shell may not be ideal if you plan to leave a session open for more than 20 minutes, as it disconnects automatically, causing any unsaved work to be lost. It’s also unsuitable if you need administrative permissions, like sudo access, or if you require tools that aren’t supported in the limited Cloud Shell environment. For more complex needs, such as accessing storage across multiple regions.


Bash

Bash is a tool for managing Linux machines. The name is short for "Bourne Again Shell."

Linux Bash Prompt

A shell is a program that communicates with the operating system to execute various commands. Users can input commands directly in a console or run scripts to automate multiple commands at once. Shells like PowerShell and Bash provide system administrators with the precise control they need to manage and configure systems effectively.

While other Linux shells exist, such as csh and zsh, Bash has become the standard for Linux. Bash not only integrates the best features of earlier shells but also introduces enhancements, including built-in commands and the flexibility to call external programs, making it a versatile tool for system management.

Bash’s success can be attributed to its simplicity, which aligns closely with the Unix design philosophy. As Peter Salus explains in A Quarter Century of Unix, three core principles underpin Unix’s approach: programs should do one thing and do it well, programs should work together, and text streams should serve as the universal interface.

This last principle is essential for understanding Bash. In Unix and Linux, everything operates as a file, allowing commands to function uniformly, regardless of whether the input or output comes from a keyboard, file, socket, pipe, or another I/O source. This text-based, file-oriented approach makes Bash a flexible and powerful tool across varied environments.

Common Bash Commands

ls command

The ls command displays the contents of a directory. When used without any arguments, it lists all files and directories in the current directory. You can also specify a directory as an argument to ls to view the contents of that specific location.

cat command

To view the contents of a file, you can use the cat command. This command outputs the file’s contents directly to the terminal, which is especially useful for reading text files.

sudo command

Certain Bash commands require root, or superuser, privileges to execute. These commands are typically reserved for system administrators to perform critical tasks. If you attempt to run one of these commands without the necessary permissions, it will fail due to insufficient privileges.

cd command

The cd command, short for "change directory," allows you to navigate between directories in the file system. By specifying the path of a directory as an argument, cd moves you from your current directory to the specified one. It functions similarly to the cd command in Windows, providing a straightforward way to move through directories.

rm command

The rm command, short for "remove," is used to delete files. When you use rm followed by a file name, it permanently deletes that file from the system. It’s a powerful command, so it’s often recommended to use it carefully, as removed files cannot be easily recovered.

cp command

The cp command is used to copy files and, if specified, entire directories along with their contents. By default, cp copies files, but by adding the -r (recursive) option, you can copy entire directories along with all subdirectories and files within them. This makes cp a versatile command for duplicating both single files and complex directory structures.

ps command

The ps command provides a snapshot of all the currently running processes on your system. By default, without any arguments, it shows only the processes running in your current shell session, which can be quite limited. However, when you add the -A flag (or -e), it displays all processes running on the system, giving you a comprehensive view of every active process across all users.

PowerShell

PowerShell started as a tool for managing and automating tasks within Windows. Overtime it has grown to be a cross-platform tool that can be used for various tasks. PowerShell is both a scripting language and command-line shell

PowerShell Prompt


If you are using Windows you will have PowerShell already installed on your machine, for other operating systems (Linux, macOS) you will need to download and install it from the microsoft website. Once the base PowerShell shell is installed you can installed modules for your specific requirements (Install-Module -Name az) installs the Az PowerShell module needed for managing Azure resources.

PowerShell commands or cmdlets (pronounced command-let) follow a standard naming convention of verb-noun. These cmdlets are built using .Net Core in recent versions and each module you install adds many specific cmdlets for varied commands. Eg Get-AzVM lists virtual machines currently created within you Azure subscription.

AZ CLI

Within this blog post we have discussed bash, PowerShell and the PowerShell modules. We also mentioned AZ CLI. The AZ CLI (Azure Command-Line Interface) is another cross-platform tool designed for managing and configuring Azure resources. With AZ CLI, you can create, modify, and delete Azure resources, manage subscriptions, and automate cloud workflows directly from the command line.

It was the original cross platform tool for Linux and macOS users to manage their Azure resources. In recent years, PowerShell has also become cross platform. So, the decision to use AZ CLI or PowerShell come down more to the developers preference, or the specific resources being managed, sometimes one tool is better for specific management tasks over the other.

AZ CLI Prompt

Summary

In this post we have looked at the Azure Cloud Shell and the associated command-line tools and scripting languages of bash, PowerShell and AZ CLI. All these tools offers capabilities to create and manage Azure Resources using the command line and scripted configuration.