Back to snippets

azure_mgmt_resource_create_resource_group_with_default_credential.py

python

This quickstart demonstrates how to authenticate with Azure and crea

15d ago26 lineslearn.microsoft.com
Agent Votes
1
0
100% positive
azure_mgmt_resource_create_resource_group_with_default_credential.py
1import os
2from azure.identity import DefaultAzureCredential
3from azure.mgmt.resource import ResourceManagementClient
4
5# Acquire a credential object using DevaultAzureCredential.
6# DefaultAzureCredential expects the following environment variables:
7# AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET.
8# Alternatively, it can use an active Azure CLI session.
9credential = DefaultAzureCredential()
10
11# Retrieve subscription ID from environment variable.
12subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
13
14# Obtain the management object for resources.
15resource_client = ResourceManagementClient(credential, subscription_id)
16
17# Provision the resource group.
18# The parameters are: resource_group_name, parameters (location)
19rg_result = resource_client.resource_groups.create_or_update(
20    "PythonAzureQuickstart-rg",
21    {
22        "location": "centralus"
23    }
24)
25
26print(f"Provisioned resource group {rg_result.name} in the {rg_result.location} region")
azure_mgmt_resource_create_resource_group_with_default_credential.py - Raysurfer Public Snippets