跳至主要内容

生成配置

OpenTofu 可以为在 import 中定义但尚未存在于您的配置中的资源生成代码。OpenTofu 生成 HCL 作为模板,其中包含 OpenTofu 对每个资源参数的适当值的最佳猜测。

从 OpenTofu 生成的 HCL 开始,我们建议您进行迭代,通过删除一些属性、调整其他属性的值以及根据需要将 resource 块重新排列到文件和模块中,来找到您的理想配置。

要生成配置,请使用 -generate-config-out 标志运行 tofu plan 并提供新的文件路径。请勿提供现有文件的路径,否则 OpenTofu 会抛出错误。

代码块
$ tofu plan -generate-config-out=generated_resources.tf

如果 import 块目标的任何资源在您的配置中不存在,则 OpenTofu 会生成并写入这些资源的配置到 generated_resources.tf 中。

工作流程

生成配置的工作流程类似于 import 块工作流程,但在计划阶段增加了生成配置的步骤。然后,您可以在应用之前查看和修改生成的配置。

1. 添加 import

import 块添加到您的配置中。此 import 块可以位于单独的文件(例如,import.tf)或现有的配置文件中。

代码块
import {
to = aws_iot_thing.bar
id = "foo"
}

import 块的 to 参数指向资源在您的状态文件中将具有的地址。如果您的状态中的资源地址与 import 块的 to 参数匹配,则 OpenTofu 会尝试导入到该资源中。在未来的计划中,OpenTofu 知道它不需要为已存在于您的状态中的资源生成配置。

import 块的 id 参数使用该资源的 导入 ID

如果您的配置不包含所选提供程序的其他资源,则必须添加一个 provider 块来告知 OpenTofu 应使用哪个提供程序生成配置。否则,如果无法确定要使用哪个提供程序,OpenTofu 将显示错误。如果向您的配置添加新的 provider 块,则必须再次运行 tofu init

2. 计划和生成配置

要指示 OpenTofu 为您定义的 import 块生成配置,请使用 -generate-config-out= 标志和新的文件路径运行 tofu plan。OpenTofu 将显示导入资源的计划以及 OpenTofu 根据此计划生成配置的文件。

代码块
$ tofu plan -generate-config-out=generated.tf

aws_iot_thing.bar: Preparing import... [id=foo]
aws_iot_thing.bar: Refreshing state... [id=foo]

OpenTofu will perform the following actions:

# aws_iot_thing.bar will be imported
# (config will be generated)
resource "aws_iot_thing" "bar" {
arn = "arn:aws:iot:eu-west-1:1234567890:thing/foo"
attributes = {}
default_client_id = "foo"
id = "foo"
name = "foo"
version = 1
}

Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.


│ Warning: Config generation is experimental

│ Generating configuration during import is currently experimental, and the generated configuration format may change in future versions.


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

OpenTofu has generated configuration and written it to generated.tf. Please review the configuration and edit it as necessary before adding it to version control.

3. 查看生成的配置

上面的示例指示 OpenTofu 将配置生成到名为 generated.tf 的文件中。以下代码是 generated.tf 文件的示例。

代码块
resource aws_iot_thing "bar" {
name = "foo"
}

查看生成的配置并根据需要进行更新。您可能希望将生成的配置移动到另一个文件,添加或删除资源参数,或将其更新为引用配置中的输入变量或其他资源。

4. 应用

运行 tofu apply 以导入您的基础设施。

代码块
$ tofu apply

aws_iot_thing.bar: Preparing import... [id=foo]
aws_iot_thing.bar: Refreshing state... [id=foo]

OpenTofu will perform the following actions:

# aws_iot_thing.bar will be imported
resource "aws_iot_thing" "bar" {
arn = "arn:aws:iot:eu-west-1:1234567890:thing/foo"
attributes = {}
default_client_id = "foo"
id = "foo"
name = "foo"
version = 1
}

Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.
aws_iot_thing.bar: Importing... [id=foo]
aws_iot_thing.bar: Import complete [id=foo]

Apply complete! Resources: 1 imported, 0 added, 0 changed, 0 destroyed.

将新的资源配置提交到您的版本控制系统。

限制

冲突的资源参数

OpenTofu 通过请求提供程序的资源属性值,在计划期间为可导入的资源生成配置。对于某些具有复杂架构的资源,OpenTofu 可能无法从这些值构建有效的配置。

如果在生成配置时未收到资源属性的值,OpenTofu 将显示如下所示的错误。

代码块
$ tofu plan -generate-config-out=generated.tf

│ Error: Conflicting configuration arguments

│ with aws_instance.ubuntu,
│ on g.tf line 20, in resource "aws_instance" "ubuntu":
│ 20: ipv6_address_count = 0

│ "ipv6_address_count": conflicts with ipv6_addresses

在上面的示例中,OpenTofu 仍然生成配置并将其写入 generated.tf。此错误源于 ipv6_address_countipv6_addresses 参数之间的冲突。资源支持这两个参数,但在配置资源时,您必须只选择其中一个。您可以通过删除这两个参数之一来修复此错误,然后再次运行 tofu plan 以检查是否存在其他问题。