github.com/hugorut/terraform@v1.1.3/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
    80  [the `remote` backend](/language/settings/backends/remote)
    81  only, `terraform import`
    82  also accepts the option
    83  [`-ignore-remote-version`](/language/settings/backends/remote#command-line-arguments).
    84  
    85  For configurations using
    86  [the `local` backend](/language/settings/backends/local) only,
    87  `terraform import` also accepts the legacy options
    88  [`-state`, `-state-out`, and `-backup`](/language/settings/backends/local#command-line-arguments).
    89  
    90  ## Provider Configuration
    91  
    92  Terraform will attempt to load configuration files that configure the
    93  provider being used for import. If no configuration files are present or
    94  no configuration for that specific provider is present, Terraform will
    95  prompt you for access credentials. You may also specify environmental variables
    96  to configure the provider.
    97  
    98  The only limitation Terraform has when reading the configuration files
    99  is that the import provider configurations must not depend on non-variable
   100  inputs. For example, a provider configuration cannot depend on a data
   101  source.
   102  
   103  As a working example, if you're importing AWS resources and you have a
   104  configuration file with the contents below, then Terraform will configure
   105  the AWS provider with this file.
   106  
   107  ```hcl
   108  variable "access_key" {}
   109  variable "secret_key" {}
   110  
   111  provider "aws" {
   112    access_key = "${var.access_key}"
   113    secret_key = "${var.secret_key}"
   114  }
   115  ```
   116  
   117  ## Example: Import into Resource
   118  
   119  This example will import an AWS instance into the `aws_instance` resource named `foo`:
   120  
   121  ```shell
   122  $ terraform import aws_instance.foo i-abcd1234
   123  ```
   124  
   125  ## Example: Import into Module
   126  
   127  The example below will import an AWS instance into the `aws_instance` resource named `bar` into a module named `foo`:
   128  
   129  ```shell
   130  $ terraform import module.foo.aws_instance.bar i-abcd1234
   131  ```
   132  
   133  ## Example: Import into Resource configured with count
   134  
   135  The example below will import an AWS instance into the first instance of the `aws_instance` resource named `baz` configured with
   136  [`count`](/language/meta-arguments/count):
   137  
   138  ```shell
   139  $ terraform import 'aws_instance.baz[0]' i-abcd1234
   140  ```
   141  
   142  ## Example: Import into Resource configured with for_each
   143  
   144  The example below will import an AWS instance into the `"example"` instance of the `aws_instance` resource named `baz` configured with
   145  [`for_each`](/language/meta-arguments/for_each):
   146  
   147  Linux, Mac OS, and UNIX:
   148  
   149  ```shell
   150  $ terraform import 'aws_instance.baz["example"]' i-abcd1234
   151  ```
   152  
   153  PowerShell:
   154  
   155  ```shell
   156  $ terraform import 'aws_instance.baz[\"example\"]' i-abcd1234
   157  ```
   158  
   159  Windows `cmd.exe`:
   160  
   161  ```shell
   162  $ terraform import aws_instance.baz[\"example\"] i-abcd1234
   163  ```