Azure Redis Cache Management
The azure-mgmt-redis client library provides functionality to manage Azure Redis Cache instances, including creation, update, deletion, and scaling. It is part of the Azure SDK for Python, currently at version 14.5.0, and follows a continuous release cadence for bug fixes and new features, with major versions introducing breaking changes.
Warnings
- breaking Breaking changes were introduced in `azure-mgmt-redis` version 8.0.0 due to alignment with the new Azure SDK guidelines. This involved significant changes to client constructor signatures and model names.
- gotcha List operations (e.g., `list_by_subscription`, `list_by_resource_group`) return iterable objects (an Azure Pager) for paginated results, not standard Python lists.
- breaking Exception handling for Azure SDK clients changed from `msrestazure.azure_exceptions.CloudError` to `azure.core.exceptions.HttpResponseError` when the SDK moved to `azure-core`.
- gotcha Many Azure management operations, especially for existing resources, require a specific resource group name. Failing to provide it will result in an error or incorrect operation.
Install
-
pip install azure-mgmt-redis azure-identity
Imports
- RedisManagementClient
from azure.mgmt.redis import RedisManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.redis import RedisManagementClient
# Set your Azure Subscription ID as an environment variable (e.g., AZURE_SUBSCRIPTION_ID)
# Or replace os.environ.get with your actual subscription ID.
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_SUBSCRIPTION_ID')
# Authenticate using DefaultAzureCredential
# This will try to authenticate using various methods, e.g., environment variables, managed identity, Azure CLI, Visual Studio Code.
credential = DefaultAzureCredential()
# Create a RedisManagementClient
redis_client = RedisManagementClient(credential, subscription_id)
print(f"Listing Redis Caches in subscription {subscription_id}:")
# List all Redis Caches in the subscription
# Operations like .list_by_subscription() return an iterator for paginated results.
try:
for cache in redis_client.redis.list_by_subscription():
print(f" - Name: {cache.name}, Location: {cache.location}, SKU: {cache.sku.name}")
except Exception as e:
print(f"An error occurred: {e}")