Skip to content

Commit 77632d7

Browse files
committed
Create the Redis Data Access configuration from Terraform
1 parent 5eb6135 commit 77632d7

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

cache/spring3-sample/spring-cloud-azure-redis-sample-passwordless/README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser t
117117

118118
### Enable Microsoft Entra ID authentication on your cache
119119

120-
Refer [Enable Microsoft Entra ID authentication on your cache](https://learn.microsoft.com/azure/azure-cache-for-redis/cache-azure-active-directory-for-authentication#enable-microsoft-entra-id-authentication-on-your-cache) and copy the value of username.
120+
Refer to [Enable Microsoft Entra ID authentication on your cache](https://learn.microsoft.com/azure/azure-cache-for-redis/cache-azure-active-directory-for-authentication) for more information.
121121

122122
### Export Output to Your Local Environment
123123
Running the command below to export environment values:
@@ -126,21 +126,19 @@ Running the command below to export environment values:
126126

127127
```shell
128128
source ./terraform/setup_env.sh
129-
export AZURE_CACHE_REDIS_USERNAME='<copied-username>'
130129
```
131130

132131
#### Run with Powershell
133132

134133
```shell
135134
terraform\setup_env.ps1
136-
$env:AZURE_CACHE_REDIS_USERNAME='<copied-username>'
137135
```
138136

139137
If you want to run the sample in debug mode, you can save the output value.
140138

141139
```shell
142140
AZURE_CACHE_REDIS_HOST=...
143-
AZURE_CACHE_REDIS_USERNAME=<copied-username>
141+
AZURE_CACHE_REDIS_USERNAME=...
144142
```
145143

146144
## Run Locally

cache/spring3-sample/spring-cloud-azure-redis-sample-passwordless/terraform/main.tf

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ terraform {
22
required_providers {
33
azurerm = {
44
source = "hashicorp/azurerm"
5-
version = "3.9.0"
5+
version = "3.110.0"
66
}
77
azurecaf = {
88
source = "aztfmod/azurecaf"
9-
version = "1.2.16"
9+
version = "1.2.26"
1010
}
1111
}
1212
}
@@ -35,6 +35,7 @@ resource "azurerm_resource_group" "main" {
3535

3636
# =================== redis ================
3737
data "azurerm_subscription" "current" { }
38+
data "azuread_client_config" "current" {}
3839

3940
resource "azurecaf_name" "azurecaf_name_redis" {
4041
name = var.application_name
@@ -54,5 +55,15 @@ resource "azurerm_redis_cache" "redis" {
5455
minimum_tls_version = "1.2"
5556

5657
redis_configuration {
58+
enable_authentication = true
59+
active_directory_authentication_enabled = true
5760
}
5861
}
62+
63+
resource "azurerm_redis_cache_access_policy_assignment" "current_user" {
64+
name = "current-user"
65+
redis_cache_id = azurerm_redis_cache.redis.id
66+
access_policy_name = "Data Contributor"
67+
object_id = data.azuread_client_config.current.object_id
68+
object_id_alias = "current-user"
69+
}

cache/spring3-sample/spring-cloud-azure-redis-sample-passwordless/terraform/outputs.tf

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ output "AZURE_CACHE_REDIS_HOST" {
22
value = azurerm_redis_cache.redis.hostname
33
description = "The host name of the Redis instance."
44
}
5+
6+
output "AZURE_CACHE_REDIS_USERNAME" {
7+
value = data.azuread_client_config.current.object_id
8+
description = "The username of the Redis instance."
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
$env:AZURE_CACHE_REDIS_HOST=$(terraform -chdir=terraform output -raw AZURE_CACHE_REDIS_HOST)
2+
$env:AZURE_CACHE_REDIS_USERNAME=$(terraform -chdir=terraform output -raw AZURE_CACHE_REDIS_USERNAME)
23

34
echo AZURE_CACHE_REDIS_HOST=$env:AZURE_CACHE_REDIS_HOST
5+
echo AZURE_CACHE_REDIS_USERNAME=$env:AZURE_CACHE_REDIS_USERNAME
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export AZURE_CACHE_REDIS_HOST=$(terraform -chdir=./terraform output -raw AZURE_CACHE_REDIS_HOST)
2+
export AZURE_CACHE_REDIS_USERNAME=$(terraform -chdir=./terraform output -raw AZURE_CACHE_REDIS_USERNAME)
23

34
echo AZURE_CACHE_REDIS_HOST=AZURE_CACHE_REDIS_HOST
5+
echo AZURE_CACHE_REDIS_USERNAME=AZURE_CACHE_REDIS_USERNAME

0 commit comments

Comments
 (0)