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
So I have found one solution, please feel free to comment if there are better options available
Added null resource like below
In your module, add an output:
Then in your top level Terraform, reference that output: