我有一个过去使用以下资源块构建的 EC2 实例资源:
resource "aws_instance" "this" {
...
}
现在我想将其迁移到我创建的模块块中。
我尝试用我的模块块替换资源块,但在 terraform 规划中,我看到它打算替换我的实例,但我不希望这样。
有什么方法可以切换到模块块而不用 terraform 重新创建我的资源?
您需要移动该州的资源地址。在不知道新地址的情况下(问题中不存在模块声明和计划),从根模块执行以下子命令将实现预期目标:
terraform state mv aws_instance.this module.my_module.aws_instance.this
更多信息可以在文档中找到。
或者,也可以将 moved block 与最新版本的 terraform 一起使用:
moved {
from = aws_instance.this
to = module.my_module.aws_instance.this
}