The objective is to take only the files from the dags folder for each metric and transfer them to a bucket, which needs to have this structure bucket/dags/metric_name/files.
The folder structure is like this:
Local:
metrics/
a_metric/dags/
shared/files
files
b_metric/dags/
shared/files
files
c_metric/dags/
shared/files
files
My main.tf:
provider "google-beta" {
project = local.base_vars.project
region = local.base_vars.region
}
locals {
# Base variables
base_vars = yamldecode(file("base/vars.yaml"))
#OK
# Metrics subfolder names
metrics = distinct([
for dir_path in fileset("metrics/", "*/*/") :
basename(dirname(dir_path))
])
# Metrics configuration
metrics_config = flatten([
for subfolder in local.metrics :
[
for file in fileset("metrics/${subfolder}", "vars.yaml") :
merge(yamldecode(file("metrics/${subfolder}/${file}")), { subfolder = subfolder })
]
])
metrics_paths = [
for subfolder in local.metrics :
{ for file in fileset("metrics/${subfolder}/dags", "**") :
subfolder => {
local_path = "metrics/${subfolder}/dags/${file}"
remote_path = "dags/${subfolder}/${file}"
}...}
]
}
resource "google_storage_bucket_object" "default" {
for_each = local.metrics_paths
name = each.value.remote_path
source = each.value.local_path
bucket = "bucket_test_dags_terraform_001"
}
# OUTPUTS
output "base_vars" {
value = local.base_vars
}
output "metrics" {
value = local.metrics
}
output "metrics_paths" {
value = local.metrics_paths
}
output "metrics_config" {
value = local.metrics_config
}
Error:
│ Error: Invalid for_each argument
│
│ on main.tf line 47, in resource “google_storage_bucket_object” “default”:
│ 47: for_each = local.metrics_paths
│ ├────────────────
│ │ local.metrics_paths is tuple with 3 elements
│
│ The given “for_each” argument value is unsuitable: the “for_each” argument must be a map, or set of strings, and you have provided a value of type tuple.
╵