skip to Main Content

I would like to update one environment variable value for Azure Container App with the help of terraform. To accomplish this I am using "azapi_update_resource" but when I do so it removes all and add the one trying to add.

How can I update single environment value with the help of "azapi_update_resource" or is it not possible?

Below is the sample code I have tried and its not working.

I would really appreciate if someone can help in this

terraform {
  required_providers {
    azapi = {
      source = "Azure/azapi"
    }
  }
}

provider "azapi" {
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "resource_group"
  location = "East US"
}

resource "azurerm_log_analytics_workspace" "example" {
  name                = "acctest-01"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  sku                 = "PerGB2018"
  retention_in_days   = 30
}

resource "azurerm_container_app_environment" "example" {
  name                       = "Example-Environment"
  location                   = azurerm_resource_group.example.location
  resource_group_name        = azurerm_resource_group.example.name
  log_analytics_workspace_id = azurerm_log_analytics_workspace.example.id
}
resource "azurerm_container_app" "example" {
  name                         = "example-app"
  container_app_environment_id = azurerm_container_app_environment.example.id
  resource_group_name          = azurerm_resource_group.example.name
  revision_mode                = "Single"

  template {
    container {
      name   = "examplecontainerapp"
      image  = "mcr.microsoft.com/azuredocs/containerapps-helloworld:latest"
      cpu    = 0.25
      memory = "0.5Gi"

        env {
            name = "keyname"
            value = "keyvalue"
        }
    }
  }
}

resource "azapi_update_resource" "example" {
  type        = "Microsoft.App/containerApps@2022-10-01"
  resource_id = azurerm_container_app.example.id
  
  body = jsonencode({
    properties = {
      template = {
        containers = [
          {
            env = [
              {
                name = "testname"
                value = "testvalue"
              }
            ]
          }
        ]
      }
    }
  })
}

2

Answers


  1. Chosen as BEST ANSWER

    So I have found one solution, please feel free to comment if there are better options available

    Added null resource like below

    resource "null_resource" "update_env_value" {
      depends_on = [azurerm_container_app.example]
      triggers = {
        always_run = "${timestamp()}"
      }
      provisioner "local-exec" {
        command = "az containerapp update --name example-app --resource-group resource_groupn --set-env-vars azkey=azvalue3"
      }
    }
    

  2. In your module, add an output:

    output "my_module_value" {
      value = "testvalue"  # Whatever value reference you need here
    }
    

    Then in your top level Terraform, reference that output:

    module "my_module" {
      # Your module parameters
    }
    
    resource "azurerm_container_app" "example" {
      name                         = "example-app"
      container_app_environment_id = azurerm_container_app_environment.example.id
      resource_group_name          = azurerm_resource_group.example.name
      revision_mode                = "Single"
    
      template {
        container {
          name   = "examplecontainerapp"
          image  = "mcr.microsoft.com/azuredocs/containerapps-helloworld:latest"
          cpu    = 0.25
          memory = "0.5Gi"
    
            env {
                name = "keyname"
                value = "keyvalue"
            }
    
            env {
                name = "testname"
                value = module.my_module.my_module_value
            }
        }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search