Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Rose M Koron <32436232+rkoron007@users.noreply.github.com>
  • Loading branch information
kmoe and rkoron007 committed Jun 5, 2023
1 parent 62d3aa9 commit def5d5f
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions website/docs/language/import/generating-configuration.mdx
@@ -1,28 +1,30 @@
---
page_title: Import - Generating configuration
page_title: Import - Generating Configuration
description: >-
Import resources into Terraform to bring them under Terraform's management by writing `import` blocks in code.
Generate configuration and manage existing resources with Terraform using configuration-driven import.
---

# Import: Generating configuration
# Generating Configuration

-> **Note:** Config generation is available in Terraform v1.5 as an experimental feature. Later minor versions may contain changes to the format of generated configuration and behaviour of the `terraform plan` command when the `-generate-config-out` flag is supplied.
~> **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.

Terraform can generate HCL for resources which do not already exist in configuration. The HCL produced by Terraform is intended to act as a template, and contains Terraform's best guess at the appropriate value for each resource argument. You may find that you arrive at an ideal configuration by starting from the HCL generated by Terraform, removing some attributes, adjusting the value of others, and rearranging `resource` blocks into files and modules as appropriate.
Terraform can generate HCL for resources 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.

To instruct Terraform to generate configuration during the plan, supply a file path with `-generate-config-out`. The file should not already exist. For example:
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.

To generate configuration, use `terraform plan` with the `-generate-config-out` flag and supply a new file path. Do not supply a path to an existing file, this command creates a new one.

```
terraform plan -generate-config-out=generated_resources.tf
```

Terraform will generate configuration for any resources targeted by an `import` block that do not already exist in configuration, and write the HCL to `generated_resources.tf`.
If any resources targeted by an [`import` block](/terraform/language/import/index) do not exist in your configuration, Terraform generates and writes configuration for those resources in `generated_resources.tf`.

## Workflow

Terraform generates configuration during the plan and writes the new HCL file to disk after the plan has completed.
The workflow for generating configuration is similar to the [`import` block workflow](/terraform/language/import/index#planning-and-applying-an-import), with the extra step of generating configuration during the planning stage. You can then review and tweak the file Terraform generates before applying your configuration.

### 1. Add the `import` block to configuration
### 1. Add the `import` Block

```hcl
provider "aws" {
Expand All @@ -35,12 +37,11 @@ import {
}
```

### 2. Plan the import
Run `terraform plan -generate-config-out=generated.tf`. You should see output like the following:

### 2. Plan and Generate Configuration
Next, you will run the `terraform plan` command with the `-generate-config-out=` flag and a file path.

```
$ terraform plan
$ terraform plan -generate-config-out=generated.tf
aws_iot_thing.bar: Preparing import... [id=foo]
aws_iot_thing.bar: Refreshing state... [id=foo]
Expand All @@ -60,20 +61,20 @@ Terraform will perform the following actions:
Plan: 1 to import, 0 to add, 0 to change, 0 to destroy.
```

### 3. Review the generated code
### 3. Review Generated Configuration

Open `generated.tf` in your editor and review the generated code. It should look like this:
In our example, we specified that Terraform should generate configuration in a file named `generated.tf`. The below code demonstrates what the `generated.tf` file might look like.

```hcl
resource aws_iot_thing "bar" {
name = "foo"
}
```

Add or remove resource arguments as desired. Now is an appropriate time to add variables or references to other resources.
You can review the code Terraform generated and add or remove resource arguments, variables, and references to other resources as needed for your configuration.

### 4. Apply the import
Run `terraform apply`. You should see output like the following:
### 4. Apply
Running `terraform apply` create and provisions your infrastructure. Ensure you add your generated code alongside your Terraform code to your version control system.

```
$ terraform apply
Expand Down Expand Up @@ -105,7 +106,9 @@ Make sure that your generated code, along with your other Terraform code, is add

## Limitations

Terraform generates configuration for importable resources during the plan by requesting values for resource attributes from the provider. For certain resources with complex schemas, it may not be possible for Terraform to construct a valid configuration from these values. In this case, you may see an error like the following when planning:
Terraform generates configuration for importable resources during a plan by requesting values for resource attributes from the provider. For certain resources with complex schemas, it may not be possible for Terraform to construct a valid configuration from these values.

Terraform will throw an error like the one below if it does not receive values for resource attributes while generating configuration.

```
$ terraform plan -generate-config-out=generated.tf
Expand All @@ -120,4 +123,4 @@ $ terraform plan -generate-config-out=generated.tf
```

In this case, Terraform still generates configuration and writes it to `generated.tf`. You will probably want to remove one or more of the `ipv6_address_count` and `ipv6_addresses` arguments.
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. You could fix the error by removing one of these two arguments.

0 comments on commit def5d5f

Please sign in to comment.