2
votes

I am new to Terraform and experimenting to Get some experience. But i run to some problems at the start. I am declaring variable with some names which will be created as web apps in Azure and databases connected to web apps with same name. Later in the Code i am running for_each and creating the resources. My problem is that resouces are getting created fine but they are not getting destroyed by running terraform destroy command.

At the same time after those resources are created if i run Terraform plan i Get huge error message stating that it can not contact Azure api. I do not Get error if i manually delete those resources in Azure portal.

If i run Terraform destroy -target azurerm_mysql_database.mladenl222 it is successfull but the Resource is not getting destroyed.

Same problem occurs if i create Azure web apps by using ARM Template. I create web app using predefined ARM Template and passing some parameters in Terraform Code. All works fine and Resource is getting created, but it is not being destroyed by Terraform destroy Command. Command States success but nothing is deleteed. Below is some code example.

variable "students_2025"{
    type = set(string)
    default = ["test222","test12345"]
}

resource "azurerm_mysql_database" "default" {
   for_each = var.students_2025 
  name                = "${each.key}"
  resource_group_name = azurerm_resource_group.RG_mok_2025.name
  server_name         = azurerm_mysql_server.wp-db-mok-2025.name
  charset             = "utf8"
  collation           = "utf8_unicode_ci"
}
1
I'm not sure about the foreach problem, but for the template its expected. terraform doesnt know anything about your template. so it wont be able to delete resources created by the template.4c74356b41
Does Terraform destroy the objects as expected if you remove the -target option from your command line? The -target option is for exceptional circumstances only and not for routine use, and adds some considerable extra complexity to Terraform's behavior. You should be able to to run all Terraform commands without -target most of the time.Martin Atkins
Hi, thanks for your answers. If i try just to run terraform destroy i get error message as shown below. Same for each resource created by for_each. Same error message is displayed if i run terraform apply. I need to delete manually those resources in azure portal to be able to run plan or apply on other resources. Error making Read request on AzureRM App Service Source Control "mladenl222": web.AppsClient#GetSourceControl: Failure responding to request: StatusCode=400Mladen Lukic

1 Answers

0
votes

I used the below and terraform apply and destroy are working as intended:

variable "students_2025"{
    type = set(string)
    default = ["test222","test12345"]
}

resource "azurerm_resource_group" "test" {
  name     = "amgar-mysql-server"
  location = "West Europe"
}

resource "azurerm_mysql_server" "test" {
  name                = "amgar-sql-server"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"

  sku {
    name     = "B_Gen5_2"
    capacity = 2
    tier     = "Basic"
    family   = "Gen5"
  }

  storage_profile {
    storage_mb            = 5120
    backup_retention_days = 7
    geo_redundant_backup  = "Disabled"
  }

  administrator_login          = "mysqladminun"
  administrator_login_password = "H@Sh1CoR3!"
  version                      = "5.7"
  ssl_enforcement              = "Enabled"
}

resource "azurerm_mysql_database" "test" {
  for_each = var.students_2025
  name                = each.key
  resource_group_name = "${azurerm_resource_group.test.name}"
  server_name         = "${azurerm_mysql_server.test.name}"
  charset             = "utf8"
  collation           = "utf8_unicode_ci"
}

I am using the latest terraform version 0.12.9

Hope this helps!