Module directory does not exist or cannot be read in Terraform

Viewed 17849

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}"
      }
3 Answers

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.

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.

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

Related