Skip to main content

Troubleshooting

Ada beberapa kejadian disaat melakukan terraform apply kita mengalami beberapa Error, dan berikut saya kumpulkan error-error yang saya dapatkan:

ERROR 1:

Error: A resource with the ID "/subscriptions/cacd41b5-0500-4c1b-840b-13405b6e5b93/resourceGroups/rg-wid-research/providers/Microsoft.DBforPostgreSQL/flexibleServers/wid-postgres-db" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_postgresql_flexible_server" for more information.
│ 
│   with azurerm_postgresql_flexible_server.postgresql,
│   on postgresql.tf line 1, in resource "azurerm_postgresql_flexible_server" "postgresql":
│    1: resource "azurerm_postgresql_flexible_server" "postgresql" {

Solusi & Penjelasan

Error ini muncul karena resource PostgreSQL Flexible Server dengan nama wid-postgres-db sudah ada di Azure, tetapi belum tercatat di state file Terraform Anda.

Agar Terraform bisa mengelola resource ini, Anda harus import resource tersebut ke dalam state.

Jalankan perintah berikut di terminal Anda:

terraform import azurerm_postgresql_flexible_server.postgresql /subscriptions/cacd41b5-0500-4c1b-840b-13405b6e5b93/resourceGroups/rg-wid-research/providers/Microsoft.DBforPostgreSQL/flexibleServers/wid-postgres-db

Setelah Import

  1. Jalankan terraform plan untuk memastikan konfigurasi di file .tf sudah sesuai dengan resource yang ada di Azure.
  2. Jika ada perbedaan, sesuaikan file .tf atau resource di Azure agar konsisten.
  3. Setelah itu, Anda bisa melanjutkan terraform apply seperti biasa.

Catatan:

  • Jangan hapus resource langsung dari Azure Portal tanpa menghapusnya dari state Terraform, agar tidak terjadi drift.
  • Jika ingin menghapus resource, lakukan dengan terraform destroy setelah resource sudah ter-import ke state.