如何在资源配置中使用 terraform import
How to use terraform import with resource configuration
在导入 terraform 之前,我定义了:
# instance.tf
resource "aws_instance" "appserver" {
}
然后我 运行: terraform import aws_instance.appserver <instance-id>
很顺利,我可以使用 terraform show 看到导入的 ec2 资源。然而,对我来说神秘的是 "transfer" 这个现有的 terraform 状态进入 terraform 配置(instance.tf 上面)这样我就可以将它作为基础设施作为代码进行管理(或者至少是我的理解方式).我添加了 ami 和 instance_type 键及其相应的值,但每次我发出 terraform plan
,terraform 似乎想要 "replace" 我现有的实例。
1) 为什么 terraform 想要替换那个实例?
2) 我如何 "transfer" 将实例的 terraform 状态添加到配置中? (这可能吗?)
3) 对于经验丰富的 运行s,你们是如何在 terraform 中管理现有的 aws 基础设施的?
首先,terraform 想要替换您的实例,因为 terraform 在资源配置和当前现有实例之间没有执行您期望的'link'。
Terraform 官方文档:(https://www.terraform.io/docs/import/index.html)
The current implementation of Terraform import can only import
resources into the state. It does not generate configuration. A future
version of Terraform will also generate configuration.
Because of this, prior to running terraform import it is necessary to
write manually a resource configuration block for the resource, to
which the imported object will be mapped.
While this may seem tedious, it still gives Terraform users an avenue
for importing existing resources. A future version of Terraform will
fully generate configuration, significantly simplifying this process.
理解上面写的内容后,我会使用以下步骤:
首先,编写您的 Terraform 资源配置。应如下所示:
resource "aws_instance" "example" {
# ...instance configuration...
}
terraform import aws_instance.example i-abcd1234
以便将现有基础架构导入您的 状态 并将其附加到您在上面创建的资源配置。
在导入 terraform 之前,我定义了:
# instance.tf
resource "aws_instance" "appserver" {
}
然后我 运行: terraform import aws_instance.appserver <instance-id>
很顺利,我可以使用 terraform show 看到导入的 ec2 资源。然而,对我来说神秘的是 "transfer" 这个现有的 terraform 状态进入 terraform 配置(instance.tf 上面)这样我就可以将它作为基础设施作为代码进行管理(或者至少是我的理解方式).我添加了 ami 和 instance_type 键及其相应的值,但每次我发出 terraform plan
,terraform 似乎想要 "replace" 我现有的实例。
1) 为什么 terraform 想要替换那个实例?
2) 我如何 "transfer" 将实例的 terraform 状态添加到配置中? (这可能吗?)
3) 对于经验丰富的 运行s,你们是如何在 terraform 中管理现有的 aws 基础设施的?
首先,terraform 想要替换您的实例,因为 terraform 在资源配置和当前现有实例之间没有执行您期望的'link'。
Terraform 官方文档:(https://www.terraform.io/docs/import/index.html)
The current implementation of Terraform import can only import resources into the state. It does not generate configuration. A future version of Terraform will also generate configuration.
Because of this, prior to running terraform import it is necessary to write manually a resource configuration block for the resource, to which the imported object will be mapped.
While this may seem tedious, it still gives Terraform users an avenue for importing existing resources. A future version of Terraform will fully generate configuration, significantly simplifying this process.
理解上面写的内容后,我会使用以下步骤:
首先,编写您的 Terraform 资源配置。应如下所示:
resource "aws_instance" "example" { # ...instance configuration... }
terraform import aws_instance.example i-abcd1234
以便将现有基础架构导入您的 状态 并将其附加到您在上面创建的资源配置。