Ask Your Question
0

How can I obtain the name of a resource in a different template?

asked 2023-05-30 18:36:08 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-05-30 18:44:02 +0000

bukephalos gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-30 18:36:08 +0000

Seen: 7 times

Last updated: May 30 '23