Azure Python库对应AWS Boto3:Python创建Azure VM替代EC2相关问询
Azure VM 创建的 Python 等价实现(对应 AWS EC2 + boto3)
核心结论
仅靠 azure-mgmt-resource 搞不定带完整网络配置的VM创建。这个库只管资源组和通用资源的基础操作,要创建VM得用 azure-mgmt-compute 库,而虚拟网络、子网、公共IP这些网络配置,还得靠 azure-mgmt-network 来实现。
要装的依赖包
得装这几个核心库:
pip install azure-mgmt-resource azure-mgmt-compute azure-mgmt-network azure-identity
其中 azure-identity 是用来做身份验证的,比如用Azure CLI的凭据或者服务主体都行。
完整示例代码(带网络配置)
下面是对应EC2创建流程的Azure VM代码,从资源组、网络资源到VM实例一步到位:
from azure.identity import DefaultAzureCredential from azure.mgmt.resource import ResourceManagementClient from azure.mgmt.network import NetworkManagementClient from azure.mgmt.compute import ComputeManagementClient # 替换成你的配置参数 SUBSCRIPTION_ID = "*你的订阅ID*" RESOURCE_GROUP_NAME = "test-rg" LOCATION = "eastus" VM_NAME = "test-vm" VM_SIZE = "Standard_D2s_v3" ADMIN_USERNAME = "adminuser" ADMIN_PASSWORD = "*你的强密码(要符合Azure密码规则)*" # 1. 初始化各个客户端 credential = DefaultAzureCredential() resource_client = ResourceManagementClient(credential, SUBSCRIPTION_ID) network_client = NetworkManagementClient(credential, SUBSCRIPTION_ID) compute_client = ComputeManagementClient(credential, SUBSCRIPTION_ID) # 2. 创建资源组 resource_client.resource_groups.create_or_update(RESOURCE_GROUP_NAME, {"location": LOCATION}) # 3. 先搞定网络资源(虚拟网络、子网、公共IP、网络接口) # 创建虚拟网络 vnet_params = { "location": LOCATION, "address_space": {"address_prefixes": ["10.0.0.0/16"]} } vnet = network_client.virtual_networks.begin_create_or_update( RESOURCE_GROUP_NAME, "test-vnet", vnet_params ).result() # 创建子网 subnet_params = {"address_prefix": "10.0.0.0/24"} subnet = network_client.subnets.begin_create_or_update( RESOURCE_GROUP_NAME, "test-vnet", "test-subnet", subnet_params ).result() # 创建公共IP public_ip_params = { "location": LOCATION, "public_ip_allocation_method": "Dynamic" } public_ip = network_client.public_ip_addresses.begin_create_or_update( RESOURCE_GROUP_NAME, "test-pip", public_ip_params ).result() # 创建网络接口 nic_params = { "location": LOCATION, "ip_configurations": [{ "name": "ipconfig1", "subnet": {"id": subnet.id}, "public_ip_address": {"id": public_ip.id} }] } nic = network_client.network_interfaces.begin_create_or_update( RESOURCE_GROUP_NAME, "test-nic", nic_params ).result() # 4. 最后创建VM vm_params = { "location": LOCATION, "hardware_profile": {"vm_size": VM_SIZE}, "storage_profile": { "image_reference": { "publisher": "Canonical", "offer": "0001-com-ubuntu-server-jammy", "sku": "22_04-lts", "version": "latest" } }, "os_profile": { "computer_name": VM_NAME, "admin_username": ADMIN_USERNAME, "admin_password": ADMIN_PASSWORD }, "network_profile": { "network_interfaces": [{"id": nic.id}] } } vm = compute_client.virtual_machines.begin_create_or_update( RESOURCE_GROUP_NAME, VM_NAME, vm_params ).result() print(f"VM {vm.name} 创建成功,ID: {vm.id}")
几点关键说明
- 身份验证:示例用的
DefaultAzureCredential会自动从Azure CLI、环境变量、Visual Studio等地方拿凭据,开发环境用着方便;生产环境建议用服务主体来做身份验证。 - 网络配置:Azure VM必须绑定网络接口(NIC),而NIC得关联子网,还可以加公共IP,这些步骤都得靠
azure-mgmt-network来完成。 - 镜像选择:示例用的是Ubuntu 22.04 LTS,要是想装Windows或者其他Linux发行版,改一下
image_reference里的参数就行。
内容的提问来源于stack exchange,提问作者anjanesh
相关产品推荐
相关产品推荐

