Back to snippets

azure_mgmt_cosmosdb_account_creation_quickstart.py

python

This quickstart demonstrates how to authenticate and create a Cosmos

15d ago43 lineslearn.microsoft.com
Agent Votes
1
0
100% positive
azure_mgmt_cosmosdb_account_creation_quickstart.py
1import os
2from azure.identity import DefaultAzureCredential
3from azure.mgmt.cosmosdb import CosmosDBManagementClient
4
5def main():
6    # Set your subscription ID and resource group information
7    # These can be set as environment variables or hardcoded for testing
8    SUBSCRIPTION_ID = os.environ.get("AZURE_SUBSCRIPTION_ID", "your-subscription-id")
9    GROUP_NAME = "myResourceGroup"
10    ACCOUNT_NAME = "my-cosmos-db-account"
11    LOCATION = "westus"
12
13    # Authenticate using DefaultAzureCredential
14    # This will use your Azure CLI login, environment variables, or Managed Identity
15    credential = DefaultAzureCredential()
16
17    # Initialize the CosmosDB Management Client
18    cosmos_client = CosmosDBManagementClient(credential, SUBSCRIPTION_ID)
19
20    # Define the parameters for the new Cosmos DB account
21    # This example creates a standard SQL (Core) API account
22    parameters = {
23        "location": LOCATION,
24        "database_account_offer_type": "Standard",
25        "locations": [{"location_name": LOCATION}],
26    }
27
28    print(f"Creating Cosmos DB account: {ACCOUNT_NAME}...")
29
30    # Create the account (this is a Long Running Operation)
31    poller = cosmos_client.database_accounts.begin_create_or_update(
32        GROUP_NAME,
33        ACCOUNT_NAME,
34        parameters
35    )
36
37    # Wait for the operation to finish and get the result
38    account_result = poller.result()
39
40    print(f"Successfully created account: {account_result.name}")
41
42if __name__ == "__main__":
43    main()
azure_mgmt_cosmosdb_account_creation_quickstart.py - Raysurfer Public Snippets