Harry Leboeuf
Harry Leboeuf

Reputation: 745

Terraform Import

I have a Terraform environment with multiple subscriptions. My Directory structure looks in a simplified way like this

_Config
++SubScr1
++++terraform.tfvars
++SubScr2
++++terraform.tfvars

_Modules
++general
++++rg_create
++++++main.tf
++++++output.tf
++++++variables.tf

_Templates
++rg
++++main.tf
++++variables.tf

maif.tf
providers.tf
variables.tf

Until now all was in Azure DevOps, we are now also working locally and syncing git.

I've been able to create a CLI scripts that performs a 'az login', set environment variables and performs a terraform init. Working fine.

A terraform plan also is working from the 'root' directory.

But when I try to do a import of a (ex) resource group this fails complaining that azurerm_resource_group is not found, when I move to the subdirectory '_module\general\rg_create' then the system gives error on the fact thet other modules haven't been initialized..

So I'm a bit confused. Might be I'm missing somewhere a concept, any help appreciated

Error 1

    T:\_CommandLine>CD \

 T:\>TerraForm import -var-file="T:\_config\it-poc-int-01\core_000\terraform.tfvars" azurerm_resource_group.rg /subscriptions/8dc72845-b367-4dcc-98f9-d9a4a933defc/resourceGroups/rg-poc-edw-999
    Error: resource address "azurerm_resource_group.rg" does not exist in the configuration.
    
    Before importing this resource, please create its configuration in the root module. For example:
    
    resource "azurerm_resource_group" "rg" {
      # (resource arguments)
    }

Or error 2

T:\_CommandLine>CD \_Modules\general\rg_create

T:\_modules\general\rg_create>TerraForm import -var-file="T:\_config\it-poc-int-01\core_000\terraform.tfvars" azurerm_resource_group.rg /subscriptions/8dc72845-b367-4dcc-98f9-d9a4a933defc/resourceGroups/rg-poc-edw-999
╷
│ Error: Module not installed
│
│   on main.tf line 14:
│   14: module "subscription" {
│
│ This module is not yet installed. Run "terraform init" to install all modules required by this configuration.
╵

╷
│ Error: Module not installed
│
│   on main.tf line 18:
│   18: module "location" {
│
│ This module is not yet installed. Run "terraform init" to install all modules required by this configuration.
╵

╷
│ Error: Module not installed
│
│   on main.tf line 38:
│   38: module "rg_roles_aadgroups" {
│
│ This module is not yet installed. Run "terraform init" to install all modules required by this configuration.
╵

Upvotes: 0

Views: 1972

Answers (1)

David
David

Reputation: 584

you need to do

terraform init

in root directory,

to import you need at least

resource "azurerm_resource_group" "rg" { } anywhere in your configs and again you should call it in root dir

Upvotes: 1

Related Questions