2016-05-18 20:56:51 +02:00
|
|
|
---
|
|
|
|
layout: "docs"
|
|
|
|
page_title: "Command: import"
|
|
|
|
sidebar_current: "docs-commands-import"
|
|
|
|
description: |-
|
|
|
|
The `terraform import` command is used to import existing resources into Terraform.
|
|
|
|
---
|
|
|
|
|
|
|
|
# Command: import
|
|
|
|
|
2020-10-02 20:02:59 +02:00
|
|
|
> **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.
|
2020-07-31 22:16:35 +02:00
|
|
|
|
2016-05-18 20:56:51 +02:00
|
|
|
The `terraform import` command is used to
|
|
|
|
[import existing resources](/docs/import/index.html)
|
|
|
|
into Terraform.
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
Usage: `terraform import [options] ADDRESS ID`
|
|
|
|
|
|
|
|
Import will find the existing resource from ID and import it into your Terraform
|
|
|
|
state at the given ADDRESS.
|
|
|
|
|
|
|
|
ADDRESS must be a valid [resource address](/docs/internals/resource-addressing.html).
|
|
|
|
Because any resource address is valid, the import command can import resources
|
2020-04-10 14:54:19 +02:00
|
|
|
into modules as well as directly into the root of your state.
|
2016-05-18 20:56:51 +02:00
|
|
|
|
|
|
|
ID is dependent on the resource type being imported. For example, for AWS
|
|
|
|
instances it is the instance ID (`i-abcd1234`) but for AWS Route53 zones
|
2016-10-09 18:29:38 +02:00
|
|
|
it is the zone ID (`Z12ABC4UGMOZ2N`). Please reference the provider documentation for details
|
2016-05-18 20:56:51 +02:00
|
|
|
on the ID format. If you're unsure, feel free to just try an ID. If the ID
|
|
|
|
is invalid, you'll just receive an error message.
|
|
|
|
|
2020-08-01 00:22:50 +02:00
|
|
|
~> Warning: Terraform expects that each remote object it is managing will be
|
|
|
|
bound to only one resource address, which is normally guaranteed by Terraform
|
|
|
|
itself having created all objects. If you import existing objects into Terraform,
|
|
|
|
be careful to import each remote object to only one Terraform resource address.
|
|
|
|
If you import the same object multiple times, Terraform may exhibit unwanted
|
|
|
|
behavior. For more information on this assumption, see
|
|
|
|
[the State section](/docs/state/).
|
|
|
|
|
2016-05-18 20:56:51 +02:00
|
|
|
The command-line flags are all optional. The list of available flags are:
|
|
|
|
|
|
|
|
* `-backup=path` - Path to backup the existing state file. Defaults to
|
|
|
|
the `-state-out` path with the ".backup" extension. Set to "-" to disable
|
|
|
|
backups.
|
|
|
|
|
2016-11-02 19:11:42 +01:00
|
|
|
* `-config=path` - Path to directory of Terraform configuration files that
|
|
|
|
configure the provider for import. This defaults to your working directory.
|
|
|
|
If this directory contains no Terraform configuration files, the provider
|
|
|
|
must be configured via manual input or environmental variables.
|
|
|
|
|
2016-05-18 20:56:51 +02:00
|
|
|
* `-input=true` - Whether to ask for input for provider configuration.
|
|
|
|
|
2017-04-04 19:48:59 +02:00
|
|
|
* `-lock=true` - Lock the state file when locking is supported.
|
2016-05-18 20:56:51 +02:00
|
|
|
|
2017-04-04 19:48:59 +02:00
|
|
|
* `-lock-timeout=0s` - Duration to retry a state lock.
|
|
|
|
|
|
|
|
* `-no-color` - If specified, output won't contain any color.
|
2016-05-18 20:56:51 +02:00
|
|
|
|
2019-03-06 15:25:36 +01:00
|
|
|
* `-parallelism=n` - Limit the number of concurrent operation as Terraform
|
|
|
|
[walks the graph](/docs/internals/graph.html#walking-the-graph). Defaults
|
|
|
|
to 10.
|
|
|
|
|
2019-09-20 16:02:42 +02:00
|
|
|
* `-provider=provider` - **Deprecated** Override the provider configuration to
|
|
|
|
use when importing the object. By default, Terraform uses the provider specified
|
|
|
|
in the configuration for the target resource, and that is the best behavior in most cases.
|
2016-11-23 10:44:52 +01:00
|
|
|
|
2017-08-09 20:01:45 +02:00
|
|
|
* `-state=path` - Path to the source state file to read from. Defaults to the
|
|
|
|
configured backend, or "terraform.tfstate".
|
2017-04-04 19:48:59 +02:00
|
|
|
|
2017-08-09 20:01:45 +02:00
|
|
|
* `-state-out=path` - Path to the destination state file to write to. If this
|
|
|
|
isn't specified the source state file will be used. This can be a new or
|
|
|
|
existing path.
|
2017-04-04 19:48:59 +02:00
|
|
|
|
2017-01-24 22:01:23 +01:00
|
|
|
* `-var 'foo=bar'` - Set a variable in the Terraform configuration. This flag
|
|
|
|
can be set multiple times. Variable values are interpreted as
|
|
|
|
[HCL](/docs/configuration/syntax.html#HCL), so list and map values can be
|
|
|
|
specified via this flag. This is only useful with the `-config` flag.
|
|
|
|
|
|
|
|
* `-var-file=foo` - Set variables in the Terraform configuration from
|
2017-06-22 03:22:07 +02:00
|
|
|
a [variable file](/docs/configuration/variables.html#variable-files). If
|
|
|
|
a `terraform.tfvars` or any `.auto.tfvars` files are present in the current
|
|
|
|
directory, they will be automatically loaded. `terraform.tfvars` is loaded
|
|
|
|
first and the `.auto.tfvars` files after in alphabetical order. Any files
|
|
|
|
specified by `-var-file` override any values set automatically from files in
|
|
|
|
the working directory. This flag can be used multiple times. This is only
|
|
|
|
useful with the `-config` flag.
|
2017-01-24 22:01:23 +01:00
|
|
|
|
2016-05-18 20:56:51 +02:00
|
|
|
## Provider Configuration
|
|
|
|
|
2016-11-02 19:11:42 +01:00
|
|
|
Terraform will attempt to load configuration files that configure the
|
|
|
|
provider being used for import. If no configuration files are present or
|
|
|
|
no configuration for that specific provider is present, Terraform will
|
|
|
|
prompt you for access credentials. You may also specify environmental variables
|
|
|
|
to configure the provider.
|
|
|
|
|
|
|
|
The only limitation Terraform has when reading the configuration files
|
|
|
|
is that the import provider configurations must not depend on non-variable
|
|
|
|
inputs. For example, a provider configuration cannot depend on a data
|
|
|
|
source.
|
|
|
|
|
|
|
|
As a working example, if you're importing AWS resources and you have a
|
|
|
|
configuration file with the contents below, then Terraform will configure
|
|
|
|
the AWS provider with this file.
|
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```hcl
|
2016-11-02 19:11:42 +01:00
|
|
|
variable "access_key" {}
|
|
|
|
variable "secret_key" {}
|
|
|
|
|
|
|
|
provider "aws" {
|
|
|
|
access_key = "${var.access_key}"
|
|
|
|
secret_key = "${var.secret_key}"
|
|
|
|
}
|
|
|
|
```
|
2016-05-18 20:56:51 +02:00
|
|
|
|
2019-08-03 01:36:24 +02:00
|
|
|
## Example: Import into Resource
|
2016-05-18 20:56:51 +02:00
|
|
|
|
2019-08-03 01:36:24 +02:00
|
|
|
This example will import an AWS instance into the `aws_instance` resource named `foo`:
|
2016-05-18 20:56:51 +02:00
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```shell
|
2016-05-18 20:56:51 +02:00
|
|
|
$ terraform import aws_instance.foo i-abcd1234
|
|
|
|
```
|
|
|
|
|
2019-08-03 01:36:24 +02:00
|
|
|
## Example: Import into Module
|
2016-05-18 20:56:51 +02:00
|
|
|
|
2019-08-03 01:36:24 +02:00
|
|
|
The example below will import an AWS instance into the `aws_instance` resource named `bar` into a module named `foo`:
|
2016-05-18 20:56:51 +02:00
|
|
|
|
2017-04-05 17:29:27 +02:00
|
|
|
```shell
|
2016-05-18 20:56:51 +02:00
|
|
|
$ terraform import module.foo.aws_instance.bar i-abcd1234
|
|
|
|
```
|
2019-08-03 01:36:24 +02:00
|
|
|
|
2019-08-06 03:50:17 +02:00
|
|
|
## Example: Import into Resource configured with count
|
2019-08-03 01:36:24 +02:00
|
|
|
|
2019-08-06 03:50:17 +02:00
|
|
|
The example below will import an AWS instance into the first instance of the `aws_instance` resource named `baz` configured with
|
2020-11-13 03:30:52 +01:00
|
|
|
[`count`](/docs/configuration/meta-arguments/count.html):
|
2019-08-03 01:36:24 +02:00
|
|
|
|
|
|
|
```shell
|
|
|
|
$ terraform import 'aws_instance.baz[0]' i-abcd1234
|
|
|
|
```
|
|
|
|
|
2019-08-06 03:50:17 +02:00
|
|
|
## Example: Import into Resource configured with for_each
|
2019-08-03 01:36:24 +02:00
|
|
|
|
2019-08-06 03:50:17 +02:00
|
|
|
The example below will import an AWS instance into the `"example"` instance of the `aws_instance` resource named `baz` configured with
|
2020-11-13 03:30:52 +01:00
|
|
|
[`for_each`](/docs/configuration/meta-arguments/for_each.html):
|
2019-08-03 01:36:24 +02:00
|
|
|
|
|
|
|
Linux, Mac OS, and UNIX:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
$ terraform import 'aws_instance.baz["example"]' i-abcd1234
|
|
|
|
```
|
|
|
|
|
|
|
|
PowerShell:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
$ terraform import 'aws_instance.baz[\"example\"]' i-abcd1234
|
|
|
|
```
|
|
|
|
|
|
|
|
Windows `cmd.exe`:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
$ terraform import aws_instance.baz[\"example\"] i-abcd1234
|
|
|
|
```
|