opentofu/website/docs/cli/commands/import.mdx
Christian Mesh 3b3822d770
Improve documentation around static evaluation (#1843)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Signed-off-by: ollevche <ollevche@gmail.com>
Co-authored-by: ollevche <ollevche@gmail.com>
2024-07-23 09:31:50 -04:00

168 lines
6.4 KiB
Plaintext

---
description: The tofu import command brings existing resources into OpenTofu state.
---
# Command: import
The `tofu import` command [imports existing resources](../import/index.mdx)
into OpenTofu.
## Usage
Usage: `tofu import [options] ADDRESS ID`
Import will find the existing resource from ID and import it into your OpenTofu
state at the given ADDRESS.
ADDRESS must be a valid [resource address](../state/resource-addressing.mdx).
Because any resource address is valid, the import command can import resources
into modules as well as directly into the root of your state.
ID is dependent on the resource type being imported. For example, for AWS EC2
instances it is the instance ID (`i-abcd1234`) but for AWS Route53 zones
it is the zone ID (`Z12ABC4UGMOZ2N`). Please reference the provider documentation for details
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.
:::warning
OpenTofu expects that each remote object it is managing will be
bound to only one resource address, which is normally guaranteed by OpenTofu
itself having created all objects. If you import existing objects into OpenTofu,
be careful to import each remote object to only one OpenTofu resource address.
If you import the same object multiple times, OpenTofu may exhibit unwanted
behavior. For more information on this assumption, see
[the State section](../../language/state/index.mdx).
:::
:::note
Use of variables in [module sources](../../language/modules/sources.mdx#support-for-variable-and-local-evaluation),
[backend configuration](../../language/settings/backends/configuration.mdx#variables-and-locals),
or [encryption block](../../language/state/encryption.mdx#configuration)
requires [assigning values to root module variables](../../language/values/variables.mdx#assigning-values-to-root-module-variables)
when running `tofu import`.
:::
The command-line flags are all optional. The following flags are available:
- `-config=path` - Path to directory of OpenTofu configuration files that
configure the provider for import. This defaults to your working directory.
If this directory contains no OpenTofu configuration files, the provider
must be configured via manual input or environmental variables.
- `-input=true` - Whether to ask for input for provider configuration.
- `-lock=false` - Don't hold a state lock during the operation. This is
dangerous if others might concurrently run commands against the same
workspace.
- `-lock-timeout=0s` - Duration to retry a state lock.
- `-no-color` - If specified, output won't contain any color.
- `-parallelism=n` - Limit the number of concurrent operation as OpenTofu
[walks the graph](../../internals/graph.mdx#walking-the-graph). Defaults
to 10.
- `-provider=provider` - **Deprecated** Override the provider configuration to
use when importing the object. By default, OpenTofu uses the provider specified
in the configuration for the target resource, and that is the best behavior in most cases.
- `-var 'foo=bar'` - Set a variable in the OpenTofu configuration. This flag
can be set multiple times. Variable values are interpreted as
[literal expressions](../../language/expressions/types.mdx) in the
OpenTofu language, so list and map values can be specified via this flag.
- `-var-file=foo` - Set variables in the OpenTofu configuration from
a [variable file](../../language/values/variables.mdx#variable-definitions-tfvars-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.
For configurations using the [`cloud` backend](../../cli/cloud/index.mdx) or the [`remote` backend](../../language/settings/backends/remote.mdx)
only, `tofu import`
also accepts the option
[`-ignore-remote-version`](../../cli/cloud/command-line-arguments.mdx#ignore-remote-version).
For configurations using
[the `local` backend](../../language/settings/backends/local.mdx) only,
`tofu import` also accepts the legacy options
[`-state`, `-state-out`, and `-backup`](../../language/settings/backends/local.mdx#command-line-arguments).
## Provider Configuration
OpenTofu 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, OpenTofu will
prompt you for access credentials. You may also specify environmental variables
to configure the provider.
The only limitation OpenTofu 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 OpenTofu will configure
the AWS provider with this file.
```hcl
variable "access_key" {}
variable "secret_key" {}
provider "aws" {
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
}
```
## Example: Import into Resource
This example will import an AWS instance into the `aws_instance` resource named `foo`:
```shell
$ tofu import aws_instance.foo i-abcd1234
```
## Example: Import into Module
The example below will import an AWS instance into the `aws_instance` resource named `bar` into a module named `foo`:
```shell
$ tofu import module.foo.aws_instance.bar i-abcd1234
```
## Example: Import into Resource configured with count
The example below will import an AWS instance into the first instance of the `aws_instance` resource named `baz` configured with
[`count`](../../language/meta-arguments/count.mdx):
```shell
$ tofu import 'aws_instance.baz[0]' i-abcd1234
```
## Example: Import into Resource configured with for_each
The example below will import an AWS instance into the `"example"` instance of the `aws_instance` resource named `baz` configured with
[`for_each`](../../language/meta-arguments/for_each.mdx):
Linux, Mac OS, and UNIX:
```shell
$ tofu import 'aws_instance.baz["example"]' i-abcd1234
```
PowerShell:
```shell
$ tofu import 'aws_instance.baz[\"example\"]' i-abcd1234
```
Windows `cmd.exe`:
```shell
$ tofu import aws_instance.baz[\"example\"] i-abcd1234
```