处理已经存在的【terraform】tfstate的情况
总结
以下是如何处理在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 的清单或参照通过 ① 创建的备份文件。
$ 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.
问题已解决!