2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【terraform】tfstateが既に存在していた場合の対処

Posted at

概要

terraform import時に既に存在していると怒られた時の対処法を備忘録として記す

エラー

Error: Resource already managed by Terraform

Terraform is already managing a remote object for
module.network.aws_security_group.XXX. To import to this
address you must first remove the existing object from the state.

対処法

①バックアップを取る

対象を削除した後、問題が起きてからでは怖いのでバックアップを取得する

$ terraform state pull > tfstate.bk

②stateのlistを参照する or ①で作成したバックアップファイルを参照する

$ terraform state list

③対象の中身も確認する

$ terraform state show module.XXX.XXX

④stateに記載されている対象を削除

$ terraform state rm module.XXX.XXX

⑤import実行

$ terraform import module.XXX.XXX
module.network.aws_security_group.XXX: Importing from ID "sg-XXX"...
module.network.aws_security_group.XXX: Import prepared!
  Prepared aws_security_group for import
module.network.aws_security_group.XXX: Refreshing state... [id=sg-XXX]

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

以上で解決!

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?