website/docs/language/import (#228)

Signed-off-by: Marcin Białoń <mbialon@spacelift.io>
This commit is contained in:
Marcin Białoń 2023-08-29 16:36:41 +02:00 committed by GitHub
parent 26dcb63d0f
commit 51bb449ac3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 49 deletions

View File

@ -1,28 +1,28 @@
---
page_title: Import - Generating Configuration
description: >-
Generate configuration and manage existing resources with Terraform using configuration-driven import.
Generate configuration and manage existing resources with OpenTF using configuration-driven import.
---
# Generating configuration
~> **Experimental:** Configuration generation is available in Terraform v1.5 as an experimental feature. Later minor versions may contain changes to the formatting of generated configuration and behavior of the `terraform plan` command using the `-generate-config-out` flag.
~> **Experimental:** Configuration generation is available in OpenTF v1.6 as an experimental feature. Later minor versions may contain changes to the formatting of generated configuration and behavior of the `opentf plan` command using the `-generate-config-out` flag.
Terraform can generate code for the resources you define in [`import` blocks](/terraform/language/import) that do not already exist in your configuration. Terraform produces HCL to act as a template that contains Terraform's best guess at the appropriate value for each resource argument.
OpenTF can generate code for the resources you define in [`import` blocks](/opentf/language/import) that do not already exist in your configuration. OpenTF produces HCL to act as a template that contains OpenTF's best guess at the appropriate value for each resource argument.
Starting with Terraform'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.
Starting with OpenTF'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 `terraform plan` with the `-generate-config-out` flag and supply a new file path. Do not supply a path to an existing file, or Terraform throws an error.
To generate configuration, run `opentf plan` with the `-generate-config-out` flag and supply a new file path. Do not supply a path to an existing file, or OpenTF throws an error.
```shell
$ terraform plan -generate-config-out=generated_resources.tf
$ opentf plan -generate-config-out=generated_resources.tf
```
If any resources targeted by an `import` block do not exist in your configuration, Terraform then generates and writes configuration for those resources in `generated_resources.tf`.
If any resources targeted by an `import` block do not exist in your configuration, OpenTF 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](/terraform/language/import#plan-and-apply-an-import), with the extra step of generating configuration during the planning stage. You can then review and modify the generated configuration before applying.
The workflow for generating configuration is similar to the [`import` block workflow](/opentf/language/import#plan-and-apply-an-import), 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
@ -35,24 +35,24 @@ import {
}
```
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, Terraform attempts to import into that resource. In future planning, Terraform knows it doesn't need to generate configuration for resources that already exist in your state.
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, OpenTF attempts to import into that resource. In future planning, OpenTF 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](/terraform/language/import#import-id).
The import block's `id` argument uses that resource's [import ID](/opentf/language/import#import-id).
If your configuration does not contain other resources for your selected provider, you must add a `provider` block to inform Terraform which provider it should use to generate configuration. Otherwise, Terraform 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 `terraform init` again.
If your configuration does not contain other resources for your selected provider, you must add a `provider` block to inform OpenTF which provider it should use to generate configuration. Otherwise, OpenTF 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 `opentf init` again.
### 2. Plan and generate configuration
To instruct Terraform to generate configuration for the `import` blocks you defined, run `terraform plan` with the `-generate-config-out=` flag and a new file path. Terraform displays its plan for importing your resource and the file where Terraform generated configuration based on this plan.
To instruct OpenTF to generate configuration for the `import` blocks you defined, run `opentf plan` with the `-generate-config-out=` flag and a new file path. OpenTF displays its plan for importing your resource and the file where OpenTF generated configuration based on this plan.
```shell
$ terraform plan -generate-config-out=generated.tf
$ opentf plan -generate-config-out=generated.tf
aws_iot_thing.bar: Preparing import... [id=foo]
aws_iot_thing.bar: Refreshing state... [id=foo]
Terraform will perform the following actions:
OpenTF will perform the following actions:
# aws_iot_thing.bar will be imported
# (config will be generated)
@ -75,12 +75,12 @@ Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Terraform has generated configuration and written it to generated.tf. Please review the configuration and edit it as necessary before adding it to version control.
OpenTF 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. Review generated configuration
The example above instructs Terraform to generate configuration in a file named `generated.tf`. The below code is an example of a `generated.tf` file.
The example above instructs OpenTF to generate configuration in a file named `generated.tf`. The below code is an example of a `generated.tf` file.
```hcl
resource aws_iot_thing "bar" {
@ -92,15 +92,15 @@ Review the generated configuration and update it as needed. You may wish to move
### 4. Apply
Run `terraform apply` to import your infrastructure.
Run `opentf apply` to import your infrastructure.
```shell
$ terraform apply
$ opentf apply
aws_iot_thing.bar: Preparing import... [id=foo]
aws_iot_thing.bar: Refreshing state... [id=foo]
Terraform will perform the following actions:
OpenTF will perform the following actions:
# aws_iot_thing.bar will be imported
resource "aws_iot_thing" "bar" {
@ -126,12 +126,12 @@ Commit your new resource configuration to your version control system.
### Conflicting resource arguments
Terraform generates configuration for importable resources during a plan by requesting values for resource attributes from the provider. For certain resources with complex schemas, Terraform may not be able to construct a valid configuration from these values.
OpenTF generates configuration for importable resources during a plan by requesting values for resource attributes from the provider. For certain resources with complex schemas, OpenTF may not be able to construct a valid configuration from these values.
Terraform will display an error like the one below if it does not receive values for resource attributes while generating configuration.
OpenTF will display an error like the one below if it does not receive values for resource attributes while generating configuration.
```shell
$ terraform plan -generate-config-out=generated.tf
$ opentf plan -generate-config-out=generated.tf
│ Error: Conflicting configuration arguments
@ -143,4 +143,4 @@ $ terraform plan -generate-config-out=generated.tf
```
In the example above, Terraform 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 `terraform plan` again to check that there are no further issues.
In the example above, OpenTF 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 `opentf plan` again to check that there are no further issues.

View File

@ -1,24 +1,22 @@
---
page_title: Import - Configuration Language
description: >-
Import and manage existing resources with Terraform using configuration-driven import.
Import and manage existing resources with OpenTF using configuration-driven import.
---
# Import
-> **Note:** Import blocks are only available in Terraform v1.5.0 and later.
~> **Experimental:** While we do not expect to make backwards-incompatible changes to syntax, the `-generate-config-out` flag and how OpenTF processes imports during the plan stage and generates configuration may change in future releases.
~> **Experimental:** While we do not expect to make backwards-incompatible changes to syntax, the `-generate-config-out` flag and how Terraform processes imports during the plan stage and generates configuration may change in future releases.
Use the `import` block to import existing infrastructure resources into OpenTF, bringing them under OpenTF's management. Unlike the `opentf import` command, configuration-driven import using `import` blocks is predictable, works with CICD pipelines, and lets you preview an import operation before modifying state.
Use the `import` block to import existing infrastructure resources into Terraform, bringing them under Terraform's management. Unlike the `terraform import` command, configuration-driven import using `import` blocks is predictable, works with CICD pipelines, and lets you preview an import operation before modifying state.
Once imported, OpenTF tracks the resource in your state file. You can then manage the imported resource like any other, updating its attributes and destroying it as part of a standard resource lifecycle.
Once imported, Terraform tracks the resource in your state file. You can then manage the imported resource like any other, updating its attributes and destroying it as part of a standard resource lifecycle.
The `import` block records that Terraform imported the resource and did not create it. After importing, you can optionally remove import blocks from your configuration or leave them as a record of the resource's origin.
The `import` block records that OpenTF imported the resource and did not create it. After importing, you can optionally remove import blocks from your configuration or leave them as a record of the resource's origin.
## Syntax
You can add an `import` block to any Terraform configuration file. A common pattern is to create an `imports.tf` file, or to place each `import` block beside the `resource` block it imports into.
You can add an `import` block to any OpenTF configuration file. A common pattern is to create an `imports.tf` file, or to place each `import` block beside the `resource` block it imports into.
```hcl
import {
@ -37,39 +35,37 @@ The above `import` block defines an import of the AWS instance with the ID "i-ab
The `import` block has the following arguments:
- `to` - The instance address this resource will have in your state file.
- `id` - A string with the [import ID](#import-id) of the resource.
- `provider` (optional) - An optional custom resource provider, see [The Resource provider Meta-Argument](/terraform/language/meta-arguments/resource-provider) for details.
- `provider` (optional) - An optional custom resource provider, see [The Resource provider Meta-Argument](/opentf/language/meta-arguments/resource-provider) for details.
If you do not set the `provider` argument, Terraform attempts to import from the default provider.
If you do not set the `provider` argument, OpenTF attempts to import from the default provider.
### Import ID
The import block requires you to provide the `id` argument with a literal string of your resource's import ID. Terraform needs this import ID to locate the resource you want to import.
The import block requires you to provide the `id` argument with a literal string of your resource's import ID. OpenTF needs this import ID to locate the resource you want to import.
The identifier you use for a resource's import ID is resource-specific. You can find the required ID in the [provider documentation](https://registry.terraform.io/browse/providers) for the resource you wish to import.
## Plan and apply an import
Terraform processes the `import` block during the plan stage. Once a plan is approved, Terraform imports the resource into its state during the subsequent apply stage.
OpenTF processes the `import` block during the plan stage. Once a plan is approved, OpenTF imports the resource into its state during the subsequent apply stage.
To import a resource using `import` blocks, you must:
1. Define an `import` block for the resource(s).
1. Add a corresponding `resource` block to your configuration , or [generate configuration](/terraform/language/import/generating-configuration) for that resource.
1. Run `terraform plan` to review how Terraform will import the resource(s).
1. Apply the configuration to import the resources and update your Terraform state.
> **Hands-on:** Try the [State Import](/terraform/tutorials/state/state-import?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial.
1. Add a corresponding `resource` block to your configuration , or [generate configuration](/opentf/language/import/generating-configuration) for that resource.
1. Run `opentf plan` to review how OpenTF will import the resource(s).
1. Apply the configuration to import the resources and update your OpenTF state.
The `import` block is [_idempotent_](https://en.wikipedia.org/wiki/Idempotence), meaning that applying an import action and running another plan will not generate another import action as long as that resource remains in your state.
Terraform only needs to import a given resource once. Attempting to import a resource into the same address again is a harmless no-op. You can remove `import` blocks after completing the import or safely leave them in your configuration as a record of the resource's origin for future module maintainers. For more information on maintaining configurations over time, see [Refactoring](/terraform/language/modules/develop/refactoring).
OpenTF only needs to import a given resource once. Attempting to import a resource into the same address again is a harmless no-op. You can remove `import` blocks after completing the import or safely leave them in your configuration as a record of the resource's origin for future module maintainers. For more information on maintaining configurations over time, see [Refactoring](/opentf/language/modules/develop/refactoring).
## Resource configuration
Before importing, you must add configuration for every resource you want Terraform to import. Otherwise, Terraform throws an error during planning, insisting you add resource configuration before it can successfully import. You can create resource configuration manually or [generate it using Terraform](/terraform/language/import/generating-configuration).
Before importing, you must add configuration for every resource you want OpenTF to import. Otherwise, OpenTF throws an error during planning, insisting you add resource configuration before it can successfully import. You can create resource configuration manually or [generate it using OpenTF](/opentf/language/import/generating-configuration).
We recommend writing a `resource` block if you know what most of the [resource's arguments](/terraform/language/resources/syntax#resource-arguments) will be. For example, your configuration may already contain a similar resource whose configuration you can copy and modify.
We recommend writing a `resource` block if you know what most of the [resource's arguments](/opentf/language/resources/syntax#resource-arguments) will be. For example, your configuration may already contain a similar resource whose configuration you can copy and modify.
We recommend [generating configuration](/terraform/language/import/generating-configuration) when importing multiple resources or a single complex resource that you do not already have the configuration for.
We recommend [generating configuration](/opentf/language/import/generating-configuration) when importing multiple resources or a single complex resource that you do not already have the configuration for.
### Add a `resource` block
@ -88,8 +84,8 @@ resource "aws_instance" "example" {
### Generate configuration
Terraform can generate HCL for resources that do not already exist in configuration.
For more details, see [Generating Configuration](/terraform/language/import/generating-configuration).
OpenTF can generate HCL for resources that do not already exist in configuration.
For more details, see [Generating Configuration](/opentf/language/import/generating-configuration).
## Examples
@ -102,7 +98,7 @@ import {
}
```
The below example shows how to import a resource that includes [`count`](/terraform/language/meta-arguments/count).
The below example shows how to import a resource that includes [`count`](/opentf/language/meta-arguments/count).
```hcl
import {
@ -112,7 +108,7 @@ import {
```
The below example shows how to import a resource that includes [`for_each`](/terraform/language/meta-arguments/for_each).
The below example shows how to import a resource that includes [`for_each`](/opentf/language/meta-arguments/for_each).
```hcl
import {
to = aws_instance.example["foo"]