Skip to content

Latest commit

 

History

History
117 lines (73 loc) · 5.67 KB

azure-sdk-example-resource-group.md

File metadata and controls

117 lines (73 loc) · 5.67 KB
title description ms.date ms.topic ms.custom
Create a resource group using the Azure libraries for Python
Use the resource management library in the Azure SDK for Python to create a resource group from Python code.
03/04/2024
conceptual
devx-track-python, py-fresh-zinc

Example: Use the Azure libraries to create a resource group

This example demonstrates how to use the Azure SDK management libraries in a Python script to create a resource group. (The Equivalent Azure CLI command is given later in this article. If you prefer to use the Azure portal, see Create resource groups.)

All the commands in this article work the same in Linux/macOS bash and Windows command shells unless noted.

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 Azure library packages

Create a file named requirements.txt with the following contents:

:::code language="txt" source="~/../python-sdk-docs-examples/resource_group/requirements.txt":::

In a terminal or command prompt with the virtual environment activated, install the requirements:

pip install -r requirements.txt

3: Write code to create a resource group

Create a Python file named provision_rg.py with the following code. The comments explain the details:

:::code language="python" source="~/../python-sdk-docs-examples/resource_group/provision_rg.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 and list resource groups 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 and list resource groups 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_rg.py

5: Verify the resource group

You can verify that the group exists through the Azure portal or the Azure CLI.

  • Azure portal: open the Azure portal, select Resource groups, and check that the group is listed. If you've already had the portal open, use the Refresh command to update the list.

  • Azure CLI: use the az group show command:

    az group show -n PythonAzureExample-rg
    

6: Clean up resources

Run the az group delete command if you don't need to keep the resource group created in this example. Resource groups don't incur any ongoing charges in your subscription, but resources in the resource group 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-rg  --no-wait

You can also use the ResourceManagementClient.resource_groups.begin_delete method to delete a resource group from code. The commented code at the bottom of the script in this article demonstrates the usage.

For reference: equivalent Azure CLI command

The following Azure CLI az group create command creates a resource group with tags just like the Python script:

:::code language="azurecli" source="~/../python-sdk-docs-examples/resource_group/provision.cmd":::

See also