Edit

Share via


Manage an Azure Redis cache using the Azure CLI

This article describes how to create and delete an Azure Redis cache instance by using the Azure CLI. The article also shows how to use the Azure CLI to get cache details including provisioning status, hostname, ports, and keys.

Prerequisites

  • Make sure you're signed in to Azure with the subscription you want to create your cache under. To use a different subscription than the one you're signed in with, run az account set -s <subscriptionId>, replacing <subscriptionId> with the subscription ID you want to use.

Note

Azure Managed Redis uses the Azure CLI az redisenterprise commands. The redisenterprise extension for Azure CLI version 2.61.0 or higher prompts you for installation the first time you run an az redisenterprise command.

Azure Cache for Redis uses the az redisenterprise commands for Enterprise tiers and the az redis commands for Basic, Standard, and Premium tiers. You can use the following scripts to create and manage Azure Managed Redis or Azure Cache for Redis Enterprise. For Azure Cache for Redis Basic, Standard, and Premium, use the Azure Cache for Redis scripts.

Create an Azure Managed Redis cache

To create an Azure Managed Redis cache by using Azure CLI, the name, location, resourceGroup, and sku parameters are required. Other parameters are optional and have defaults.

You can use the Azure CLI script in this section to create an Azure Managed Redis cache with default settings. You can also use the following other methods to create a cache:

The cache name must be a string of 1-63 characters that's unique in the Azure region. The name can contain only numbers, letters, and hyphens, must start and end with a number or letter, and can't contain consecutive hyphens.

The location should be an Azure region near other services that use your cache.

Choose a sku that has the appropriate features and performance for your cache.

Microsoft Entra authentication is enabled by default for all new caches and is recommended for security.

Important

Use Microsoft Entra ID with managed identities to authorize requests against your cache if possible. Authorization using Microsoft Entra ID and managed identity provides better security and is easier to use than shared access key authorization. For more information about using managed identities with your cache, see Use Microsoft Entra for cache authentication with Azure Managed Redis.

Transport Layer Security (TLS) 1.2-1.3 encryption is enabled by default for all new caches. You can enable the non-TLS port and connections during or after cache creation, but for security reasons, disabling TLS isn't recommended.

The following script sets variables, and then uses the az group create and az redisenterprise create commands to create a resource group with an Azure Managed Redis Balanced B1 SKU cache in it.


# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="redis-cache-rg-$randomIdentifier"
tag="create-manage-cache"
cache="redis-cache-$randomIdentifier"
sku="Balanced_B1"

# Create a resource group
echo "Creating $resourceGroup in "$location"..."
az group create --resource-group $resourceGroup --location "$location" --tags $tag

# Create a Balanced B1 Azure Managed Redis cache
echo "Creating $cache"
az redisenterprise create --name $cache --resource-group $resourceGroup --location "$location" --sku $sku

Get details for an Azure Managed Redis cache

The following script uses the az redisenterprise show and az redisenterprise database list-keys commands to get and display the name, hostname, ports, and keys details for the preceding cache.

Important

The list-keys operation works only when access keys are enabled for the cache. The output of this command might compromise security by showing secrets, and may trigger a sensitive information warning. For more information, see Use Azure CLI to manage sensitive information.

# Get details of an Azure Managed Redis cache
echo "Showing details of $cache"
az redisenterprise show --name "$cache" --resource-group $resourceGroup 

# Retrieve the hostname and ports for an Azure Redis Cache instance
redis=($(az redisenterprise show --name "$cache" --resource-group $resourceGroup --query [hostName,enableNonSslPort,port,sslPort] --output tsv))

# Retrieve the keys for an Azure Redis Cache instance
keys=($(az redisenterprise database list-keys --cluster-name "$cache" --resource-group $resourceGroup --query [primaryKey,secondaryKey] --output tsv))

# Display the retrieved hostname, keys, and ports
echo "Hostname:" ${redis[0]}
echo "Non SSL Port:" ${redis[2]}
echo "Non SSL Port Enabled:" ${redis[1]}
echo "SSL Port:" ${redis[3]}
echo "Primary Key:" ${keys[0]}
echo "Secondary Key:" ${keys[1]}

Clean up resources

The following script uses the az group delete and az redisenterprise delete commands to delete the preceding cache, and then delete its resource group.

