<p>Run terraform plan first and use the names in the output.</p>
<p>Goal here is to import in the resources that are listed in the plan output so next time you run plan they won't be listed for add.</p>
<p>So aws_s3_bucket.machine-learning didn't exist in the plan output I bet. It could have been module.aws_s3_bucket.machine-learning or even module.aws_s3_bucket.machine-learning[0] depending on what your aws-assets.tf does.</p>

CC BY-SA 4.0
2021-06-02T13:39:58.503Z
This works! Thanks!
KC S
CC BY-SA 4.0
2022-01-05T11:19:48.963Z
Thats the best hack! :D
Tara Prasad Gurung
CC BY-SA 4.0
2022-02-16T03:29:44.157Z
Very helpful! Assuming you've correctly defined the resource/module (not necessarily an empty one), `terraform plan` shows the `ADDR` and, in some cases at least, the `ID` is also exposed (perhaps less directly). Use those as `terraform import ADDR ID` ... and you're manual changes are now part of your state! (But see _escaping_ hints for indices in the other answer.)
Brent Bradburn
CC BY-SA 4.0
2021-03-10T17:56:23.737Z
2021-03-10T17:56:23.737Z
Logan Waggoner
<p><a href="https://i.sstatic.net/QdcYr.png" rel="noreferrer"><img src="https://i.sstatic.net/QdcYr.png" alt="enter image description here"></a></p>

<p>main.tf</p>

<pre><code>module "assets" {
  source       = "./modules"
}
</code></pre>

<p>modules/aws-assests.tf</p>

<pre><code>resource "aws_s3_bucket" "machine-learning" {
  # (resource arguments)
}
</code></pre>

<p>To install the aws_s3_bucket module run the below command</p>

<pre><code># terraform init
</code></pre>

