Terraform resource exits even after destroy command

Muhammad Amir 0 Reputation points
2024-10-23T10:39:32.01+00:00

Hi Guys

I am using terraform to deploy simple azure nodejs container, i activated diagnostic_setting setting through terraform and below is the code, when i run terraform destroy it destroys all resources created but when run terraform init && terraform apply it throws error saying the resource already exits. I manually check Azure portal but i am not able to find the resource. Where can i find this resource, were does it exits?

resource "azurerm_monitor_diagnostic_setting" "diagnostic_setting" {
  name               = "${azurerm_container_group.exp.name}-diagnostic-setting"
  target_resource_id = azurerm_container_group.exp.id

  log_analytics_workspace_id = azurerm_log_analytics_workspace.workspace.id

  enabled_log {
    category = "ContainerInstanceLog"
  }
}
Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,658 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Pranay Reddy Madireddy 6,180 Reputation points Microsoft External Staff Moderator
    2024-11-11T19:45:07.2366667+00:00

    Hii Muhammad Amir

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    Run "terraform state list" to see all the resources Terraform is managing and look for your diagnostic setting in the list.

    If the resource is in the state but not in Azure, you can remove it from Terraform's state using this command:

    terraform state rm azurerm_monitor_diagnostic_setting.diagnostic_setting

    This command tells Terraform to forget the resource, so you can run terraform apply without any errors.

    After removing the missing resource from the state, just run terraform apply again. Terraform will create any resources that are missing in Azure.

    After applying, check the Azure portal to confirm the diagnostic setting is created.

    Sometimes resources don't appear in the Azure portal because of filters or permissions. Make sure you're in the correct subscription and resource group.

    If a resource was created outside of Terraform and you want to manage it, use terraform import to bring it into Terraform.

    terraform import azurerm_monitor_diagnostic_setting.diagnostic_setting <resource_id>

    If you have any further queries, do let us know.


    If the answer is helpful, please click "Accept Answer" and "Upvote it".

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.