Home > Net >  access terraform modules in different root directory
access terraform modules in different root directory

Time:10-24

I have this file structure that contains 2 root modules that I apply from them separately. I want to access in one module module output from different root directory

module "lambda" {
  source                           = "../../modules/lambda"

  environment                      = var.environment
  clean-bucket-name                = "../flow-components/module.s3.clean-bucket-name"
  infected-bucket-name             = "../flow-components/module.s3.infected-bucket-name"
  after-classification-bucket-name = module.s3.after-classification-bucket-name
}

this is two examples of trying to access the module.

the first one just give this string and the second one gives me this error

│ Error: Reference to undeclared module
│
│   on lambda.tf line 8, in module "lambda":
│    8:   after-classification-bucket-name = module.s3.after-classification-bucket-name
│
│ No module call named "s3" is declared in the root module.

this is the second module

module "s3" {
  source      = "../../modules/s3"
  environment = var.environment
}

and the output file of that module

output "before-av-bucket-name" {
  value = aws_s3_bucket.before-av-bucket.bucket
}

output "clean-bucket-name" {
  value = aws_s3_bucket.clean-bucket.bucket
}

output "infected-bucket-name" {
  value = aws_s3_bucket.infected-bucket.bucket
}

output "after-classification-bucket-name" {
  value = aws_s3_bucket.after-classification-bucket.bucket
}

the file structure

├───environment
│   ├───flow-components
│   │   ├───provider
│   │   ├───s3
│   ├───lambdas
│   │   ├───provider
│   │   ├─── lambda
└───modules
    ├───lambda
    └───s3

How can I access the output of this module?

CodePudding user response:

You aren't using valid Terraform syntax. To reference the output of a module you don't specify that as a file path. You have the correct syntax right there in the after-classification-bucket-name attribute, so I'm not sure what you are trying to do with the other two attributes.

The correct syntax is:

module "lambda" {
  source                           = "../../modules/lambda"

  environment                      = var.environment
  clean-bucket-name                = module.s3.clean-bucket-name
  infected-bucket-name             = module.s3.infected-bucket-name
  after-classification-bucket-name = module.s3.after-classification-bucket-name
}

CodePudding user response:

You should reference your module and not a resource:

output "before-av-bucket-name" {
  value = module.before-av-bucket.bucket
}

output "clean-bucket-name" {
  value = module.clean-bucket.bucket
}

output "infected-bucket-name" {
  value = module.infected-bucket.bucket
}

output "after-classification-bucket-name" {
  value = module.after-classification-bucket.bucket
}

Try this approach

  • Related