Last week, during the Build 2017 event lots of announcements were made. Of course, there were big announcements like Azure Cosmos DB. But there was also this small announcement which, in my eyes, will change the way we manage Azure fundamentally.
Right in the keynote, the Azure Cloud Shell was announced and it was available right away.
The last couple of weeks, I have written multiple blogs about the Azure CLI. This is a great tool but running it on your PC locally and connecting it to your Azure subscription was not that easy. Now it’s right there in your browser! Just click on this icon:
Let’s look how we can make use of this feature and why it’s Azure on steroids!
This is part four of a series of blogs regarding the Azure CLI:
- Getting started with Azure Command Line Interface 2.0
- Working with the IoT Hub using Azure Command Line Interface
- Azure CLI made easy. Using the shell
- Azure portal on steroids or, Bash shell in your browser
Starting the shell for the first time
When you start with your very first session (by pressing that icon on top of the portal), you will get this dialog:
You are asked to accept the creation of some Azure Storage. And it has to run using one of your subscriptions. Although the Azure shell can actually run without this Azure Storage, Microsoft has made a clever addition to the shell. Create the storage. And right after the creation of the storage, you will see that this Azure Storage contains a File service. We will see why in a minute…
So the shell is started and a prompt is shown at the bottom of the screen:
This is a Bash shell. The dropdown icon at the top of the shell suggest there are more types of shells but at this moment only the Bash shell is available:
Let’s create a new Resource group, this is pretty straight forward:
This takes a few seconds and the Resource group is created. This is very simple, it only takes one line of code instead of multiple dialogs. Some JSON is created with the response of this command. Check it out.
Note: Output is not shown in the Notification of the Azure portal. This would be a great addition…
Logging command execution
Now the response is on the screen, it’s not very to share using copy/paste. Wouldn’t it be nice to log this execution to a file?
Well, this is one of the reasons the Azure Storage File Service is added for!
Just execute the next two lines:
cd clouddrive touch logging.txt
This way an empty file is written in the File Service just to proof we can write files (please refresh if needed):
So let’s create a Resource group and log the console output in a new file:
As you can see, a new file is created and it’s NOT empty. Let’s check out what’s in there:
The same JSON output as expected is added to the file. And we can download it, etc.
But on the other hand, the output towards the console is not shown anymore. This is not a problem if you are pretty sure about the outcome. Luckily, it’s possible to show both the log as console output and in a file. We are just working inside the Bash shell, after all. So run this line:
az group create --name createsplitlog --location westeurope | tee splitlog.txt
And see that both output destinations are filled with the outcome:
Executing batches of commands
So we can execute a command from the prompt. That’s great. But can we take this a step further, can we execute multiple commands using a batch file?
Yes, we can!
Create a file like this with two commands in it:
Now upload it to the File Service and execute it using the ‘bash’ command:
We see the output of two commands right after each other. Commands are executed in a blocking way. Only after a complete execution of the first command, the next command is executed. This way we are confident that resources are always there when needed.
Note: The CLI has some tooling/parameters you can add for executing multiple commands at once if you know there are no dependencies (look for –no-wait).
Nano in Bash
Just to show we are really running bash, here is some proof on how to create a new file using the Nano tool:
Yes, if you are a heavy Bash user, you will have a lot of fun in the browser!
Deleted File Service
If you delete the File Service, or the containing Azure Storage, or the containing Resource group, and you run the Azure Cloud Shell, you will get this warning:
This is not so helpful. How can we undo the deletion?
You can fix this using the (not so helpful) information in this link. There are two possible ways to create a new cloud drive:
Create a resource group: az group create --name cloud-shell-storage-westeurope --location westeurope Create a storage account: az storage account create --name cs[guid] --location westeurope --resource-group cloud-shell-storage-westeurope --sku Standard_LRS Look up the connection string of your storage using this command: az storage account show-connection-string --name cs[guid] --resource-group cloud-shell-storage-westeurope Create a File Service in the storage account: az storage share create --name cs-[email][guid] --quota 6 --connection-string "[connection string]" Look up the subscription id of your subscription in the portal... Mount the cloud drive clouddrive mount -s [subscription] -g cloud-shell-storage-westeurope -n cs[guid] -f cs-[email][guid]
or you just unmount and restart the shell again:
I prefer this last one 🙂
And that’s all, you have your storage back:
What about that shell that made my life easy by providing hints?
Ok, until now you have experienced the regular CLI commands. But in the previous blog that marvelous help in a shell was shown. Where is this help now?
Well, it’s still there. Just execute:
And there it is:
Note: when I deleted some resources, the feedback was minimal. So be sure to test how to check success or fail.
Error: MissingSubscriptionRegistration 409
A colleague of mine got this 409 error:
The resource group was created, but the storage was not.
A resolution for that can be found here.
Just register the appropriate namespace and try again!
What we have seen here, is a very valuable piece of work. We can now create large amounts of our Azure resources, just by executing batch files on the command line. Of course, there will be some work when you need to extract input values from the JSON output (coming from previous commands). But when you are a Bash user already, this interface is stellar. And if you are not yet a Bash user, this is the moment to become one!
More documentation is available here