I'm working on setting azure apim auto scaling using terraform. When the terraform plan is applied for the first time, auto scale settings are applied to azure APIM successfully. But when we re-run the terraform plan getting below error.
│ --------------------------------------------------------------------------------
│ RESPONSE 409: 409 Conflict
│ ERROR CODE: SettingAlreadyExists
│ --------------------------------------------------------------------------------
│ {
│ "code": "SettingAlreadyExists",
│ "message": "An autoscale setting already exists for target resource '/subscriptions/<subid>/resourceGroups/<rg>/providers/Microsoft.ApiManagement/service/<apim_name>'."
│ }
│ --------------------------------------------------------------------------------
│
Auto scaling settings are created using tf module
resource "azapi_resource" "apim_scaling" {
type = "Microsoft.Insights/autoscalesettings@2022-10-01"
name = "Auto-scale-capacity-70-percentage"
location = var.resource_group.location
parent_id = data.azurerm_resource_group.rg.id
tags = local.tags
body = jsonencode({
properties = {
enabled = true
name = "Auto-scale-capacity-70-percentage"
notifications = [
{
email = {
customEmails = [
"cloud@test.com"
]
sendToSubscriptionAdministrator = false
sendToSubscriptionCoAdministrators = false
}
operation = "Scale"
}
]
profiles = [
{
capacity = {
default = "1"
maximum = "3"
minimum = "1"
}
name = "Auto created default scale condition"
rules = [
{
metricTrigger = {
dimensions = []
dividePerInstance = false
metricName = "Capacity"
metricNamespace = "microsoft.apimanagement/service"
metricResourceLocation = azurerm_api_management.apim[0].location
metricResourceUri = azurerm_api_management.apim[0].id
operator = "GreaterThan"
statistic = "Average"
threshold = 70
timeAggregation = "Average"
timeGrain = "PT1M"
timeWindow = "PT10M"
}
scaleAction = {
cooldown = "PT5M"
direction = "Increase"
type = "ChangeCount"
value = "1"
}
},
{
metricTrigger = {
dimensions = []
dividePerInstance = false
metricName = "Capacity"
metricNamespace = "microsoft.apimanagement/service"
metricResourceLocation = azurerm_api_management.apim[0].location
metricResourceUri = azurerm_api_management.apim[0].id
operator = "LessThan"
statistic = "Average"
threshold = 65
timeAggregation = "Average"
timeGrain = "PT1M"
timeWindow = "PT10M"
}
scaleAction = {
cooldown = "PT5M"
direction = "Decrease"
type = "ChangeCount"
value = "1"
}
}
]
}
]
targetResourceLocation = azurerm_api_management.apim[0].location
targetResourceUri = azurerm_api_management.apim[0].id
}
})
depends_on = [
azurerm_api_management.apim
]
}
So, Could you please help how we can check whether auto scale settings are enabled on APIM before applying the auto scaling settings using terraform.