20

I have an existing resource which i'd like to import into my config, to keep with style I have written this into a module, as below.

When I try to import it (using terraform import resource.module id) I get an error, as below >Before importing this resource, please create its configuration in the root module.

If I add this to my root, it errors on a dupe resource.

How do I go about importing a resource which is not in my root module?

Current structure: #modules.tf

module my_module {
   source ./modules
   ...
} 

#./modules/main.tf

resource 'my_resource' 'my_resource_name' {
   ...
}

#./modules/output.tf

output {
   value = ...
}

2 Answers 2

24

The easiest way to import resources into terraform is to first run terraform plan, check the command output and see what's the resource names it generates under the module.

Then run terraform import <resource-name-in-module> <arn/id, depend on the resource>.

So in your case, you would probably need to run something like

terraform import 'module.my_module.my_resource.my_resource_name' 'id'

Note the quotes around the module name; if the module contains count, this is necessary so it's a good habit to get used to.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this. My actual problem (which I don't think was well reprex'd here) required a moved block as well. The import was very helpful, however, thnak you.
worked when adding ‘module‘ to the resource part terraform import 'module.my_module.my_resource.my_resource_name' 'id'
-1

In case of GCP Compute instances I had add to use:

terraform import module.my_instances_module.google_compute_instance.my_resource_name project_id/region/instance_name

1 Comment

This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.