SiddhiMorajkar
SiddhiMorajkar

Reputation: 425

Module directory does not exist or cannot be read in Terraform

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

Upvotes: 9

Views: 49095

Answers (5)

Jules Clements
Jules Clements

Reputation: 478

I have encountered this symptom with quite an unexpected root cause. Using Terraform Cloud for state management, in the CI/CD tooling I construct the credentials.tfrc.json file, however, I had a typo causing the JSON to be invalid. Instead of reporting a validation error or 4xx status, the following was returned.

The directory could not be read for module

Note: there are no remote modules in this use case, all modules are local to the deploy time workspace.

Upvotes: 0

Zhakyp Zhoomart uulu
Zhakyp Zhoomart uulu

Reputation: 83

Check your Terraform working directory in the Terraform cloud. The directory that Terraform will execute within. This defaults to the root of your repository and is typically set to a subdirectory matching the environment when multiple environments exist within the same repository. Add your module's path to Terraform Working Directory

Upvotes: 1

user16250589
user16250589

Reputation: 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.

Upvotes: -1

Yash Varshney
Yash Varshney

Reputation: 375

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

Upvotes: -3

Charles Xu
Charles Xu

Reputation: 31384

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.

Upvotes: 6

Related Questions