github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/website/docs/cli/commands/import.html.md (about)

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