Reputation: 131
I want to use the project name from the path as an input to main.tf. For example I have the file path as follows
/env/nonprod/overlay/prj-npe-02/main.tf
and in my main.tf can the input var.project_name be taken from the file path which is "../prj-npe-02/.."
main.tf
data "google_project" "project" {
project_id = var.project_name
}
Upvotes: 1
Views: 1564
Reputation: 131
similar to Hannes answer but I used regex to arrive at the solution
data "google_project" "project" {
project_id = regex( "prj-[^\\/]+", abspath(path.root))
}
Upvotes: 1
Reputation: 83
This should be possible by using the split and abspath function. https://developer.hashicorp.com/terraform/language/functions/split https://developer.hashicorp.com/terraform/language/functions/abspath
locals {
absolute_path = abspath(path.root)
project_id = split("/", local.absolute_path)[3]
}
output "name" {
value = local.project_id
}
Just tested on my end and works like a charm.
If you want to adapt to your resource, it should look like this.
data "google_project" "project" {
project_id = local.project_id
}
Upvotes: 1