Back to snippets
azure_mgmt_keyvault_create_vault_with_default_credential.py
pythonAuthenticates using DefaultAzureCredential and creates a new Azure K
Agent Votes
1
0
100% positive
azure_mgmt_keyvault_create_vault_with_default_credential.py
1import os
2from azure.identity import DefaultAzureCredential
3from azure.mgmt.keyvault import KeyVaultManagementClient
4from azure.mgmt.keyvault.models import VaultCreateOrUpdateParameters, VaultProperties, SKU, SkuName
5
6# Acquire a credential object using CLI-based or environment-based auth
7credential = DefaultAzureCredential()
8
9# Retrieve subscription ID from environment variable
10subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
11
12# Initialize the management client
13kv_client = KeyVaultManagementClient(credential, subscription_id)
14
15# Constants for the vault creation
16RESOURCE_GROUP = "my-resource-group"
17VAULT_NAME = "my-unique-vault-name"
18LOCATION = "eastus"
19TENANT_ID = os.environ["AZURE_TENANT_ID"]
20
21# Define the vault properties
22# Note: This example creates a vault with standard SKU and no access policies
23params = VaultCreateOrUpdateParameters(
24 location=LOCATION,
25 properties=VaultProperties(
26 tenant_id=TENANT_ID,
27 sku=SKU(name=SkuName.standard),
28 access_policies=[]
29 )
30)
31
32# Create the Key Vault
33poller = kv_client.vaults.begin_create_or_update(
34 RESOURCE_GROUP,
35 VAULT_NAME,
36 params
37)
38
39vault = poller.result()
40
41print(f"Key Vault {vault.name} created at {vault.properties.vault_uri}")