How can we fetch Redis Cache Server hostname and primary access key and update in azure function app settings

am trying to create the redis cache server and update the redis hostname & Primary access key to Azure function app setting to connect the azure function app to connect redis server.
below is my code
Redi Cache server:
resource “azurerm_redis_cache” “redis” {
name = var.redis_cache_name
location = var.location
resource_group_name = var.resource_group_name
capacity = var.capacity
family = var.family
sku_name = var.sku_name

enable_non_ssl_port = false

redis_configuration {
maxmemory_policy = “allkeys-lru” # Eviction policy
maxmemory_reserved = “30”
maxfragmentationmemory_reserved = “30”
}
}
output “redis_hostname” {
value = azurerm_redis_cache.redis.name
}

output “redis_primary_key” {
value = azurerm_redis_cache.redis.primary_access_key
}

Main.tf:
module “redis_cache” {
source = “./modules/redis_cache”
redis_cache_name = var.redis_cache_name
location = var.location
resource_group_name = module.resource_group.web_app_resource_group_id
capacity = var.redis_capacity
family = var.redis_family
sku_name = var.redis_sku_name
redis_subnet_id = module.network.api_plink_subnet_id
vnet_id = module.network.vnet_id
}
output “redis_cache” {
value = {
hostname = module.redis_cache.redis_hostname
key = module.redis_cache.redis_primary_access_key
}
}
#App Service
module “private_web_app” {
source = “./modules/private_web_app”
private_api_app_name = var.api_app_name
resource_group_name = module.resource_group.web_app_resource_group_id
location = var.location
app_service_plan_id = module.app_service_plan.app_service_plan_id
private_api_subnet_id = module.network.private_api_subnet_id
cosmosdb_account_endpoint = module.database.cosmosdb_account_endpoint
cosmosdb_account_key = module.database.cosmosdb_account_key
redis_hostname = module.redis_cache.redis_hostname
redis_primary_key = module.redis_cache.redis_primary_access_key
vnet_id = module.network.vnet_id
}

getting below error during the validation
Error: Unsupported attribute

│ on main.tf line 67, in module “private_web_app”:
│ 67: redis_hostname = module.redis_cache.redis_hostname
│ ├────────────────
│ │ module.redis_cache is a object

│ This object does not have an attribute named “redis_hostname”.


│ Error: Unsupported attribute

│ on main.tf line 68, in module “private_web_app”:
│ 68: redis_primary_key = module.redis_cache.redis_primary_access_key
│ ├────────────────
│ │ module.redis_cache is a object

│ This object does not have an attribute named “redis_primary_access_key”.

Please put your terraform code in code blocks, </> in the text entry menu bar.

resource "azurerm_redis_cache" "redis" {
  name                = var.redis_cache_name
  location            = var.location
  resource_group_name = var.resource_group_name
  capacity            = var.capacity
  family              = var.family
  sku_name            = var.sku_name

  enable_non_ssl_port = false

  redis_configuration {
    maxmemory_policy = "allkeys-lru" # Eviction policy
    maxmemory_reserved  = "30"
    maxfragmentationmemory_reserved = "30"
  }
}

output "redis_hostname" {
  value = azurerm_redis_cache.redis.name
}

output "redis_primary_key" {
  value = azurerm_redis_cache.redis.primary_access_key
}

Main.tf in the root directory

module "redis_cache" {
  source = "./modules/redis_cache"
  redis_cache_name          = var.redis_cache_name
  location                  = var.location
  resource_group_name       = module.resource_group.web_app_resource_group_id
  capacity                  = var.redis_capacity
  family                    = var.redis_family
  sku_name                  = var.redis_sku_name
  redis_subnet_id           = module.network.api_plink_subnet_id
  vnet_id                   = module.network.vnet_id
}
module "private_web_app" {
  source = "./modules/private_web_app"
  private_api_app_name      = var.api_app_name
  resource_group_name       = module.resource_group.web_app_resource_group_id
  location                  = var.location
  app_service_plan_id       = module.app_service_plan.app_service_plan_id
  private_api_subnet_id     = module.network.private_api_subnet_id
  cosmosdb_account_endpoint = module.database.cosmosdb_account_endpoint
  cosmosdb_account_key      = module.database.cosmosdb_account_key
  redis_hostname            = module.redis_cache.redis_hostname
  redis_primary_key         = module.redis_cache.redis_primary_access_key
  vnet_id                   = module.network.vnet_id
}

below is the output.tf in the root directory

output "redis_cache" {
  value = {
    hostname = module.redis_cache.redis_hostname
    key      = module.redis_cache.redis_primary_access_key
  }
}

Where is the source code for the modules?

it’s in Azure repos and it’s my folder structure

can i get the help to resolve my issues

Please share the modules.

Shall i attach the code here or is there any other way to share?

It is probably best put in a public GitHub repo.

Hi,

here is the GitHub link. could you please check and help me to resolve the issue
BalaGit/webproject (github.com)

You didn’t name the outputs file correctly in your redis module, it was missing the tf extension:

renamed:    modules/redis_cache/output -> modules/redis_cache/output.tf

Validates for me then.

Also you might want to run terraform format.

Thanks for the support