github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/website/docs/cli/commands/import.mdx (about)

     1  ---
     2  page_title: 'Command: import'
     3  description: The terraform import command brings existing resources into Terraform state.
     4  ---
     5  
     6  # Command: import
     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  The `terraform import` command [imports existing resources](/cli/import)
    11  into Terraform.
    12  
    13  ## Usage
    14  
    15  Usage: `terraform import [options] ADDRESS ID`
    16  
    17  Import will find the existing resource from ID and import it into your Terraform
    18  state at the given ADDRESS.
    19  
    20  ADDRESS must be a valid [resource address](/cli/state/resource-addressing).
    21  Because any resource address is valid, the import command can import resources
    22  into modules as well as directly into the root of your state.
    23  
    24  ID is dependent on the resource type being imported. For example, for AWS EC2
    25  instances it is the instance ID (`i-abcd1234`) but for AWS Route53 zones
    26  it is the zone ID (`Z12ABC4UGMOZ2N`). Please reference the provider documentation for details
    27  on the ID format. If you're unsure, feel free to just try an ID. If the ID
    28  is invalid, you'll just receive an error message.
    29  
    30  ~> Warning: Terraform expects that each remote object it is managing will be
    31  bound to only one resource address, which is normally guaranteed by Terraform
    32  itself having created all objects. If you import existing objects into Terraform,
    33  be careful to import each remote object to only one Terraform resource address.
    34  If you import the same object multiple times, Terraform may exhibit unwanted
    35  behavior. For more information on this assumption, see
    36  [the State section](/language/state).
    37  
    38  The command-line flags are all optional. The following flags are available:
    39  
    40  - `-config=path` - Path to directory of Terraform configuration files that
    41    configure the provider for import. This defaults to your working directory.
    42    If this directory contains no Terraform configuration files, the provider
    43    must be configured via manual input or environmental variables.
    44  
    45  - `-input=true` - Whether to ask for input for provider configuration.
    46  
    47  - `-lock=false` - Don't hold a state lock during the operation. This is
    48    dangerous if others might concurrently run commands against the same
    49    workspace.
    50  
    51  - `-lock-timeout=0s` - Duration to retry a state lock.
    52  
    53  - `-no-color` - If specified, output won't contain any color.
    54  
    55  - `-parallelism=n` - Limit the number of concurrent operation as Terraform
    56    [walks the graph](/internals/graph#walking-the-graph). Defaults
    57    to 10.
    58  
    59  - `-provider=provider` - **Deprecated** Override the provider configuration to
    60    use when importing the object. By default, Terraform uses the provider specified
    61    in the configuration for the target resource, and that is the best behavior in most cases.
    62  
    63  - `-var 'foo=bar'` - Set a variable in the Terraform configuration. This flag
    64    can be set multiple times. Variable values are interpreted as
    65    [literal expressions](/language/expressions/types) in the
    66    Terraform language, so list and map values can be specified via this flag.
    67  
    68  - `-var-file=foo` - Set variables in the Terraform configuration from
    69    a [variable file](/language/values/variables#variable-definitions-tfvars-files). If
    70    a `terraform.tfvars` or any `.auto.tfvars` files are present in the current
    71    directory, they will be automatically loaded. `terraform.tfvars` is loaded
    72    first and the `.auto.tfvars` files after in alphabetical order. Any files
    73    specified by `-var-file` override any values set automatically from files in
    74    the working directory. This flag can be used multiple times. This is only
    75    useful with the `-config` flag.
    76  
    77  For configurations using the [Terraform Cloud CLI integration](/cli/cloud) or the [`remote` backend](/language/settings/backends/remote)
    78  only, `terraform import`
    79  also accepts the option
    80  [`-ignore-remote-version`](/cli/cloud/command-line-arguments#ignore-remote-version).
    81  
    82  For configurations using
    83  [the `local` backend](/language/settings/backends/local) only,
    84  `terraform import` also accepts the legacy options
    85  [`-state`, `-state-out`, and `-backup`](/language/settings/backends/local#command-line-arguments).
    86  
    87  ## Provider Configuration
    88  
    89  Terraform will attempt to load configuration files that configure the
    90  provider being used for import. If no configuration files are present or
    91  no configuration for that specific provider is present, Terraform will
    92  prompt you for access credentials. You may also specify environmental variables
    93  to configure the provider.
    94  
    95  The only limitation Terraform has when reading the configuration files
    96  is that the import provider configurations must not depend on non-variable
    97  inputs. For example, a provider configuration cannot depend on a data
    98  source.
    99  
   100  As a working example, if you're importing AWS resources and you have a
   101  configuration file with the contents below, then Terraform will configure
   102  the AWS provider with this file.
   103  
   104  ```hcl
   105  variable "access_key" {}
   106  variable "secret_key" {}
   107  
   108  provider "aws" {
   109    access_key = "${var.access_key}"
   110    secret_key = "${var.secret_key}"
   111  }
   112  ```
   113  
   114  ## Example: Import into Resource
   115  
   116  This example will import an AWS instance into the `aws_instance` resource named `foo`:
   117  
   118  ```shell
   119  $ terraform import aws_instance.foo i-abcd1234
   120  ```
   121  
   122  ## Example: Import into Module
   123  
   124  The example below will import an AWS instance into the `aws_instance` resource named `bar` into a module named `foo`:
   125  
   126  ```shell
   127  $ terraform import module.foo.aws_instance.bar i-abcd1234
   128  ```
   129  
   130  ## Example: Import into Resource configured with count
   131  
   132  The example below will import an AWS instance into the first instance of the `aws_instance` resource named `baz` configured with
   133  [`count`](/language/meta-arguments/count):
   134  
   135  ```shell
   136  $ terraform import 'aws_instance.baz[0]' i-abcd1234
   137  ```
   138  
   139  ## Example: Import into Resource configured with for_each
   140  
   141  The example below will import an AWS instance into the `"example"` instance of the `aws_instance` resource named `baz` configured with
   142  [`for_each`](/language/meta-arguments/for_each):
   143  
   144  Linux, Mac OS, and UNIX:
   145  
   146  ```shell
   147  $ terraform import 'aws_instance.baz["example"]' i-abcd1234
   148  ```
   149  
   150  PowerShell:
   151  
   152  ```shell
   153  $ terraform import 'aws_instance.baz[\"example\"]' i-abcd1234
   154  ```
   155  
   156  Windows `cmd.exe`:
   157  
   158  ```shell
   159  $ terraform import aws_instance.baz[\"example\"] i-abcd1234
   160  ```