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