Skip to content

Commit

Permalink
Create the Redis Data Access configuration from Terraform (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdala authored Jul 1, 2024
1 parent 5eb6135 commit 8a6ccc2
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ You can go to [Azure portal](https://ms.portal.azure.com/) in your web browser t

### Enable Microsoft Entra ID authentication on your cache

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.
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.

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

```shell
source ./terraform/setup_env.sh
export AZURE_CACHE_REDIS_USERNAME='<copied-username>'
```

#### Run with Powershell

```shell
terraform\setup_env.ps1
$env:AZURE_CACHE_REDIS_USERNAME='<copied-username>'
```

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

```shell
AZURE_CACHE_REDIS_HOST=...
AZURE_CACHE_REDIS_USERNAME=<copied-username>
AZURE_CACHE_REDIS_USERNAME=...
```

## Run Locally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.9.0"
version = "3.110.0"
}
azurecaf = {
source = "aztfmod/azurecaf"
version = "1.2.16"
version = "1.2.26"
}
}
}
Expand Down Expand Up @@ -35,6 +35,7 @@ resource "azurerm_resource_group" "main" {

# =================== redis ================
data "azurerm_subscription" "current" { }
data "azuread_client_config" "current" {}

resource "azurecaf_name" "azurecaf_name_redis" {
name = var.application_name
Expand All @@ -54,5 +55,15 @@ resource "azurerm_redis_cache" "redis" {
minimum_tls_version = "1.2"

redis_configuration {
enable_authentication = true
active_directory_authentication_enabled = true
}
}

resource "azurerm_redis_cache_access_policy_assignment" "current_user" {
name = "current-user"
redis_cache_id = azurerm_redis_cache.redis.id
access_policy_name = "Data Contributor"
object_id = data.azuread_client_config.current.object_id
object_id_alias = "current-user"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ output "AZURE_CACHE_REDIS_HOST" {
value = azurerm_redis_cache.redis.hostname
description = "The host name of the Redis instance."
}

output "AZURE_CACHE_REDIS_USERNAME" {
value = data.azuread_client_config.current.object_id
description = "The username of the Redis instance."
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
$env:AZURE_CACHE_REDIS_HOST=$(terraform -chdir=terraform output -raw AZURE_CACHE_REDIS_HOST)
$env:AZURE_CACHE_REDIS_USERNAME=$(terraform -chdir=terraform output -raw AZURE_CACHE_REDIS_USERNAME)

echo AZURE_CACHE_REDIS_HOST=$env:AZURE_CACHE_REDIS_HOST
echo AZURE_CACHE_REDIS_USERNAME=$env:AZURE_CACHE_REDIS_USERNAME
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export AZURE_CACHE_REDIS_HOST=$(terraform -chdir=./terraform output -raw AZURE_CACHE_REDIS_HOST)
export AZURE_CACHE_REDIS_USERNAME=$(terraform -chdir=./terraform output -raw AZURE_CACHE_REDIS_USERNAME)

echo AZURE_CACHE_REDIS_HOST=AZURE_CACHE_REDIS_HOST
echo AZURE_CACHE_REDIS_USERNAME=AZURE_CACHE_REDIS_USERNAME

0 comments on commit 8a6ccc2

Please sign in to comment.