Best practices for modules and reusage

Dear all,

I’m new with Terraform and therefore took time to go through the . documentation and several threads but unfortunately I’m still missing a piece about modules’ structure and configuration.

I’m currently starting a build several configurations and try to structure them the best way possible.

Basically I’m using Terraform for both AWS and OCI. To do so I created a folder dedicated to Terraform with 2 subfolders (AWS and OCI). I will put all modules for AWS in the first one and the modules for OCI in the second one.

Beside, I’m trying to follow the principle of having a main.tf as well as a tfvars file for the variables and an outputs.tf.

As I’m going to have different kind of configuration such as Reporting, Deploy workshop, Deploy PaaS my idea is to have a structure like the following:

Terraform
|
|----> AWS
|----> OCI
        |-----> Reporting
        |-----> Deploy_workshop
                   |-----> main.tf
                   |-----> outputs.tf
                   |-----> variables.tfvars

The first question is does it make sense the organise things like that?

While setting this up, I went in the documentation about modules and calling child modules as there will be some common commands that could be mutualised between the projects.

Here the first challenge I do not get working is to configure the provider only once and not in each “project”.

I have the following for the provider:

provider "oci" {
  tenancy_ocid = var.tenancy_ocid
  user_ocid = var.user_ocid
  fingerprint = var.fingerprint
  private_key_path = var.private_key_path
  region = var.region
}

and stored it directly under teh OCI folder.

Now in the main.tf of the reporting “project” for instance, I tried to call the provider as a module:

module "oci" {
                source = ".."
}

I tried several values for the source but I always get an error saying unknown module “oci”.

Any idea how I could configure / define my provider only once and then call it in my different projects??

Many thanks for your support!

David

The provider configuration is rather flexible. I pass a provider implicitly from shell environment variables.

The module source is also flexible. In your case, try the full path from where you run terraform to the module’s directory.

Hi Robert,

thanks for your reply!

I tried with the environment variables and it works. In the second solution by calling a module is works if I put the absolute path to the module.

Thanks

David