CC BY-SA 4.0
2020-04-17T05:42:18.140Z
Sorry, but this does not help anything. My config was that already(module was in ./modules/aws-assets but otherwise exactly the same.  It gives same error still:

❯ terraform import aws_s3_bucket.machine-learning machine-learning
Error: resource address "aws_s3_bucket.machine-learning" does not exist in the configuration.

Before importing this resource, please create its configuration in the root module. For example:

resource "aws_s3_bucket" "machine-learning" {
  # (resource arguments)
}
Bysmyyr
CC BY-SA 4.0
2020-04-17T12:04:17.247Z
I have tested it. I tried as above it is working perfectly for me.
Arvind Rawat
CC BY-SA 4.0
2022-02-15T22:33:04.110Z
This seems to be answering a totally different question: "How to create and initialize a skeleton project that uses a module". This answer does not address the question at hand: "How to `terraform import` a manually created resource defined as a module".
Brent Bradburn
CC BY-SA 4.0
2020-04-16T11:55:29.630Z
2020-04-16T11:55:29.630Z
Arvind Rawat
<p>The <code>terraform import</code> command uses <a href="https://www.terraform.io/docs/internals/resource-addressing.html" rel="noreferrer">the &quot;Resource Address&quot; syntax</a>, which is a way to talk about objects in a configuration from outside of that configuration. (This is as opposed to <a href="https://www.terraform.io/docs/configuration/expressions.html#references-to-named-values" rel="noreferrer">references</a> in the main Terraform language, which are always resolved in the context of a particular module.)</p>
<p>To refer to a resource that is declared in a child module, you can add a module path to the beginning of the address:</p>
<pre><code>terraform import module.assets.aws_s3_bucket.machine-learning BUCKET-NAME
</code></pre>
<hr />
<p>If you aren't sure which address to use for a particular resource instance, you can run <code>terraform plan</code> to see which instance address Terraform is proposing to <em>create</em>, and then use that same address with <code>terraform import</code> instead to tell Terraform to use the existing object.</p>
<p>For addresses that include sequences like <code>[0]</code> and <code>[&quot;foo&quot;]</code> to represent one of multiple instances of a module or resource, you'll need to use escaping or quoting to make sure your shell doesn't interpret those as its own metacharacters, and instead passes them on literally to Terraform:</p>
<ul>
<li><p>On Unix-style shells, use single quotes to make the inner address be taken literally:
<code>terraform import 'aws_instance.example[&quot;foo&quot;]'</code></p>
</li>
<li><p>On Windows, from the command interpreter <code>cmd.exe</code>, escape any quotes with a backslash:</p>
<p><code>terraform import &quot;aws_instance.example[\&quot;foo\&quot;]&quot;</code></p>
<p>(PowerShell's escaping rules for external programs are very awkward and so I would not suggest using PowerShell to run a command like this.)</p>
</li>
</ul>

CC BY-SA 4.0
2021-10-19T12:57:09.840Z
To expand on this if there are multiple modules calling each other you may have something like this: `terraform import module.my-bucket.module.my-generic-s3-bucket.aws_s3_bucket.this my-bucket-name`
Juraj Martinka
CC BY-SA 4.0
2022-02-11T15:54:14.150Z
I had to dig through my existing TF state in order to find the correct path, building it up to find the piece I was looking for. (new to terraform here, especially errors!)
ps2goat
CC BY-SA 4.0
2023-03-09T18:12:30.517Z
It's a lot easier to see resource addresses using `terraform state list` and `grep`
chb
CC BY-SA 4.0
2020-04-17T18:37:16.357Z
2022-02-11T17:44:46.753Z
2022-02-11T17:44:46.753Z
Martin Atkins
<p>Terraform v0.11.11</p>
<p>We have a quite big AWS setup created with Terraform. Different regions are applied separately, and that is why we have most of the things in modules which are imported like this:</p>
<pre><code>module &quot;assets&quot; {
  source       = &quot;../modules/aws-assets&quot;
  environment  = &quot;${var.environment}&quot;
  upload_users = &quot;${var.upload_users}&quot;
}
</code></pre>
<p>We have one s3 bucket created manually, and I try to import it. Terraform import command does not find the configuration file and errors.</p>
<pre><code>Error: resource address &quot;aws_s3_bucket.machine-learning&quot; does not exist in the configuration.
</code></pre>
<p>Before importing this resource, please create its configuration in the root module. For example:</p>
<pre><code>resource &quot;aws_s3_bucket&quot; &quot;machine-learning&quot; {
  # (resource arguments)
}
</code></pre>
<p>terraform apply/plan do find the configuration and wants to create it which is not what we want?</p>
<p>Thanks for you help!</p>
<p>================================</p>
<p>Edit:</p>
<p>now moved configuration to root and ended up with different error...</p>

<terraform>
How to use terraform import with module
Bysmyyr
# How to use terraform import with module

(Asked by: username_0 on 2020-04-16)

Terraform v0.11.11
We have a quite big AWS setup created with Terraform. Different regions are applied separately, and that is why we have most of the things in modules which are imported like this:
module "assets" {
  source       = "../modules/aws-assets"
  environment  = "${var.environment}"
  upload_users = "${var.upload_users}"
}

We have one s3 bucket created manually, and I try to import it. Terraform import command does not find the configuration file and errors.
Error: resource address "aws_s3_bucket.machine-learning" does not exist in the configuration.

Before importing this resource, please create its configuration in the root module. For example:
resource "aws_s3_bucket" "machine-learning" {
  # (resource arguments)
}

terraform apply/plan do find the configuration and wants to create it which is not what we want?
Thanks for you help!
================================
Edit:
now moved configuration to root and ended up with different error...

**Tags:** terraform


---

## 3 answers

### Answer by username_6 on 2020-04-17. Score: 50

The terraform import command uses the "Resource Address" syntax, which is a way to talk about objects in a configuration from outside of that configuration. (This is as opposed to references in the main Terraform language, which are always resolved in the context of a particular module.)
To refer to a resource that is declared in a child module, you can add a module path to the beginning of the address:
terraform import module.assets.aws_s3_bucket.machine-learning BUCKET-NAME


If you aren't sure which address to use for a particular resource instance, you can run terraform plan to see which instance address Terraform is proposing to create, and then use that same address with terraform import instead to tell Terraform to use the existing object.
For addresses that include sequences like [0] and ["foo"] to represent one of multiple instances of a module or resource, you'll need to use escaping or quoting to make sure your shell doesn't interpret those as its own metacharacters, and instead passes them on literally to Terraform:

On Unix-style shells, use single quotes to make the inner address be taken literally:
terraform import 'aws_instance.example["foo"]'

On Windows, from the command interpreter cmd.exe, escape any quotes with a backslash:
terraform import "aws_instance.example[\"foo\"]"
(PowerShell's escaping rules for external programs are very awkward and so I would not suggest using PowerShell to run a command like this.)

> **Comments:**
> **username_7**: To expand on this if there are multiple modules calling each other you may have something like this: `terraform import module.my-bucket.module.my-generic-s3-bucket.aws_s3_bucket.this my-bucket-name`  
> **username_8**: I had to dig through my existing TF state in order to find the correct path, building it up to find the piece I was looking for. (new to terraform here, especially errors!)  
> **username_9**: It's a lot easier to see resource addresses using `terraform state list` and `grep`  

---

### Answer by username_1 on 2021-03-10. Score: 8

Run terraform plan first and use the names in the output.

Goal here is to import in the resources that are listed in the plan output so next time you run plan they won't be listed for add.

So aws\_s3\_bucket.machine-learning didn't exist in the plan output I bet. It could have been module.aws\_s3\_bucket.machine-learning or even module.aws\_s3\_bucket.machine-learning[0] depending on what your aws-assets.tf does.

> **Comments:**
> **username_2**: This works! Thanks!  
> **username_3**: Thats the best hack! :D  
> **username_4**: Very helpful! Assuming you've correctly defined the resource/module (not necessarily an empty one), `terraform plan` shows the `ADDR` and, in some cases at least, the `ID` is also exposed (perhaps less directly). Use those as `terraform import ADDR ID` ... and you're manual changes are now part of your state! (But see _escaping_ hints for indices in the other answer.)  

---

### Answer by username_5 on 2020-04-16. Score: 4

main.tf
module "assets" {
  source       = "./modules"
}

modules/aws-assests.tf
resource "aws_s3_bucket" "machine-learning" {
  # (resource arguments)
}

To install the aws_s3_bucket module run the below command
# terraform init

> **Comments:**
> **username_0**: Sorry, but this does not help anything. My config was that already(module was in ./modules/aws-assets but otherwise exactly the same.  It gives same error still:  ❯ terraform import aws_s3_bucket.machine-learning machine-learning Error: resource address "aws_s3_bucket.machine-learning" does not exist in the configuration.  Before importing this resource, please create its configuration in the root module. For example:  resource "aws_s3_bucket" "machine-learning" {   # (resource arguments) }  
> **username_5**: I have tested it. I tried as above it is working perfectly for me.  
> **username_4**: This seems to be answering a totally different question: "How to create and initialize a skeleton project that uses a module". This answer does not address the question at hand: "How to `terraform import` a manually created resource defined as a module".  

---

