The Azure SDK for Python provides a comprehensive solution for managing Azure resources programmatically. This guide will demonstrate how to leverage the Azure SDK in Python for various cloud management tasks, including working with compute resources, storage services, and more.
Setting Up the Azure SDK for Python
To use the Azure SDK for Python, you need to install the necessary packages. The Azure SDK is modular, so you can install only the packages you need for your application.
Installation
Install the Azure identity and management packages using pip:
pip install azure-identity azure-mgmt-compute azure-storage-blob
- azure-identity: Provides Azure Active Directory (AAD) token authentication support.
- azure-mgmt-compute: Contains the management operations for Azure compute resources.
- azure-storage-blob: For working with Azure Blob Storage.
Authentication
To interact with Azure services, you’ll need to authenticate. The DefaultAzureCredential class from azure-identity simplifies this process by supporting multiple authentication methods.
Example:
from azure.identity import DefaultAzureCredential
# Authenticate using DefaultAzureCredential
credential = DefaultAzureCredential()
Note: The DefaultAzureCredential attempts to authenticate via the following methods, in order:
- Environment variables
- Managed identity (if running in an Azure environment)
- Azure CLI (if you have logged in locally)
Ensure that the appropriate authentication method is set up for your environment.
Working with Azure Compute Resources
You can manage Azure Virtual Machines (VMs) using the azure-mgmt-compute package. This includes creating, starting, stopping, and deleting VMs.
Example: Managing Azure VMs
from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient
# Set up credentials and client
credential = DefaultAzureCredential()
subscription_id = ‘your-subscription-id’
compute_client = ComputeManagementClient(credential, subscription_id)
# List all VMs in a subscription
for vm in compute_client.virtual_machines.list_all():
print(f”VM Name: {vm.name}”)
# Start a VM
resource_group_name = ‘your-resource-group’
vm_name = ‘your-vm-name’
compute_client.virtual_machines.begin_start(resource_group_name, vm_name)
print(f”Started VM: {vm_name}”)
# Stop a VM
compute_client.virtual_machines.begin_power_off(resource_group_name, vm_name)
print(f”Stopped VM: {vm_name}”)
Note: Replace ‘your-subscription-id’, ‘your-resource-group’, and ‘your-vm-name’ with your actual Azure subscription ID, resource group name, and VM name.
Managing Azure Storage Services
The azure-storage-blob package allows you to interact with Azure Blob Storage, which is optimized for storing massive amounts of unstructured data.
Example: Working with Azure Blob Storage
from azure.storage.blob import BlobServiceClient
# Initialize the BlobServiceClient
connection_string = “your-connection-string”
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
# Create a container
container_name = “my-container”
container_client = blob_service_client.create_container(container_name)
print(f”Created container: {container_name}”)
# Upload a blob
blob_name = “sample.txt”
blob_content = “Hello, Azure Blob Storage!”
blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
blob_client.upload_blob(blob_content)
print(f”Uploaded blob: {blob_name}”)
# Download a blob
downloaded_blob = blob_client.download_blob().readall()
print(f”Downloaded blob content: {downloaded_blob.decode(‘utf-8’)}”)
# List blobs in a container
print(“Listing blobs…”)
container_client = blob_service_client.get_container_client(container_name)
for blob in container_client.list_blobs():
print(f”- {blob.name}”)
Note: Replace “your-connection-string” with your actual Azure Storage account connection string. You can find it in the Azure portal under your storage account’s access keys.
Other Azure Services
The Azure SDK for Python extends beyond compute and storage, offering tools for working with various Azure services, such as:
- Azure Key Vault: Securely store and access secrets, keys, and certificates.
pip install azure-keyvault-secrets
- Azure Cosmos DB: Interact with Azure’s globally distributed, multi-model database service.
pip install azure-cosmos
- Azure Functions: Develop and deploy serverless functions.
pip install azure-functions
- Azure AI Services: Integrate AI capabilities like Computer Vision, Text Analytics, and more.
pip install azure-ai-textanalytics
Example: Accessing Secrets from Azure Key Vault
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
# Set up credentials and client
credential = DefaultAzureCredential()
vault_url = “https://your-key-vault-name.vault.azure.net”
secret_client = SecretClient(vault_url=vault_url, credential=credential)
# Retrieve a secret
secret_name = “my-secret”
retrieved_secret = secret_client.get_secret(secret_name)
print(f”Secret Value: {retrieved_secret.value}”)
Note: Replace “your-key-vault-name” and “my-secret” with your actual Key Vault name and secret name.