Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

If you want to obtain the name of a resource in a different template, you can pass it as a parameter to the other template. In Terraform, you can use the output block to export the value of a resource attribute or a variable, and then use the module block to include that output as a parameter in another module. For example:

In the module that creates the resource:

resource "aws_instance" "my_instance" {
  # ... instance configuration ...
}

output "instance_name" {
  value = aws_instance.my_instance.id
}

In the module that references the resource:

module "my_module" {
  source = "path/to/module"
}

# use the instance name as a parameter in this module
resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-${module.my_module.instance_name}-bucket"
  # ... bucket configuration ...
}

In this example, the first module creates an EC2 instance and exports its ID as an output called instance_name. The second module includes the first module using the module block, and passes the output value as a parameter to the aws_s3_bucket resource. This allows the bucket name to include the instance name as a unique identifier.