# Delete a redis cache
echo "Deleting $cache"
az redisenterprise delete --name "$cache" --resource-group $resourceGroup -y

# echo "Deleting all resources"
az group delete --resource-group $resourceGroup -y

Important

Use these scripts to create and manage Azure Cache for Redis Basic, Standard, and Premium tiers with the Azure CLI az redis commands.

Azure Cache for Redis Enterprise tiers and Azure Managed Redis use the az redisenterprise commands. The redisenterprise extension for Azure CLI version 2.61.0 or higher prompts you for installation the first time you run an az redisenterprise command.

To create and manage an Azure Cache for Redis Enterprise-tier cache, use the Azure Managed Redis scripts.

Create an Azure Cache for Redis cache

You can use the following Azure CLI script to create an Azure Cache for Redis Basic, Standard, or Premium-tier cache. To create and manage an Azure Cache for Redis Enterprise-tier cache, use the Azure Managed Redis scripts.

To create an Azure Cache for Redis Basic, Standard, or Premium cache by using Azure CLI, the name, location, resourceGroup, sku, and size parameters are required. Other parameters are optional and have defaults.

You can use the Azure CLI script in this section to create an Azure Cache for Redis Basic cache with default settings. You can also use the following other methods to create a cache:

The cache name must be a string of 1-63 characters that's unique in the Azure region. The name can contain only numbers, letters, and hyphens, must start and end with a number or letter, and can't contain consecutive hyphens.

The location should be an Azure region near other services that use your cache.

Choose a sku and size that have the appropriate features and performance for your cache.

Transport Layer Security (TLS) 1.2-1.3 encryption is enabled by default for all new caches. You can enable the non-TLS port and connections during or after cache creation, but for security reasons, disabling TLS isn't recommended.

Important

Microsoft Entra authentication is recommended for security. You can enable Microsoft Entra Authentication during or after cache creation.

Use Microsoft Entra ID with managed identities to authorize requests against your cache if possible. Authorization using Microsoft Entra ID and managed identity provides better security and is easier to use than shared access key authorization. For more information about using managed identities with your cache, see Use Microsoft Entra ID for cache authentication.

The following script uses the az group create and az redis create commands to create a resource group with an Azure Cache for Redis Basic C0 cache in it.


# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
resourceGroup="redis-cache-rg-$randomIdentifier"
tag="create-manage-cache"
cache="redis-cache-$randomIdentifier"
sku="basic"
size="C0"

# Create a resource group
echo "Creating $resourceGroup in "$location"..."
az group create --resource-group $resourceGroup --location "$location" --tags $tag

# Create a Basic C0 (256 MB) Azure Redis cache
echo "Creating $cache"
az redis create --name $cache --resource-group $resourceGroup --location "$location" --sku $sku --vm-size $size

Get details for an Azure Cache for Redis cache

The following script uses the az redis show and az redis list-keys commands to get and display the name, hostname, ports, and keys details for the preceding cache.

Important

The list-keys operation works only when access keys are enabled for the cache. The output of this command might compromise security by showing secrets, and may trigger a sensitive information warning. For more information, see Use Azure CLI to manage sensitive information.


# Get details of an Azure Cache for Redis cache
echo "Showing details of $cache"
az redis show --name "$cache" --resource-group $resourceGroup

# Retrieve the hostname and ports for an Azure Redis instance
redis=($(az redis show --name "$cache" --resource-group $resourceGroup --query [hostName,enableNonSslPort,port,sslPort] --output tsv))

# Retrieve the keys for an Azure Redis instance
keys=($(az redis list-keys --name "$cache" --resource-group $resourceGroup --query [primaryKey,secondaryKey] --output tsv))

# Display the retrieved hostname, keys, and ports
echo "Hostname:" ${redis[0]}
echo "Non SSL Port:" ${redis[2]}
echo "Non SSL Port Enabled:" ${redis[1]}
echo "SSL Port:" ${redis[3]}
echo "Primary Key:" ${keys[0]}
echo "Secondary Key:" ${keys[1]}

Clean up resources

The following script uses the az group delete and az redis delete commands to delete the preceding cache, and then delete its resource group.

# Delete an Azure Redis cache
echo "Deleting $cache"
az redis delete --name "$cache" --resource-group $resourceGroup -y

# echo "Deleting all resources"
az group delete --resource-group $resourceGroup -y