terraform

Terraform: Import resource which is held in a module, not root


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 = ...
}

Solution

  • 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 '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.