github.com/hugorut/terraform@v1.1.3/website/docs/cli/import/usage.mdx (about)

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