Skip to content

Commit d4a1e17

Browse files
authored
Merge pull request #299351 from v-thepet/redis15
Azure for Redis Cache Freshness #15
2 parents 75c2366 + 642307a commit d4a1e17

File tree

1 file changed

+109
-83
lines changed

1 file changed

+109
-83
lines changed
Lines changed: 109 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,101 @@
11
---
2-
title: Create, query, and delete an Azure Redis cache - Azure CLI
3-
description: This Azure CLI code sample shows how to create an Azure Managed Redis instance using the command az redisenterprise create. It then gets details of an Azure Managed Redis instance, including provisioning status, the hostname, ports, and keys for an Azure Redis instance. Finally, it deletes the cache.
2+
title: Create, query, and delete a cache using Azure CLI
3+
description: Use the Azure CLI to create an Azure Redis instance, get cache details like status, hostname, ports, and keys, and delete the cache.
44

55

66
ms.devlang: azurecli
77
ms.topic: sample
8-
ms.date: 03/11/2022
8+
ms.date: 05/08/2025
99
zone_pivot_groups: redis-type
1010
ms.custom: devx-track-azurecli, ignite-2024
1111
appliesto:
1212
- ✅ Azure Managed Redis
1313
- ✅ Azure Cache for Redis
1414
---
1515

16-
# Create an Azure Redis cache using the Azure CLI
16+
# Manage an Azure Redis cache using the Azure CLI
1717

18-
In this scenario, you learn how to create an Azure Redis cache instance. You then learn to get details of the cache, including provisioning status, the hostname, ports, and keys for the cache. Finally, you learn to delete the cache.
18+
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.
1919

20-
[!INCLUDE [quickstarts-free-trial-note](~/reusable-content/ce-skilling/azure/includes/quickstarts-free-trial-note.md)]
20+
## Prerequisites
2121

22-
[!INCLUDE [azure-cli-prepare-your-environment.md](~/reusable-content/azure-cli/azure-cli-prepare-your-environment.md)]
23-
24-
## Sample script
22+
- [!INCLUDE [quickstarts-free-trial-note](~/reusable-content/ce-skilling/azure/includes/quickstarts-free-trial-note.md)]
23+
[!INCLUDE [azure-cli-prepare-your-environment-no-header.md](~/reusable-content/azure-cli/azure-cli-prepare-your-environment-no-header.md)]
24+
- 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.
2525

2626
::: zone pivot="azure-managed-redis"
2727

