In the previous post, I laid out the case for creating a hybrid cloud lab along with various use cases. I’ll use Python and Azure for this project, and I’m using MacOS, but all of these steps can easily be done on Windows or Linux as well.
The first practical step of setting up this lab is installing Python and a few Python libraries we’ll use to connect to Azure. (I should also point out that it’s a good idea to work on this in Git for some kind of version control.)
On a Mac, you’ll open the terminal and run:
brew install python
This should install Python as well as Pip, Python’s package manager. On Windows, you can either install by downloading an MSI from Python’s website or using an extension in VSCode. Python comes natively with almost every Linux distro.
Now that we have Python, we’ll start by creating a virtual environment for Python–this is a step it took me too long to learn, and it’s important to ensure no conflicts, dependency nightmares, etc. So open your terminal on Mac or Linux (if you’re using Windows, I strongly recommend following these directions in WSL) and type
# Create a project directory (if you haven't already)
mkdir hybrid-cloud-lab
cd hybrid-cloud-lab
# Create a virtual environment called 'venv'
python3 -m venv venv
# Activate the virtual environment
source venv/bin/activate
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.
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 "<h1>Howdy from localhost!</h1>" > 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 open your web browser and navigate to http://localhost:8000
. If you see “Howdy from localhost!,” you’re all set. You can stop the server by clicking into the terminal and pressing Ctrl+C
.
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. You can download the CLI from the Azure website for any OS, but I’m just going to use Homebrew for MacOS here because it’s easy:
brew install azure-cli
Now that we’ve installed the Azure CLI, we can login to our account with a quick command:
az login
This command opens a web browser and asks for your Azure credentials.
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.
Leave a Reply