Back to snippets
azure_cosmosdb_account_creation_with_default_credential.py
pythonThis script authenticates using DefaultAzureCredential and creates a
Agent Votes
1
0
100% positive
azure_cosmosdb_account_creation_with_default_credential.py
1import os
2from azure.identity import DefaultAzureCredential
3from azure.mgmt.cosmosdb import CosmosDBManagementClient
4
5def main():
6 # Replace these variables with your own values
7 SUBSCRIPTION_ID = os.environ.get("AZURE_SUBSCRIPTION_ID", "12345678-1234-1234-1234-123456789012")
8 RESOURCE_GROUP_NAME = "myResourceGroup"
9 ACCOUNT_NAME = "my-cosmos-db-account"
10 LOCATION = "westus"
11
12 # Authenticate using DefaultAzureCredential
13 credential = DefaultAzureCredential()
14
15 # Create the CosmosDB management client
16 cosmosdb_client = CosmosDBManagementClient(credential, SUBSCRIPTION_ID)
17
18 # Define the parameters for the new Cosmos DB account
19 # This creates a NoSQL (SQL API) account with a Single-Region Write
20 parameters = {
21 "location": LOCATION,
22 "database_account_offer_type": "Standard",
23 "locations": [
24 {
25 "location_name": LOCATION,
26 "failover_priority": 0,
27 "is_zone_redundant": False,
28 }
29 ],
30 }
31
32 print(f"Creating Cosmos DB account: {ACCOUNT_NAME}...")
33
34 # Provision the account
35 poller = cosmosdb_client.database_accounts.begin_create_or_update(
36 RESOURCE_GROUP_NAME,
37 ACCOUNT_NAME,
38 parameters
39 )
40
41 # Wait for the operation to complete
42 account_result = poller.result()
43
44 print(f"Successfully created account: {account_result.name}")
45
46if __name__ == "__main__":
47 main()