我是 [
https://lonegunmanb.github.io/introduction-terraform/](
https://lonegunmanb.github.io/introduction-terraform/) 的作者。
具体不知道你的需求,但你这个直接的要求也不难做到,需要两个 `locals` 的帮助。
```hcl
terraform {
required_providers {
alicloud = {
source = "aliyun/alicloud"
version = "1.201.2"
}
}
}
provider "alicloud" {
# Configuration options
}
data "alicloud_resource_manager_folders" "CoreFolder" {
output_file = ""
}
locals {
folder_names = sort(data.alicloud_resource_manager_folders.CoreFolder.folders[*].folder_name)
folder_map = {
for folder in data.alicloud_resource_manager_folders.CoreFolder.folders :
folder.folder_name => folder
}
folders = [
for folder_name in local.folder_names :
local.folder_map[folder_name]
]
}
```
顺便说一下,这里的 `locals` 表达式我基本只是简单题试了一下,Github Copilot 就帮我写出来了。