8

I am trying to use Modules for dependencies in my Terraform code. But even after mentioning that particular source path in module, it gives an error “ Module directory does not exist or cannot be read." and “Unable to evaluate directory – The system cannot find the file specified." Can anyone let me know what can be the reason.

I have to manage 3 different environments with 3 different backend state files for each environment. Here each main file calls the respective module file.The main folder consists of backend config, creation of resource group and it calls modules file

      root
        |
        |-- main 
        |    |--prod  
        |    |--dev    
        |    |--staging
        |-- modules
        |    |--prod   
        |    |--dev     
        |    |--staging

------------CODE-----------------

    provider "azurerm" {
    version = "=2.2.0"
   features {}
    }

    #--- CREATING RESOURCE GROUP PER ENVIRONEMENT
    terraform {
      backend "azurerm" {
        resource_group_name  = ""
        storage_account_name = ""
        container_name       = ""
        key                  = ""
        }
      }


    variable "location" {
      description           =   "Location for deployment of the Azure 
    resources"
   }

     variable "Code" {
       description           =   "Enter a unique two-letter ID to identify 
    customer resources; should match the DynamoDB table."
    }

     variable "EnvironmentType" {
       description       = "Enter a valid environment type. Valid values are 
     Prod, Dev, Staging"
      }

    variable "AccountType" {
      description   = "Select the type of account you wish to create. This 
       will determine which environments and other resources are created."
       }


     resource "azurerm_resource_group" "main" {
      name        = "${var.Code}-${var.EnvironmentType}"
      location    = "${var.location}"
     }

     module "ResourcesStack" {
        source                      = "./modules"
        AccountType                 = "${var.AccountType}"
        CustomerCode                = "${var.Code}"
        EnvironmentType             = "${var.EnvironmentType}"
        location                    = "${var.location}"
      }
SiddhiMorajkar
  • 395
  • 4
  • 7
  • 18

3 Answers3

7

Well, with the communication and then I think you did the mistake when you quote the modules in the Terraform code.

The mistake is that when you want to quote the modules, you need to quote the special one. For example, you want to quote the module dev, then you can quote it in the Terraform code like this:

module "dev" {
  source      = "./modules/dev"
  ...
}

Do not set the module source with the root path of all the modules like you did.

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • i will check the source again. – SiddhiMorajkar Mar 27 '20 at 11:20
  • It is still not working. I hope you know the prod, dev, staging inside module/main folder is not a .tf file. They are folders which contains the respective environment .tf file – SiddhiMorajkar Mar 30 '20 at 14:30
  • @SiddhiMorajkar It should work. I double if you create the right modules and know how to use them. Take a look at the [Create Terraform modules](https://www.terraform.io/docs/modules/index.html). And you also need to create t.tf file in the modules to create resources. – Charles Xu Mar 31 '20 at 01:28
  • I have created and refered the module correctly. Also now I have got another issue. Suppose i have a vnet.tf that creates virtual network and then calls the application gateway module, then it throws the error "Error: Virtual Network "aksVirtualNetwork" was not found ". I guess it does not create a vnet first. – SiddhiMorajkar Mar 31 '20 at 07:41
  • @SiddhiMorajkar Can you share all the Terraform things you used? I cannot see them so that I cannot figure out what is the actual error. – Charles Xu Mar 31 '20 at 08:16
  • Okay but that error is totally different from this issue – SiddhiMorajkar Mar 31 '20 at 08:23
  • @SiddhiMorajkar If my answer solves this issue you meet? – Charles Xu Mar 31 '20 at 08:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210642/discussion-between-siddhimorajkar-and-charles-xu). – SiddhiMorajkar Mar 31 '20 at 08:33
  • @SiddhiMorajkar Your reply is too late, I wait for almost a month. I think you need to reply quickly. Maybe you do not use the real path for your modules. – Charles Xu Apr 30 '20 at 01:29
-1

I was facing a similar issue recently, while running terraform in Automation (AWS code build) Which brought me to this post. The script ran fine locally on my desktop but failed in automation may be because how it was packaged.

Initializing modules...

  • setup in
  • s3_agent in ../modules/s3
  • s3_artifacts in ../modules/s3 ╷ │ Error: Unreadable module directory │ │ Unable to evaluate directory symlink: lstat modules: no such file or │ directory ╵ Module directory does not exist or cannot be read.

I noticed that the setup module highlighted above did not show the path to the module. It was only after hours that I realized that in the overall directory which had terraform scripts there were 2 folders with same name "modules" although at different folder hierarchy. It confused the Terraform to think that all modules are in 1 of the folder.

Solution: I renamed one of the folders which had setup module as module instead of "modules"

I hope this helps someone.

-3

Always make sure name of directory is module not modules. I faced the same issue and after updating the directory name, it was resolved.

Yash Varshney
  • 365
  • 1
  • 5
  • 16
  • Examples in the official terraform documentation are showing it as "modules" directory not "module". Regardless it should not matter. – Tomer Apr 28 '22 at 02:33