- The OpenTofu Language
- Import
- Generating configuration
Generating configuration
Configuration generation is available in OpenTofu v1.6 as an experimental feature. Later minor versions may contain changes to the formatting of generated configuration and behavior of the tofu plan
command using the -generate-config-out
flag.
OpenTofu can generate code for the resources you define in import
blocks that do not already exist in your configuration. OpenTofu produces HCL to act as a template that contains OpenTofu's best guess at the appropriate value for each resource argument.
Starting with OpenTofu's generated HCL, we recommend iterating to find your ideal configuration by removing some attributes, adjusting the value of others, and rearranging resource
blocks into files and modules as appropriate.
To generate configuration, run tofu plan
with the -generate-config-out
flag and supply a new file path. Do not supply a path to an existing file, or OpenTofu throws an error.
If any resources targeted by an import
block do not exist in your configuration, OpenTofu then generates and writes configuration for those resources in generated_resources.tf
.
Workflow
The workflow for generating configuration is similar to the import
block workflow, with the extra step of generating configuration during the planning stage. You can then review and modify the generated configuration before applying.
1. Add the import
block
Add an import
block to your configuration. This import
block can be in a separate file (e.g., import.tf
) or an existing configuration file.
The import block's to
argument points to the address a resource
will have in your state file. If a resource address in your state matches an import
block's to
argument, OpenTofu attempts to import into that resource. In future planning, OpenTofu knows it doesn't need to generate configuration for resources that already exist in your state.
The import block's id
argument uses that resource's import ID.
If your configuration does not contain other resources for your selected provider, you must add a provider
block to inform OpenTofu which provider it should use to generate configuration. Otherwise, OpenTofu displays an error if it can not determine which provider to use.
If you add a new provider
block to your configuration, you must run tofu init
again.
2. Plan and generate configuration
To instruct OpenTofu to generate configuration for the import
blocks you defined, run tofu plan
with the -generate-config-out=
flag and a new file path. OpenTofu displays its plan for importing your resource and the file where OpenTofu generated configuration based on this plan.
3. Review generated configuration
The example above instructs OpenTofu to generate configuration in a file named generated.tf
. The below code is an example of a generated.tf
file.
Review the generated configuration and update it as needed. You may wish to move the generated configuration to another file, add or remove resource arguments, or update it to reference input variables or other resources in your configuration.
4. Apply
Run tofu apply
to import your infrastructure.
Commit your new resource configuration to your version control system.
Limitations
Conflicting resource arguments
OpenTofu generates configuration for importable resources during a plan by requesting values for resource attributes from the provider. For certain resources with complex schemas, OpenTofu may not be able to construct a valid configuration from these values.
OpenTofu will display an error like the one below if it does not receive values for resource attributes while generating configuration.
In the example above, OpenTofu still generates configuration and writes it to generated.tf
. This error stems from a conflict between the ipv6_address_count
and ipv6_addresses
arguments. The resource supports both of these arguments, but you must choose only one when configuring the resource. You could fix the error by removing one of these two arguments, then running tofu plan
again to check that there are no further issues.