Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Quickstart: Create a Python app with Azure Managed Redis

  • Applies to: ✅ Azure Managed Redis
Feedback

In this article

In this article, you learn how to use an Azure Managed Redis cache with the Python language and connect using Microsoft Entra ID.

Prerequisites

  • Azure subscription -create one for free
  • InstallPython 3.7+ language environment
  • Add these imports from to your project and to your development environment
    • redis - The Redis Python client
    • redis-entraid - Redis Microsoft Entra ID authentication extension
    • azure-identity - Azure authentication library

Create an Azure Managed Redis instance

First, create a cache. You can create a cache using Azure Managed Redis or Azure Cache for Redis using the Azure portal. In this Quickstart, we use Azure Managed Redis.

When you create the cache, Microsoft Entra ID is enabled by default making it secure from the start. Your cache must also use a public endpoint for this QuickStart.

To create a cache with the portal, follow one of these procedures:

Optionally, you can create a cache using Azure CLI, PowerShell, whichever you prefer.

Code to connect to a Redis cache

In the first part of the code sample, set your connection to the cache.

  • Ports for Azure Managed Redis and Enterprise caches: 10000
  • Ports for Azure Cache for Redis instances: 6380
import redisfrom azure.identity import DefaultAzureCredentialfrom redis_entraid.cred_provider import create_from_default_azure_credential# Connection details for your cache# Get the connection details for the Redis instanceredis_host = "contosob116.westus3.redis.azure.net"redis_port = 10000  #For an Azure print("🚀 Starting Azure Redis Cache connection test...")print(f"📡 Connecting to: {redis_host}:{redis_port}")# Validate configurationif not redis_host or not redis_port:    print("❌ Error: Redis host and port must be configured")    exit(1)print()  # Add a new linetry:    # Create credential provider using DefaultAzureCredential for Azure Entra ID authentication    credential_provider = create_from_default_azure_credential(         ("https://redis.azure.com/.default",),)    # Create a Redis client with Azure Entra ID authentication    r = redis.Redis(host=redis_host,                     port=redis_port,                     ssl=True,                     decode_responses=True,                     credential_provider=credential_provider,                    socket_timeout=10,                    socket_connect_timeout=10                    )

Code to test a connection

In the next section, test the connection using the Redis commandping that returns theTrue value.

# Ping the Redis server to test the connectionresult = r.ping()if result:    print("Ping returned: ", result)

Code set a key, get a key

In this section, use a basicset andget sequence to start using the Redis cache in the simplest way to get started.

    # Create a simple set and get operation    result = r.set("Message", "Hello, The cache is working with Python!")    print("✅ SET Message succeeded: " + str(result))    print()  # Add a new line    value = r.get("Message")    if value is not None:        print("✅ GET Message returned : " + str(value))        print()  # Add a new line    else:        print("⚠️  GET Message returned None")        print()  # Add a new line        print("🎉 All Redis operations completed successfully!")    print()  # Add a new line

Before you can run this code, you must add yourself as a Redis user to the cache.

You must also authorize your connection to Azure from the command line using the Azure command line or Azure developer command line (azd).

You should alsoadd users or a System principal to your cache. Add anyone who might run the program as a user on the Redis cache.

The result looks like this:

C:\utils\python-quickstart>python quickstart-amr.py🚀 Starting Azure Redis Cache connection test...📡 Connecting to: contosob116.westus3.redis.azure.net:10000✅ Ping returned : True✅ SET Message succeeded: True✅ GET Message returned : Hello, The cache is working with Python!🎉 All Redis operations completed successfully!🔐 Redis connection closed

Here, you can see this code sample in its entirety. The code contains some error checking omitted from the earlier code explanations for simplicity. The final step is closing the connection to the cache.

# Python Quickstart using Azure Entra ID authentication# Azure Managed Redis cache that you created using the Azure portal, or CLI# This script demonstrates secure connection using Microsoft Entra ID authentication# This script demonstrates secure connection using the default Azure credential provider# You should be a user on the cache and logged in to Azure CLI with the same account using `az login`import redisfrom azure.identity import DefaultAzureCredentialfrom redis_entraid.cred_provider import create_from_default_azure_credential# Connection details for your cache# Get the connection details for the Redis instanceredis_host = "<host-url>"  # Replace with your cache inforedis_port = <port number>  # Replace with your cache info print("🚀 Starting Azure Redis Cache connection test...")print(f"📡 Connecting to: {redis_host}:{redis_port}")# Validate configurationif not redis_host or not redis_port:    print("❌ Error: Redis host and port must be configured")    exit(1)print()  # Add a new linetry:    # Create credential provider using DefaultAzureCredential for Azure Entra ID authentication    credential_provider = create_from_default_azure_credential(         ("https://redis.azure.com/.default",),)    # Create a Redis client with Azure Entra ID authentication    r = redis.Redis(host=redis_host,                     port=redis_port,                     ssl=True,                     decode_responses=True,                     credential_provider=credential_provider,                    socket_timeout=10,                    socket_connect_timeout=10                    )    # Test connection     result = r.ping()    print("✅ Ping returned : " + str(result))    print()  # Add a new line    # Create a simple set and get operation    result = r.set("Message", "Hello, The cache is working with Python!")    print("✅ SET Message succeeded: " + str(result))    print()  # Add a new line    value = r.get("Message")    if value is not None:        print("✅ GET Message returned : " + str(value))        print()  # Add a new line    else:        print("⚠️  GET Message returned None")        print()  # Add a new line        print("🎉 All Redis operations completed successfully!")    print()  # Add a new lineexcept redis.ConnectionError as e:    print(f"❌ Connection error: {e}")    print("💡 Check if Redis host and port are correct, and ensure network connectivity")    print()  # Add a new lineexcept redis.AuthenticationError as e:    print(f"❌ Authentication error: {e}")    print("💡 Check if Azure Entra ID authentication is properly configured")    print()  # Add a new lineexcept redis.TimeoutError as e:    print(f"❌ Timeout error: {e}")    print("💡 Check network latency and Redis server performance")    print()  # Add a new lineexcept Exception as e:    print(f"❌ Unexpected error: {e}")    if "999" in str(e):        print("💡 Error 999 typically indicates a network connectivity issue or firewall restriction")        print("   - Verify the Redis hostname is correct")        print("   - Verify that you have logged in with Az CLI")        print("   - Ensure the Redis cache is running and accessible")    print()  # Add a new linefinally:    # Clean up connection if it exists    if 'r' in locals():        try:            r.close()            print("🔐 Redis connection closed")        except Exception as e:            print(f"❌ Error closing connection: {e}")

Clean up resources

If you want to continue to use the resources you created in this article, keep the resource group.

Otherwise, if you're finished with the resources, you can delete the Azure resource group that you created to avoid charges.

Important

Deleting a resource group is irreversible. When you delete a resource group, all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources inside an existing resource group that contains resources you want to keep, you can delete each resource individually instead of deleting the resource group.

To delete a resource group

  1. Sign in to theAzure portal, and then selectResource groups.

  2. Select the resource group you want to delete.

    If there are many resource groups, use theFilter for any field... box, type the name of your resource group you created for this article. Select the resource group in the results list.

    Screenshot showing a list of resource groups to delete in the working pane.

  3. SelectDelete resource group.

  4. You're asked to confirm the deletion of the resource group. Type the name of your resource group to confirm, and then selectDelete.

    Screenshot showing a form that requires the resource name to confirm deletion.

After a few moments, the resource group and all of its resources are deleted.

Related content


Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?