Below is my folder and file structure.
.
└── gcore
├── main.tf
├── modules
│ ├── gcore_image
│ │ ├── gcore_image.tf
│ │ ├── output.tf
│ │ └── variables.tf
│ ├── gcore_instance
│ │ ├── gcore_instance.tf
│ │ └── variables.tf
│ ├── gcore_network
│ │ ├── gcore_network.tf
│ │ ├── output.tf
│ │ └── variables.tf
│ ├── gcore_provider
│ │ ├── gcore_provider.tf
│ │ └── variables.tf
│ └── gcore_volume
│ ├── gcore_volume.tf
│ ├── output.tf
│ └── variables.tf
├── terraform.tfstate
├── terraform.tfstate.backup
├── terraform.tfvars
└── variables.tf
8 directories, 18 files
The main.tf content is show as below:
module "gcore_provider_module" {
source = "/Users/xxxx/Desktop/git repo/terraform_xxx/gcore/modules/gcore_provider"
}
module "gcore_image_module" {
source = "/Users/xxx/Desktop/git repo/terraform_xxx/gcore/modules/gcore_image"
}
module "gcore_instance_module" {
source = "/Users/xxx/Desktop/git repo/terraform_xxx/gcore/modules/gcore_instance"
}
The gcore_provider.tf content is show as below:
terraform {
required_version = ">= 0.13.0"
required_providers {
gcore = {
source = "G-Core/gcore"
version = "0.3.70"
}
}
}
provider "gcore" {
permanent_api_token = var.gcore_api_token
}
The gcore_image.tf content is show as below:
terraform {
required_version = ">= 0.13.0"
required_providers {
gcore = {
source = "G-Core/gcore"
version = "0.3.70"
}
}
}
provider "gcore" {
permanent_api_token = var.gcore_api_token
}
data "gcore_image" "ubuntu" {
name = "ubuntu-20.04"
region_id = var.gcore_region_id
project_id = var.gcore_project_id
}
With above terraform script, when I on ./gore/ path, I execute terraform init, I managed to initialise my terraform.
Now I edit gcore_image.tf and replace the terraform block as well as provider block to module gcore_provider. Now the gcore_image.tf script is show as below:
module "gcore_provider_module" {
source = "/Users/xxxx/Desktop/git repo/terraform_xxx/gcore/modules/gcore_provider"
}
data "gcore_image" "ubuntu" {
name = "ubuntu-20.04"
region_id = var.gcore_region_id
project_id = var.gcore_project_id
}
When I execute terraform init, I encountered the error below:
Initializing the backend...
Initializing modules...
Initializing provider plugins...
- Reusing previous version of g-core/gcore from the dependency lock file
- Finding latest version of hashicorp/gcore...
- Using previously-installed g-core/gcore v0.3.70
╷
│ Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider
│ hashicorp/gcore: provider registry registry.terraform.io does not have a
│ provider named registry.terraform.io/hashicorp/gcore
│
│ Did you intend to use g-core/gcore? If so, you must specify that source
│ address in each module which requires that provider. To see which modules are
│ currently depending on hashicorp/gcore, run the following command:
│ terraform providers
╵
Note that I use the provider module in main.tf and it's working but when I use the provider module in other child module, it's not working.