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