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.
I Tried to check Azure APIM autoscale settings using Terraform and with API Management service with a higher price tier, I was able to enable autoscaling successfully.
The error you're encountering, "SettingAlreadyExists," occurs because you are trying to create an autoscale setting that already exists for the specified Azure API Management (APIM) resource. This typically happens when you re-run the Terraform plan, and the previously created autoscale setting is still present. To avoid this error, you should ensure that you create the autoscale setting only if it doesn't already exist.
Here's a modified version of your Terraform configuration that checks whether the autoscale setting already exists before attempting to create it.
Terraform configuration:
terraform {
required_providers {
API = {
source = "Azure/azapi"
}
}
}
provider "azapi" {}
provider "azurerm" {
features {}
}
data "azurerm_resource_group" "rg" {
name = "v-bolliv"
}
data "azurerm_api_management" "apim" {
name = "vkdemoapi"
resource_group_name = data.azurerm_resource_group.rg.name
}
resource "azurerm_user_assigned_identity" "example" {
name = "demovksb"
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
}
resource "azapi_resource" "apim_scaling" {
type = "Microsoft.Insights/autoscalesettings@2022-10-01"
name = "Auto-scale-capacity-70-percentagevk"
location = data.azurerm_resource_group.rg.location
parent_id = data.azurerm_resource_group.rg.id
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 = data.azurerm_api_management.apim.location
metricResourceUri = data.azurerm_api_management.apim.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 = data.azurerm_api_management.apim.location
metricResourceUri = data.azurerm_api_management.apim.id
operator = "LessThan"
statistic = "Average"
threshold = 65
timeAggregation = "Average"
timeGrain = "PT1M"
timeWindow = "PT10M"
}
scaleAction = {
cooldown = "PT5M"
direction = "Decrease"
type = "ChangeCount"
value = "1"
}
}
]
}
]
targetResourceLocation = data.azurerm_api_management.apim.location
targetResourceUri = data.azurerm_api_management.apim.id
}
})
}
Output:
Note: This can be done with Higher tier pricing (Premium and standard) and only once for the API Management of a particular user ID.