I am using a monorepo with a basic structure, an example can be reviewed below, the AWS IAM Module here https://registry.terraform.io/modules/terraform-aws-modules/iam/aws/latest, and Terraform Cloud.
The problem is that I'm not grasping on how to use a module throughout the project e.g. I want to use one AWS IAM Policy module for dev, staging, and production, how do I reference this module within resources?
├── development
│ └── main.tf
├── modules
│ └── aws-iam-module.tf
├── production
│ └── main.tf
├── staging
│ └── main.tf
└── versions.tf
The module itself creates the policy without any issue if it's in the root directory of wherever terraform apply gets created. It's likely that I'm not grasping the reusability of this public module.
module "iam_policy_example" {
source = "terraform-aws-modules/iam/aws//modules/iam-policy"
version = "~> 4"
name = "example_policy"
path = "/"
description = "Yet another example policy using the module"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:Describe*"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}
You need to call the module with a local path reference in the source
argument.
Currently you only have defined a single module in the folder modules
. (Note that Terraform considers a module to consist of all .tf
files in one directory.)
module "aws_iam" {
source = "../modules"
# If the module defines variables, pass values to them here
# some_var = "some-value"
}
However, your folder structur suggests, that you might want to add further modules in future. So a recommendation would be to create a subfolder modules/aws-iam
and move aws-iam-module.tf
to this folder.
Potentially, you might also wnat to rename it to main.tf
if you want to follow Terraform Standard Module structure.
Once this is done, you can call the module with:
module "aws_iam" {
source = "../modules/aws-iam"
# If the module defines variables, pass values to them here
# some_var = "some-value"
}