github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/import/usage.html.md (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Import: Usage"
     4  sidebar_current: "docs-import-usage"
     5  description: |-
     6    The `terraform import` command is used to import existing infrastructure.
     7  ---
     8  
     9  # Import Usage
    10  
    11  The `terraform import` command is used to import existing infrastructure.
    12  
    13  The command currently can only import one resource at a time. This means
    14  you can't yet point Terraform import to an entire collection of resources
    15  such as an AWS VPC and import all of it. This workflow will be improved in a
    16  future version of Terraform.
    17  
    18  To import a resource, first write a resource block for it in your
    19  configuration, establishing the name by which it will be known to Terraform:
    20  
    21  ```
    22  resource "aws_instance" "example" {
    23    # ...instance configuration...
    24  }
    25  ```
    26  
    27  The name "example" here is local to the module where it is declared and is
    28  chosen by the configuration author. This is distinct from any ID issued by
    29  the remote system, which may change over time while the resource name
    30  remains constant.
    31  
    32  If desired, you can leave the body of the resource block blank for now and
    33  return to fill it in once the instance is imported.
    34  
    35  Now `terraform import` can be run to attach an existing instance to this
    36  resource configuration:
    37  
    38  ```shell
    39  $ terraform import aws_instance.example i-abcd1234
    40  ```
    41  
    42  This command locates the AWS instance with ID `i-abcd1234`. Then it attaches
    43  the existing settings of the instance, as described by the EC2 API, to the
    44  name `aws_instance.example` of a module. In this example the module path
    45  implies that the root module is used. Finally, the mapping is saved in the
    46  Terraform state.
    47  
    48  It is also possible to import to resources in child modules, using their paths,
    49  and to single instances of a resource with `count` or `for_each` set. See
    50  [_Resource Addressing_](/docs/internals/resource-addressing.html) for more
    51  details on how to specify a target resource.
    52  
    53  The syntax of the given ID is dependent on the resource type being imported.
    54  For example, AWS instances use an opaque ID issued by the EC2 API, but
    55  AWS Route53 Zones use the domain name itself. Consult the documentation for
    56  each importable resource for details on what form of ID is required.
    57  
    58  As a result of the above command, the resource is recorded in the state file.
    59  You can now run `terraform plan` to see how the configuration compares to
    60  the imported resource, and make any adjustments to the configuration to
    61  align with the current (or desired) state of the imported object.
    62  
    63  ## Complex Imports
    64  
    65  The above import is considered a "simple import": one resource is imported
    66  into the state file. An import may also result in a "complex import" where
    67  multiple resources are imported. For example, an AWS security group imports
    68  an `aws_security_group` but also one `aws_security_group_rule` for each rule.
    69  
    70  In this scenario, the secondary resources will not already exist in
    71  configuration, so it is necessary to consult the import output and create
    72  a `resource` block in configuration for each secondary resource. If this is
    73  not done, Terraform will plan to destroy the imported objects on the next run.
    74  
    75  If you want to rename or otherwise move the imported resources, the
    76  [state management commands](/docs/commands/state/index.html) can be used.