Hello Team,
I’m creating a test env in Azure using terraform and all is working so far except my alerts (deploying Azure Container Apps). Would appreciate some help and guidance:
aca.tf
resource "azapi_resource" "aca_env" {
type = "Microsoft.App/managedEnvironments@2022-10-01"
parent_id = azurerm_resource_group.rg.id
location = azurerm_resource_group.rg.location
name = "aca-env-terraform"
tags = local.tags
body = jsonencode({
properties = {
appLogsConfiguration = {
destination = "log-analytics"
logAnalyticsConfiguration = {
customerId = azurerm_log_analytics_workspace.law.workspace_id
sharedKey = azurerm_log_analytics_workspace.law.primary_shared_key
}
}
}
})
}
resource "azapi_resource" "aca" {
for_each = { for ca in var.container_apps: ca.name => ca}
type = "Microsoft.App/containerApps@2022-10-01"
parent_id = azurerm_resource_group.rg.id
location = azurerm_resource_group.rg.location
name = each.value.name
body = jsonencode({
properties: {
managedEnvironmentId = azapi_resource.aca_env.id
configuration = {
ingress = {
external = each.value.ingress_enabled
targetPort = each.value.ingress_enabled?each.value.containerPort: null
}
}
template = {
containers = [
{
name = "main"
image = "${each.value.image}:${each.value.tag}"
resources = {
cpu = each.value.cpu_requests
memory = each.value.mem_requests
}
}
]
scale = {
minReplicas = each.value.min_replicas
maxReplicas = each.value.max_replicas
}
}
}
})
tags = local.tags
}
variables.tf
variable "location" {
type = string
default = "westus2"
description = "WestUS2"
}
variable "container_apps" {
type = list(object({
name = string
image = string
tag = string
containerPort = number
ingress_enabled = bool
min_replicas = number
max_replicas = number
cpu_requests = number
mem_requests = string
}))
default = [ {
image = "nginx"
name = "kc-container-app"
tag = "latest"
containerPort = 80
ingress_enabled = true
min_replicas = 1
max_replicas = 2
cpu_requests = 0.5
mem_requests = "1.0Gi"
},
{
image = "nginx"
name = "kc-container-app-again"
tag = "latest"
containerPort = 80
ingress_enabled = true
min_replicas = 1
max_replicas = 2
cpu_requests = 0.5
mem_requests = "1.0Gi"
}]
}
monitor.tf
resource "azurerm_monitor_action_group" "email_alert" {
name = "email-alert"
resource_group_name = azurerm_resource_group.rg.name
short_name = "email-alert"
email_receiver {
name = "send-email-alert"
email_address = "email@email.com"
use_common_alert_schema = true
}
depends_on = [
azurerm_resource_group.rg
]
}
resource "azurerm_monitor_metric_alert" "CPU_Usage_alert" {
name = "CPU_Usage_alert"
resource_group_name = azurerm_resource_group.rg.name
scopes = [azapi_resource.aca.id]
description = "Alert triggers if CPU Usage exceeds 70000 NanoCores (70%)"
target_resource_type = "Microsoft.App/containerapps"
criteria {
metric_namespace = "Microsoft.App/containerapps"
metric_name = "UsageNanoCores"
aggregation = "Average"
operator = "GreaterThan"
threshold = 70000
}
action {
action_group_id = azurerm_monitor_action_group.email_alert.id
}
depends_on = [
azapi_resource.aca,
azurerm_monitor_action_group.email_alert
]
}
Error:
│ Error: Missing resource instance key
│
│ on monitor.tf line 20, in resource "azurerm_monitor_metric_alert" "CPU_Usage_alert":
│ 20: scopes = [azapi_resource.aca.id]
│
│ Because azapi_resource.aca has "for_each" set, its attributes must be accessed on specific instances.
│
│ For example, to correlate with indices of a referring resource, use:
│ azapi_resource.aca[each.key]
Thank you in advance!