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