我正在创建 aws_workspace,在这个 terraform 中,我正在循环主文件中提供的变量并为每个循环分配值。但是我面临的问题是我试图将用户名仅作为第二个变量传递,现在我正在尝试 for-each 循环应该选择默认值,如果该值不是从 main.tf 文件中给出的,而是它的不工作。就像它应该选择第二个用户名并默认附加其他值
main.tf
module "aws_workspace" {
source = "./modules/aws_workspace"
aws_workspace = {
user1 = {
user_name = "john.doe"
root_volume_encryption_enabled = true
user_volume_encryption_enabled = true
volume_encryption_key = "alias/aws/workspaces"
compute_type_name = "VALUE"
user_volume_size_gib = 10
root_volume_size_gib = 80
running_mode = "AUTO_STOP"
running_mode_auto_stop_timeout_in_minutes = 60
},
user2 = {
user_name = "james"
}
}
tags = {
Name = "cloud"
}
bundle_id = data.aws_workspaces_bundle.value_windows_10.id
directory_id = aws_workspaces_directory.example.id
}
变量.tf
variable "aws_workspace" {
default = [
{
root_volume_encryption_enabled = true
user_volume_encryption_enabled = true
volume_encryption_key = "alias/aws/workspaces"
compute_type_name = "VALUE"
user_volume_size_gib = 10
root_volume_size_gib = 80
running_mode = "AUTO_STOP"
running_mode_auto_stop_timeout_in_minutes = 60
}
]
description = "configuration of aws workspaces"
}
variable "tags" {
default = ""
description = "tags of the resources"
}
variable "directory_id" {
default = ""
description = "Id of the directory"
}
variable "bundle_id" {
default = ""
description = "id of the bundle"
}
资源.tf
resource "aws_workspaces_workspace" "example" {
directory_id = var.directory_id
bundle_id = var.bundle_id
for_each = var.aws_workspace
user_name = each.value.user_name
root_volume_encryption_enabled = each.value.root_volume_encryption_enabled
user_volume_encryption_enabled = each.value.user_volume_encryption_enabled
volume_encryption_key = each.value.volume_encryption_key
workspace_properties {
compute_type_name = each.value.compute_type_name
user_volume_size_gib = each.value.user_volume_size_gib
root_volume_size_gib = each.value.root_volume_size_gib
running_mode = each.value.running_mode
running_mode_auto_stop_timeout_in_minutes = each.value.running_mode_auto_stop_timeout_in_minutes
}
tags = var.tags
}
错误:
╷
│ Error: Unsupported attribute
│
│ on modules/aws_workspace/main.tf line 9, in resource "aws_workspaces_workspace" "example":
│ 9: root_volume_encryption_enabled = each.value.root_volume_encryption_enabled
│ ├────────────────
│ │ each.value is object with 1 attribute "user_name"
│
│ This object does not have an attribute named "root_volume_encryption_enabled".
╵
╷
│ Error: Unsupported attribute
│
│ on modules/aws_workspace/main.tf line 10, in resource "aws_workspaces_workspace" "example":
│ 10: user_volume_encryption_enabled = each.value.user_volume_encryption_enabled
│ ├────────────────
│ │ each.value is object with 1 attribute "user_name"
│
│ This object does not have an attribute named "user_volume_encryption_enabled".
╵
╷
│ Error: Unsupported attribute
│
│ on modules/aws_workspace/main.tf line 11, in resource "aws_workspaces_workspace" "example":
│ 11: volume_encryption_key = each.value.volume_encryption_key
│ ├────────────────
│ │ each.value is object with 1 attribute "user_name"
│
│ This object does not have an attribute named "volume_encryption_key".
╵
╷
│ Error: Unsupported attribute
│
│ on modules/aws_workspace/main.tf line 14, in resource "aws_workspaces_workspace" "example":
│ 14: compute_type_name = each.value.compute_type_name
│ ├────────────────
│ │ each.value is object with 1 attribute "user_name"
│
│ This object does not have an attribute named "compute_type_name".
╵
╷
│ Error: Unsupported attribute
│
│ on modules/aws_workspace/main.tf line 15, in resource "aws_workspaces_workspace" "example":
│ 15: user_volume_size_gib = each.value.user_volume_size_gib
│ ├────────────────
│ │ each.value is object with 1 attribute "user_name"
│
│ This object does not have an attribute named "user_volume_size_gib".
╵
╷
│ Error: Unsupported attribute
│
│ on modules/aws_workspace/main.tf line 16, in resource "aws_workspaces_workspace" "example":
│ 16: root_volume_size_gib = each.value.root_volume_size_gib
│ ├────────────────
│ │ each.value is object with 1 attribute "user_name"
│
│ This object does not have an attribute named "root_volume_size_gib".
╵
╷
│ Error: Unsupported attribute
│
│ on modules/aws_workspace/main.tf line 17, in resource "aws_workspaces_workspace" "example":
│ 17: running_mode = each.value.running_mode
│ ├────────────────
│ │ each.value is object with 1 attribute "user_name"
│
│ This object does not have an attribute named "running_mode".
╵
╷
│ Error: Unsupported attribute
│
│ on modules/aws_workspace/main.tf line 18, in resource "aws_workspaces_workspace" "example":
│ 18: running_mode_auto_stop_timeout_in_minutes = each.value.running_mode_auto_stop_timeout_in_minutes
│ ├────────────────
│ │ each.value is object with 1 attribute "user_name"
│
│ This object does not have an attribute named "running_mode_auto_stop_timeout_in_minutes".
答案 0 :(得分:2)
您必须稍微更改您的设计,以便默认值在变量之外。例如:
variable "aws_workspace" {
default = {
user1 = {
user_name = "john.doe"
root_volume_encryption_enabled = true
user_volume_encryption_enabled = true
volume_encryption_key = "alias/aws/workspaces"
compute_type_name = "VALUE"
user_volume_size_gib = 10
root_volume_size_gib = 80
running_mode = "AUTO_STOP"
running_mode_auto_stop_timeout_in_minutes = 60
},
user2 = {
user_name = "james"
}
}
description = "configuration of aws workspaces"
}
locals {
my_defaults = {
root_volume_encryption_enabled = true
user_volume_encryption_enabled = true
volume_encryption_key = "alias/aws/workspaces"
compute_type_name = "VALUE"
user_volume_size_gib = 10
root_volume_size_gib = 80
running_mode = "AUTO_STOP"
running_mode_auto_stop_timeout_in_minutes = 60
}
final_aws_workspace = {for k,v in var.aws_workspace:
k => merge(local.my_defaults, v)
}
}
然后你可以使用:
for_each = var.final_aws_workspace