Redis Entra ID

1.1.2 · active · verified Thu Apr 16

The `redis-entraid` Python package simplifies authentication with Azure Managed Redis and Azure Cache for Redis using Microsoft Entra ID (formerly Azure Active Directory). It handles fetching and renewing authentication tokens in the background, building on `redis-py`. The current version is 1.1.2, released on March 26, 2026. This library appears to have a regular release cadence with several updates in the past year.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to an Azure Managed Redis or Azure Cache for Redis instance using Microsoft Entra ID authentication with `redis-entraid` and `DefaultAzureCredential`. Ensure you have configured environment variables for `DefaultAzureCredential` (e.g., `AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`) or that your application runs with an assigned Managed Identity. Replace `your_redis_cache_name.redis.cache.windows.net` with your actual Redis endpoint and set the correct port (e.g., 10000 for Azure Managed Redis or 6380 for Azure Cache for Redis).

import os
from redis import Redis
from azure.identity import DefaultAzureCredential
from redis_entraid.cred_provider import create_from_default_azure_credential

# --- Environment Variables (for DefaultAzureCredential to pick up) ---
# Set these in your environment, e.g., in a .env file or directly:
# os.environ['AZURE_TENANT_ID'] = 'YOUR_TENANT_ID'
# os.environ['AZURE_CLIENT_ID'] = 'YOUR_CLIENT_ID'
# os.environ['AZURE_CLIENT_SECRET'] = 'YOUR_CLIENT_SECRET'
# OR ensure a Managed Identity is assigned to your application/VM.

# --- Redis Connection Details ---
# Your Azure Managed Redis or Azure Cache for Redis hostname and port
# Default port for Azure Managed Redis is 10000, for Azure Cache for Redis it's 6380.
REDIS_HOST = os.environ.get('REDIS_HOST', 'your_redis_cache_name.redis.cache.windows.net')
REDIS_PORT = int(os.environ.get('REDIS_PORT', 10000)) 

def connect_with_entra_id():
    try:
        # DefaultAzureCredential will attempt to authenticate via various methods
        # including environment variables, managed identity, etc.
        # Scopes are crucial for Entra ID authentication with Redis.
        credential_provider = create_from_default_azure_credential(
            ("https://redis.azure.com/.default",)
        )
        
        # Azure enforces TLS for Entra ID authentication.
        # decode_responses=True automatically decodes responses to Python strings.
        r = Redis(
            host=REDIS_HOST,
            port=REDIS_PORT,
            ssl=True,
            decode_responses=True,
            credential_provider=credential_provider
        )

        # Test the connection
        if r.ping():
            print(f"Successfully connected to Redis at {REDIS_HOST}:{REDIS_PORT} with Entra ID.")
            r.set("mykey", "Hello from redis-entraid!")
            value = r.get("mykey")
            print(f"Retrieved value: {value}")
        else:
            print("Redis ping failed.")

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    connect_with_entra_id()

view raw JSON →