Mladen Lukic
Mladen Lukic

Reputation: 21

Terraform destroying resources created with for_each

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"
}

Upvotes: 2

Views: 1793

Answers (1)

AmanGarg-MSFT
AmanGarg-MSFT

Reputation: 1153

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!

Upvotes: 0

Related Questions