Automating Azure VM Deployment with Python
- Jitendra Singh
- 1 day ago
- 2 min read
I recently completed a project automating Azure Infrastructure-as-Code (IaC) using Python's requests library and the Azure REST API. While tools like Terraform and Bicep are great, interacting directly with the API provides a deeper understanding of how Azure’s Resource Manager (ARM) actually works.
🛠️ The Tech Stack
Python: The engine for automation.
Azure Identity: Securely fetching OAuth 2.0 tokens using DefaultAzureCredential.
REST APIs: Performing GET and PUT operations to manage resources.
Jupyter Notebook: For iterative testing and debugging.
💡 Key Learning: The Dependency Chain
One of the biggest takeaways was managing the resource hierarchy. You can't just "request a VM." You have to build the foundation first:
Authentication: Exchange Service Principal credentials for a Bearer Token.
Discovery: Query the Subscription to find the right Resource Group and Region (southindia in this case).
Networking: Create a Network Interface (NIC) tied to a specific VNet and Subnet.
Compute: Trigger the VM creation only after the NIC ID is successfully returned.
📝 The "Gotchas" I Solved:
Case Sensitivity: Azure APIs expect storageProfile, not StorageProfile.
Authentication: Using .env files to keep my Service Principal secrets safe and out of the code.
Async Operations: Handling the transition from PUT request accepted to the resource actually being provisioned.
📋 Code Snippet Highlights
Using the azure-identity library made credential management a breeze:
By passing this token into the headers of my requests.put() call, I was able to deploy a Standard_D2ads_v5 instance programmatically.
Automation isn't just about saving time; it's about creating repeatable, error-free environments. Next step? Writing a polling loop to track deployment status in real-time!
here if Jupiter Notebook Python file for actual code and reference
hashtag#Azure hashtag#Python hashtag#CloudAutomation hashtag#RESTAPI hashtag#DevOps hashtag#CloudEngineering
Follow for more







Comments