Okay, looking at back at the last post, I realize how Mac-centric some of the instructions are, so I’ve decided to include another set of instructions on accomplishing the same thing. I’m going to be using Windows Subsystem for Linux to accomplish this. If you’re on a Windows machine, I highly suggest doing the same. Though there is a good argument for using PowerShell in the Azure environment as a whole, I find using PowerShell to do basic scripting to be a bit convoluted for those of us with a Bash background. And since Azure supports both PowerShell and Bash, I don’t necessarily think using a lot of PowerShell is going to be worthwhile unless you’re heavily invested in Microsoft administration.
So first things first, make sure you download WSL2 on your Windows machine. Windows has some good documentation on doing so. One of the cool things about WSL2 is that you’re able to pick your Linux distro of choice–I don’t know if every Linux distro is packaged for WSL, but quite a few of them are.
Full disclosure: normally I use Alma Linux 10 for my work. That’s because it’s a clone of Red Hat Linux, which is widely used in enterprise environments. Most people will probably prefer to use Ubuntu, though, so for this exercise I’m going to use Ubuntu 24.04.1 LTS. If you’re in a command line environment, you can always check to see what OS you’re running by typing cat /etc/os-release
.
We always want to begin by updating our package manager, so in the Linux terminal type:
sudo apt update && sudo apt upgrade
Type y
to accept the upgrade.
Python comes natively on most Linux distros, so once the update is complete, type python3 -V
to confirm Python is installed. As of this writing, the latest update is Python 3.12.3. You can also type pip -v
to see if pip, Python’s package manager, is installed. It’s not on my machine, so I’m going to type sudo apt install python3-pip
.
Next, we’ll create a project directory:
# Create a project directory (if you haven't already)
mkdir hybrid-cloud-lab
cd hybrid-cloud-lab
On Debian-based systems (like Ubuntu), you have to install a separate package to create a Python virtual environment. To do that, type:
sudo apt install python3.12-venv
After that, you’ll create and start your virtual environment with the following commands:
python3 -m venv venv # Activate the virtual environment
source venv/bin/activate
Now you’re in Python’s virtual environment.
Once activated, your terminal prompt will likely change to indicate you’re inside venv
, the virtual environment. Now any Python package you install now will only apply to this specific project, which can prevent conflicts and dependency conflicts that result from multiple Python projects and libraries installed across your system.
To confirm we’ve done this correctly, we’ll start a simple web server with a single web page. The point here is to make sure we can host services on localhost. In the terminal write
echo "Howdy from localhost" > index.html
# You can check the server by running it on port 8000
python -m http.server 8000
If everything is working correctly, you should be able to visit localhost:8000
in a browser and see the “Howdy from localhost” message displayed. Back in the terminal, press Ctrl C
to kill the local server.
Connecting to Azure
Now that the local environment is ready, we can install Python’s Azure libraries. The Azure SDK for Python provides a comprehensive set of libraries that allow us to interact virtually with any Azure service.
Make sure you’re still in the virtual environment, install the Azure packages:
pip install azure-identity azure-mgmt-resource azure-mgmt-storage azure-storage-blob
These packages are enabling us to complete certain functions in Azure, Specifically:
azure-identity
: handles authentication to Azureazure-mgmt-resource
: manages Azure resource groups (which is a kind of container for Azure resources)azure-mgmt-storage
: creates and manages Azure Storage Accountsazure-storage-blob
: interacts with blob storage (like uploading files
Now that we have these packages, we need the Azure Command Line Interface (Azure CLI). This is a little confusing because we’re using Python for the scripting, so why do we need the Azure CLI? Basically, it makes it easier to authenticate our Python scripts and perform quick admin tasks.
To install the Azure CLI on Linux, follow these steps in the Microsoft documentation. (Fair warning, this last step took about a minute with no feedback from the terminal, so don’t despair if it takes a bit.)
Now that we’ve installed the Azure CLI, we can login to our account with a quick command:
az login
In MacOS, this command opens a web browser and asks for your Azure credentials. However, in WSL, your browser isn’t going to open because you’re working in a virtual Linux machine inside Windows. So you what you’ll see if you ran the command above is the option to use
az login --use-device-code
.
So if your terminal is hung on the last command, press Ctrl C
, then type az login --use-device-code
. Follow the prompts from there, and you should be authenticated to Azure.
The last part of the setup process is to actually register an Azure resource provider to our subscription. Basically the resource provider is a service that enables Azure to offer specific resources (like a storage account, for example). Most are registered by default, but best practice is to explicitly register a resource provider for a project. In our case, we’ll register Microsoft.Storage
.
az provider register --namespace Microsoft.Storage
This step can take several minutes, and you won’t get a notification in the terminal when it’s complete. Give it a few minutes and check the registration state with the following command:
az provider show --namespace Microsoft.Storage --query "r"registrationState"
Once that command returns “Registered,” you’re all set.
So that’s the setup of the local environment and the connection of your local machine to Azure via Python and the Azure Cloud.
In my next post, I’ll walk through setting up a storage account (really, this time).
Leave a Reply