Terraform destroying resource on every apply

154 Views Asked by At

I am using the below tf template module to create Mongo DB Atlas private link and connect to the Azure private end point . Pls note I am using a diff module for creating the Mongo Atlas cluster

My problem is every time I do a terraform apply, the Mongo DB Atlas private link is getting destroyed and a new private link is getting created. Is there a way to resolve this . Thanks.

resource "mongodbatlas_privatelink_endpoint" "test" {
  project_id    = var.project_id
  provider_name = "AZURE"
  region        = "eastus2"
}

resource "azurerm_private_endpoint" "test" {
  name                = "endpoint-test"
  location            = data.azurerm_resource_group.test.location
  resource_group_name = var.resource_group_name
  subnet_id           = azurerm_subnet.test.id
  private_service_connection {
    name                           = mongodbatlas_privatelink_endpoint.test.private_link_service_name
    private_connection_resource_id = mongodbatlas_privatelink_endpoint.test.private_link_service_resource_id
    is_manual_connection           = true
    request_message                = "Azure Private Link test"
  }

}

resource "mongodbatlas_privatelink_endpoint_service" "test" {
  project_id                  = mongodbatlas_privatelink_endpoint.test.project_id
  private_link_id             = mongodbatlas_privatelink_endpoint.test.private_link_id
  endpoint_service_id         = azurerm_private_endpoint.test.id
  private_endpoint_ip_address = azurerm_private_endpoint.test.private_service_connection.0.private_ip_address
  provider_name               = "AZURE"
}
1

There are 1 best solutions below

1
On BEST ANSWER

My problem is every time I do a terraform apply, the Mongo DB Atlasprivate link is getting destroyed and a new private link is getting created. Is there a way to resolve this

The mongodbatlas_privatelink_endpoint resource is still being recreated on every terraform apply, even without any changes. This could be due to changes in the state of the resources or the way Terraform detects changes.

You can verify the Terraform state to see if there are any changes detected in the mongodbatlas_privatelink_endpoint resource before running terraform apply using below commands.

terraform state list 
terraform state show mongodbatlas_privatelink_endpoint.test

As I don't have a subscription to create a MongoDB Atlas cluster for testing, I've used a storage account to check the terraform state show command.

enter image description here

Alternatively, you can also use the Terraform lifecycle configuration block to prevent the recreation of the mongodbatlas_privatelink_endpoint on every terraform apply.

resource "mongodbatlas_privatelink_endpoint_service" "test" {
  project_id                  = mongodbatlas_privatelink_endpoint.test.project_id
  private_link_id             = mongodbatlas_privatelink_endpoint.test.private_link_id
  endpoint_service_id         = azurerm_private_endpoint.test.id
  private_endpoint_ip_address = azurerm_private_endpoint.test.private_service_connection.0.private_ip_address
  provider_name               = "AZURE"
lifecycle {
    prevent_destroy = true
  }
}

Refer the Stack link related to same issue answered by Me