If you’ve read the earlier post Getting Started with Google Colab, you already know how easy it is to write and run Python code in your browser without installing anything. In this post, let’s take it one step further and see how to connect Google Colab with Azure Cloud for real projects.
🔑 Why Use Colab for Azure?
When working with Azure, you often need to write scripts for automation, testing, or managing cloud resources. Google Colab is an excellent choice because:
- No setup hassle: Install Azure SDKs directly in your notebook.
- Free environment: Great for quick experiments and learning.
- Collaboration: Share cloud scripts like Google Docs.
- Portability: Run from anywhere, on any device.
🛠️ Prerequisite
👉 Make sure you’ve read our post Getting Started with Google Colab to learn the basics of creating and running a Colab notebook.
🚀 Setting Up Google Colab for Azure
Step 1: Install Azure SDK
Run this in a new Colab cell:
%pip install azure-identity azure-mgmt-resource
azure-identity: Handles authentication.azure-mgmt-resource: Manages Azure resources (like resource groups).
📝 Note: Jupyter/Colab notebooks run Python code by default. To run system commands such as
pip, you need to use either!or%.
!pip: runspip installas a shell command. This works, but sometimes packages may not be linked correctly to the notebook’s Python environment.%pip: is a magic command built into Jupyter/Colab. It installs packages directly into the notebook’s active Python environment, so they are immediately available.✅ Why
%pipis better:%pipensures reliable installation inside the notebook’s kernel, making it the recommended choice.
🔑Step 2: Authenticate with Azure
🔑 Step 2: Authenticate with Azure
Since Google Colab runs in the cloud without access to your local browser, the best way to log in is by using Device Code authentication. This method gives you a code and a URL. You open the URL in any browser, enter the code, and your Colab session is authenticated.
from azure.identity import DeviceCodeCredential
from azure.mgmt.resource import ResourceManagementClient
# Replace with your subscription ID and tenant ID
subscription_id = "YOUR_SUBSCRIPTION_ID"
tenant_id = "YOUR_TENANT_ID"
def show_message(verification_uri, user_code, expires_on):
print(f"To authenticate, visit: {verification_uri}")
print(f"Enter the code: {user_code}")
print(f"(Code expires on: {expires_on})")
# Authenticate with tenant-specific credentials
credential = DeviceCodeCredential(
tenant_id=tenant_id,
prompt_callback=show_message
)
# Example: create a client
client = ResourceManagementClient(credential, subscription_id)
🔎 How to find Subscription ID and Tenant ID in the Azure Portal
- Log in to the Azure Portal.
- In the left-hand menu, click Subscriptions.
- Select your subscription.
- In the Overview page, you’ll see:
- Subscription ID (a GUID like
1234abcd-...). - Tenant (click it to open and copy the GUID — this is your Tenant ID).
- Subscription ID (a GUID like
- Alternatively, go to Microsoft Entra ID (Azure Active Directory) → Overview.
- The Tenant ID is listed under “Basic Information.”
⚠️ Important: If you authenticate without specifying the tenant, you might see an error like
“access token is from the wrong issuer…” (InvalidAuthenticationTokenTenant).
To avoid this, always pass the correct tenant_id that matches your subscription.
Step 3: LIST Resource Groups
Here’s how you can create a new resource group from Colab:
# List all resource groups
for rg in client.resource_groups.list():
print(rg.name)
If you have reader permission to the subscription, you should be able to see all RGs we have. Else you will just see the resource groups you have access to. You can consider this lab complete in either of those two scenarios.
💡 Best Practices for Using Azure with Google Colab
When working with Azure inside Colab, keep these best practices in mind:
- Secure your credentials – Never hardcode secrets directly into a notebook. Use environment variables or securely mounted files instead, and rotate keys regularly if you are using Service Principals.
- Restart the runtime after installing packages – Colab may prompt you to restart after
%pip install. Always restart to ensure the packages are properly available in your session. - Use version control for notebooks – Save notebooks in GitHub or export them regularly. This makes collaboration easier and prevents accidental loss of work, but be careful not to push sensitive information to public repositories.
- Keep Azure SDK versions consistent – Pin specific versions when installing, for example:
%pip install azure-identity==1.15.0 azure-mgmt-resource==23.0.0This avoids unexpected issues when SDKs are updated. - Organize your resource groups – Use a dedicated group such as
colab-experimentsfor testing. This keeps resources isolated and makes cleanup easier, helping you avoid unnecessary costs.
By following these practices, your Colab–Azure workflow will remain smooth, secure, and reproducible.
✅ Conclusion
Google Colab makes it incredibly simple to use Python with Azure—no local setup, instant collaboration, and quick experiments. Combine the power of Colab notebooks with the Azure SDK to build, test, and share your cloud automation projects with ease.
👉 Already comfortable with the basics? Try creating Azure resources directly from Colab today and see the magic happen!
🔑 Bonus: Alternative Authentication Options: Service Principal (Non-Interactive / Automation)
While DeviceCodeCredential is the easiest way to authenticate in Colab, there are situations where you might prefer or need another method.
If you want your scripts to run without manual sign-in (for example in CI/CD pipelines), you can use a Service Principal with ClientSecretCredential. This approach uses an App Registration in Microsoft Entra ID (formerly Azure AD) with a client ID, secret, and tenant ID.
from azure.identity import ClientSecretCredential
from azure.mgmt.resource import ResourceManagementClient
tenant_id = "YOUR_TENANT_ID"
client_id = "YOUR_APP_ID"
client_secret = "YOUR_CLIENT_SECRET"
subscription_id = "YOUR_SUBSCRIPTION_ID"
credential = ClientSecretCredential(
tenant_id=tenant_id,
client_id=client_id,
client_secret=client_secret
)
client = ResourceManagementClient(credential, subscription_id)
This method avoids interactive prompts and is best for automated workflows, though it requires securely managing the secret.
⚠️ Bonus: Why Not InteractiveBrowserCredential in Colab?
InteractiveBrowserCredential is an Azure authentication method that opens a browser window on your machine, letting you sign in with your Microsoft account. It works well on local desktops because the browser can launch directly and capture the login session.
from azure.identity import InteractiveBrowserCredential
credential = InteractiveBrowserCredential()
However, in Google Colab this fails with:
CredentialUnavailableError: Failed to open a browser
That’s because Colab runs on a remote VM and cannot launch a local browser for sign-in.
👉 Lesson learned: while InteractiveBrowserCredential is convenient locally, you should use DeviceCodeCredential in headless environments like Colab.