28-
## Azure Managed Redis
28+
>[!NOTE]
29+
>Azure Managed Redis uses the Azure CLI [az redisenterprise](/cli/azure/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.
30+
>
31+
>Azure Cache for Redis uses the `az redisenterprise` commands for Enterprise tiers and the [az redis](/cli/azure/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](create-manage-cache.md?pivots=azure-cache-redis) scripts.
2932
30-
[!INCLUDE [managed-redis-create](../includes/managed-redis-create.md)]
33+
## Create an Azure Managed Redis cache
3134

32-
::: zone-end
35+
<!--
36+
:::code language="azurecli" source="~/azure_cli_scripts/redis-cache/create-cache/create-manage-cache.sh" id="FullScript":::
37+
This sample is broken. When it is fixed, we can fix this include.
38+
-->
39+
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.
3340

34-
::: zone pivot="azure-cache-redis"
41+
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:
3542

36-
## Azure Cache for Redis
43+
- [Azure portal](../quickstart-create-managed-redis.md)
44+
- [Azure PowerShell](../how-to-manage-redis-cache-powershell.md?pivots=azure-managed-redis)
45+
- [ARM template](../redis-cache-arm-provision.md#azure-managed-redis-preview)
46+
- [Bicep template](../redis-cache-bicep-provision.md#azure-managed-redis-preview)
3747

38-
[!INCLUDE [redis-cache-create](~/reusable-content/ce-skilling/azure/includes/azure-cache-for-redis/includes/redis-cache-create.md)]
48+
The cache `name` must be a string of 1-63 characters that's unique in the [Azure region](https://azure.microsoft.com/regions/). The name can contain only numbers, letters, and hyphens, must start and end with a number or letter, and can't contain consecutive hyphens.
3949

40-
::: zone-end
50+
The `location` should be an Azure region near other services that use your cache.
4151

42-
::: zone pivot="azure-managed-redis"
52+
Choose a [sku](https://azure.microsoft.com/pricing/details/cache/) that has the appropriate features and performance for your cache.
4353

44-
[!INCLUDE [cli-launch-cloud-shell-sign-in.md](~/reusable-content/ce-skilling/azure/includes/cli-launch-cloud-shell-sign-in.md)]
54+
Microsoft Entra authentication is enabled by default for all new caches and is recommended for security.
4555

46-
### Run the script
56+
>[!IMPORTANT]
57+
>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](../entra-for-authentication.md).
4758
48-
<!--
49-
:::code language="azurecli" source="~/azure_cli_scripts/redis-cache/create-cache/create-manage-cache.sh" id="FullScript":::
50-
This sample is broken. When it is fixed, we can fix this include.
51-
-->
59+
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.
60+
61+
The following script sets variables, and then uses the [az group create](/cli/azure/group) and [az redisenterprise create](/cli/azure/redisenterprise#az-redisenterprise-create) commands to create a resource group with an Azure Managed Redis Balanced B1 SKU cache in it.
5262

5363
```azurecli
5464
5565
# Variable block
5666
let "randomIdentifier=$RANDOM*$RANDOM"
5767
location="East US"
58-
resourceGroup="msdocs-redis-cache-rg-$randomIdentifier"
68+
resourceGroup="redis-cache-rg-$randomIdentifier"
5969
tag="create-manage-cache"
60-
cache="msdocs-redis-cache-$randomIdentifier"
70+
cache="redis-cache-$randomIdentifier"
6171
sku="Balanced_B1"
6272
6373
# Create a resource group
6474
echo "Creating $resourceGroup in "$location"..."
6575
az group create --resource-group $resourceGroup --location "$location" --tags $tag
6676
67-
# Create a Balanced B1 Azure Managed Redis Cache
77+
# Create a Balanced B1 Azure Managed Redis cache
6878
echo "Creating $cache"
6979
az redisenterprise create --name $cache --resource-group $resourceGroup --location "$location" --sku $sku
80+
```
81+
82+
## Get details for an Azure Managed Redis cache
83+
84+
The following script uses the [az redisenterprise show](/cli/azure/redisenterprise#az-redisenterprise-show) and [az redisenterprise database list-keys](/cli/azure/redisenterprise/database#az-redisenterprise-database-list-keys) commands to get and display the name, hostname, ports, and keys details for the preceding cache.
7085

71-
# Get details of an Azure Managed Redis
86+
>[!IMPORTANT]
87+
>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](https://go.microsoft.com/fwlink/?linkid=2258669).
88+
89+
```azurecli
90+
# Get details of an Azure Managed Redis cache
7291
echo "Showing details of $cache"
7392
az redisenterprise show --name "$cache" --resource-group $resourceGroup
7493
7594
# Retrieve the hostname and ports for an Azure Redis Cache instance
7695
redis=($(az redisenterprise show --name "$cache" --resource-group $resourceGroup --query [hostName,enableNonSslPort,port,sslPort] --output tsv))
7796
7897
# Retrieve the keys for an Azure Redis Cache instance
79-
keys=($(az redisenterprise list-keys --name "$cache" --resource-group $resourceGroup --query [primaryKey,secondaryKey] --output tsv))
98+
keys=($(az redisenterprise database list-keys --cluster-name "$cache" --resource-group $resourceGroup --query [primaryKey,secondaryKey] --output tsv))
8099
81100
# Display the retrieved hostname, keys, and ports
82101
echo "Hostname:" ${redis[0]}
@@ -86,75 +105,98 @@ echo "SSL Port:" ${redis[3]}
86105
echo "Primary Key:" ${keys[0]}
87106
echo "Secondary Key:" ${keys[1]}
88107
108+
```
109+
110+
## Clean up resources
111+
112+
The following script uses the [az group delete](/cli/azure/group) and [az redisenterprise delete](/cli/azure/redisenterprise#az-redisenterprise-delete) commands to delete the preceding cache, and then delete its resource group.
113+
114+
```azurecli
89115
# Delete a redis cache
90116
echo "Deleting $cache"
91117
az redisenterprise delete --name "$cache" --resource-group $resourceGroup -y
92118
93119
# echo "Deleting all resources"
94120
az group delete --resource-group $resourceGroup -y
95-
96121
```
97122

98-
## Clean up resources
123+
::: zone-end
99124

100-
[!INCLUDE [cli-clean-up-resources.md](~/reusable-content/ce-skilling/azure/includes/cli-clean-up-resources.md)]
125+
::: zone pivot="azure-cache-redis"
101126

102-
```azurecli
103-
az group delete --resource-group $resourceGroup
104-
```
127+
>[!IMPORTANT]
128+
>Use these scripts to create and manage Azure Cache for Redis Basic, Standard, and Premium tiers with the Azure CLI [az redis](/cli/azure/redis) commands.
129+
>
130+
>Azure Cache for Redis Enterprise tiers and Azure Managed Redis use the [az redisenterprise](/cli/azure/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.
131+
>
132+
>To create and manage an Azure Cache for Redis Enterprise-tier cache, use the [Azure Managed Redis](create-manage-cache.md?pivots=azure-managed-redis) scripts.
105133
106-
## Sample reference
134+
## Create an Azure Cache for Redis cache
107135

108-
This script uses the following commands to create a resource group and an Azure Managed Redis. Each command in the table links to command specific documentation.
136+
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](create-manage-cache.md?pivots=azure-managed-redis) scripts.
109137

110-
| Command | Notes |
111-
|---|---|
112-
| [az group create](/cli/azure/group) | Creates a resource group in which all resources are stored. |
113-
| [az redisenterprise create](/cli/azure/redis) | Create Azure Managed Redis instance. |
114-
| [az redisenterprise show](/cli/azure/redis) | Retrieve details of an Azure Managed Redis instance. |
115-
| [az redisenterprise list-keys](/cli/azure/redis) | Retrieve access keys for an Azure Managed Redis instance. |
116-
| [az redisenterprise delete](/cli/azure/redis) | Delete Azure Managed Redis instance. |
138+
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.
117139

118-
::: zone-end
140+
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:
119141

120-
::: zone pivot="azure-cache-redis"
142+
- [Azure portal (Basic, Standard, or Premium)](../../azure-cache-for-redis/quickstart-create-redis.md)
143+
- [Azure portal (Enterprise)](../../azure-cache-for-redis/quickstart-create-redis-enterprise.md)
144+
- [Azure PowerShell](../how-to-manage-redis-cache-powershell.md?pivots=azure-cache-redis)
145+
- [ARM template](../redis-cache-arm-provision.md#azure-cache-for-redis)
146+
- [Bicep template](../redis-cache-bicep-provision.md#azure-cache-for-redis)
121147

122-
[!INCLUDE [cli-launch-cloud-shell-sign-in.md](~/reusable-content/ce-skilling/azure/includes/cli-launch-cloud-shell-sign-in.md)]
148+
The cache `name` must be a string of 1-63 characters that's unique in the [Azure region](https://azure.microsoft.com/regions/). The name can contain only numbers, letters, and hyphens, must start and end with a number or letter, and can't contain consecutive hyphens.
123149

124-
### Run the script
150+
The `location` should be an Azure region near other services that use your cache.
125151

126-
<!--
127-
:::code language="azurecli" source="~/azure_cli_scripts/redis-cache/create-cache/create-manage-cache.sh" id="FullScript":::
128-
This sample is broken. When it is fixed, we can fix this include.
129-
-->
152+
Choose a [sku](https://azure.microsoft.com/pricing/details/cache/) and `size` that have the appropriate features and performance for your cache.
153+
154+
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.
155+
156+
>[!IMPORTANT]
157+
>Microsoft Entra authentication is recommended for security. You can enable Microsoft Entra Authentication during or after cache creation.
158+
>
159+
>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](../../azure-cache-for-redis/cache-azure-active-directory-for-authentication.md).
160+
161+
The following script uses the [az group create](/cli/azure/group) and [az redis create](/cli/azure/redis#az-redis-create) commands to create a resource group with an Azure Cache for Redis Basic C0 cache in it.
130162

131163
```azurecli
132164
133165
# Variable block
134166
let "randomIdentifier=$RANDOM*$RANDOM"
135167
location="East US"
136-
resourceGroup="msdocs-redis-cache-rg-$randomIdentifier"
168+
resourceGroup="redis-cache-rg-$randomIdentifier"
137169
tag="create-manage-cache"
138-
cache="msdocs-redis-cache-$randomIdentifier"
170+
cache="redis-cache-$randomIdentifier"
139171
sku="basic"
140172
size="C0"
141173
142174
# Create a resource group
143175
echo "Creating $resourceGroup in "$location"..."
144176
az group create --resource-group $resourceGroup --location "$location" --tags $tag
145177
146-
# Create a Basic C0 (256 MB) Redis Cache
178+
# Create a Basic C0 (256 MB) Azure Redis cache
147179
echo "Creating $cache"
148180
az redis create --name $cache --resource-group $resourceGroup --location "$location" --sku $sku --vm-size $size
181+
```
149182

150-
# Get details of an Azure Cache for Redis
183+
## Get details for an Azure Cache for Redis cache
184+
185+
The following script uses the [az redis show](/cli/azure/redis#az-redis-show) and [az redis list-keys](/cli/azure/redis#az-redis-list-keys) commands to get and display the name, hostname, ports, and keys details for the preceding cache.
186+
187+
>[!IMPORTANT]
188+
>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](https://go.microsoft.com/fwlink/?linkid=2258669).
189+
190+
```azurecli
191+
192+
# Get details of an Azure Cache for Redis cache
151193
echo "Showing details of $cache"
152194
az redis show --name "$cache" --resource-group $resourceGroup
153195
154-
# Retrieve the hostname and ports for an Azure Redis Cache instance
196+
# Retrieve the hostname and ports for an Azure Redis instance
155197
redis=($(az redis show --name "$cache" --resource-group $resourceGroup --query [hostName,enableNonSslPort,port,sslPort] --output tsv))
156198
157-
# Retrieve the keys for an Azure Redis Cache instance
199+
# Retrieve the keys for an Azure Redis instance
158200
keys=($(az redis list-keys --name "$cache" --resource-group $resourceGroup --query [primaryKey,secondaryKey] --output tsv))
159201
160202
# Display the retrieved hostname, keys, and ports
@@ -164,40 +206,24 @@ echo "Non SSL Port Enabled:" ${redis[1]}
164206
echo "SSL Port:" ${redis[3]}
165207
echo "Primary Key:" ${keys[0]}
166208
echo "Secondary Key:" ${keys[1]}
167-
168-
# Delete a redis cache
169-
echo "Deleting $cache"
170-
az redis delete --name "$cache" --resource-group $resourceGroup -y
171-
172-
# echo "Deleting all resources"
173-
az group delete --resource-group $resourceGroup -y
174-
175209
```
176210

177211
## Clean up resources
178212

179-
[!INCLUDE [cli-clean-up-resources.md](~/reusable-content/ce-skilling/azure/includes/cli-clean-up-resources.md)]
213+
The following script uses the [az group delete](/cli/azure/group) and [az redis delete](/cli/azure/redis#az-redis-delete) commands to delete the preceding cache, and then delete its resource group.
180214

181215
```azurecli
182-
az group delete --resource-group $resourceGroup
183-
```
184-
185-
## Sample reference
186-
187-
This script uses the following commands to create a resource group and an Azure Cache for Redis. Each command in the table links to command specific documentation.
216+
# Delete an Azure Redis cache
217+
echo "Deleting $cache"
218+
az redis delete --name "$cache" --resource-group $resourceGroup -y
188219
189-
| Command | Notes |
190-
|---|---|
191-
| [az group create](/cli/azure/group) | Creates a resource group in which all resources are stored. |
192-
| [az redis create](/cli/azure/redis) | Create Azure Managed Redis instance. |
193-
| [az redis show](/cli/azure/redis) | Retrieve details of an Azure Managed Redis instance. |
194-
| [az redis list-keys](/cli/azure/redis) | Retrieve access keys for an Azure Managed Redis instance. |
195-
| [az redis delete](/cli/azure/redis) | Delete Azure Managed Redis instance. |
220+
# echo "Deleting all resources"
221+
az group delete --resource-group $resourceGroup -y
222+
```
196223

197224
::: zone-end
198225

199-
## Next steps
200-
201-
For more information on the Azure CLI, see [Azure CLI documentation](/cli/azure).
226+
## Related content
202227

203-
For an Azure Managed Redis CLI script sample that creates a Azure Managed Redis with clustering, see [Azure Managed Redis with Clustering](../../azure-cache-for-redis/scripts/create-manage-premium-cache-cluster.md)
228+
- [Azure CLI documentation](/cli/azure)
229+
- [Create a Premium Azure Cache for Redis with clustering](../../azure-cache-for-redis/scripts/create-manage-premium-cache-cluster.md)

0 commit comments

Comments
 (0)