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