title | description | ms.date | ms.topic | ms.custom |
---|---|---|---|---|
Create a virtual machine using the Azure SDK libraries for Python |
How to create an Azure virtual machine using Python and the Azure SDK management libraries. |
03/14/2024 |
conceptual |
devx-track-python, py-fresh-zinc |
In this article, you learn how to use the Azure SDK management libraries in a Python script to create a resource group that contains a Linux virtual machine.
All the commands in this article work the same in Linux/macOS bash and Windows command shells unless noted.
The Equivalent Azure CLI commands are listed later in this article. If you prefer to use the Azure portal, see Create a Linux VM and Create a Windows VM.
Note
Creating a virtual machine through code is a multi-step process that involves provisioning a number of other resources that the virtual machine requires. If you're simply running such code from the command line, it's much easier to use the az vm create
command, which automatically provisions these secondary resources with defaults for any setting you choose to omit. The only required arguments are a resource group, VM name, image name, and login credentials. For more information, see Quick Create a virtual machine with the Azure CLI.
If you haven't already, set up an environment where you can run this code. Here are some options:
[!INCLUDE create_environment_options]
Create a requirements.txt file that lists the management libraries used in this example:
:::code language="txt" source="~/../python-sdk-docs-examples/vm/requirements.txt":::
Then, in your terminal or command prompt with the virtual environment activated, install the management libraries listed in requirements.txt:
pip install -r requirements.txt
Create a Python file named provision_vm.py with the following code. The comments explain the details:
:::code language="python" source="~/../python-sdk-docs-examples/vm/provision_vm.py":::
Later in this article, you sign in to Azure with the Azure CLI to run the sample code. If your account has permissions to create resource groups and network and compute resources in your Azure subscription, the code will run successfully.
To use such code in a production script, you can set environment variables to use a service principal-based method for authentication. To learn more, see How to authenticate Python apps with Azure services. You need to ensure that the service principal has sufficient permissions to create resource groups and network and compute resources in your subscription by assigning it an appropriate role in Azure; for example, the Contributor role on your subscription.
- DefaultAzureCredential (azure.identity)
- ResourceManagementClient (azure.mgmt.resource)
- NetworkManagementClient (azure.mgmt.network)
- ComputeManagementClient (azure.mgmt.compute)
-
If you haven't already, sign in to Azure using the Azure CLI:
az login
-
Set the
AZURE_SUBSCRIPTION_ID
environment variable to your subscription ID. (You can run the az account show command and get your subscription ID from theid
property in the output):set AZURE_SUBSCRIPTION_ID=00000000-0000-0000-0000-000000000000
AZURE_SUBSCRIPTION_ID=00000000-0000-0000-0000-000000000000
-
Run the script:
python provision_vm.py
The provisioning process takes a few minutes to complete.
Open the Azure portal, navigate to the "PythonAzureExample-VM-rg" resource group, and note the virtual machine, virtual disk, network security group, public IP address, network interface, and virtual network.
You can also use the Azure CLI to verify that the VM exists with the az vm list command:
az vm list --resource-group PythonAzureExample-VM-rg
:::code language="azurecli" source="~/../python-sdk-docs-examples/vm/provision.cmd":::
:::code language="azurecli" source="~/../python-sdk-docs-examples/vm/provision.sh":::
If you get an error about capacity restrictions, you can try a different size or region. For more information, see Resolve errors for SKU not available.
Leave the resources in place if you want to continue to use the virtual machine and network you created in this article. Otherwise, run the az group delete command to delete the resource group.
Resource groups don't incur any ongoing charges in your subscription, but resources contained in the group, like virtual machines, might continue to incur charges. It's a good practice to clean up any group that you aren't actively using. The --no-wait
argument allows the command to return immediately instead of waiting for the operation to finish.
az group delete -n PythonAzureExample-VM-rg --no-wait
[!INCLUDE resource_group_begin_delete]
- Example: Create a resource group
- Example: List resource groups in a subscription
- Example: Create Azure Storage
- Example: Use Azure Storage
- Example: Create a web app and deploy code
- Example: Create and query a database
- Use Azure Managed Disks with virtual machines
- Complete a short survey about the Azure SDK for Python
The following resources contain more comprehensive examples using Python to create a virtual machine:
- Azure Virtual Machines Management Samples - Python (GitHub). The sample demonstrates more management operations like starting and restarting a VM, stopping and deleting a VM, increasing the disk size, and managing data disks.