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