Skip to content

Latest commit

 

History

History
140 lines (86 loc) · 7.32 KB

azure-sdk-example-virtual-machines.md

File metadata and controls

140 lines (86 loc) · 7.32 KB
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

Example: Use the Azure libraries to create a virtual machine

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.

1: Set up your local development environment

If you haven't already, set up an environment where you can run this code. Here are some options:

[!INCLUDE create_environment_options]

2: Install the needed Azure library packages

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

3: Write code to create a virtual machine

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":::

Authentication in the code

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.

Reference links for classes used in the code

4. Run the script

  1. If you haven't already, sign in to Azure using the Azure CLI:

    az login
    
  2. 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 the id property in the output):

    set AZURE_SUBSCRIPTION_ID=00000000-0000-0000-0000-000000000000
    AZURE_SUBSCRIPTION_ID=00000000-0000-0000-0000-000000000000

  3. Run the script:

    python provision_vm.py

The provisioning process takes a few minutes to complete.

5. Verify the resources

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.

Azure portal page for the new resource group showing the virtual machine and related resources

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

Equivalent Azure CLI commands

:::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.

6: Clean up resources

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]

See also

The following resources contain more comprehensive examples using Python to create a virtual machine: