我正在使用 terraform 创建 aws_workspace。如果变量中未提供值,我会将 local.tf 中的变量与默认值合并。然后将它们传递给模块。所以在 resource.tf 中,我想消除 for_each 循环并在没有任何循环的情况下分配值。可以吗?
local.tf
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)
}
}
模块.tf
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 = "wahaj.akmal"
}
}
description = "aws workspace configuration"
}
variable "tags" {
default = ""
description = "tags for the resouces"
}
variable "region" {
default = ""
description = "region on which aws infra is to be deployed"
}
data "aws_workspaces_bundle" "value_windows_10" {
bundle_id = "wsb-bh8rsxt14"
}
module "aws_workspace" {
source = "./modules/aws_workspace"
aws_workspace = local.final_aws_workspace
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 = ""
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"
}
resource "aws_workspaces_workspace" "this" {
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
}
答案 0 :(得分:2)
您的 aws_workspace
是一张地图,每个用户都有不同的值。您只能更改为 count
,但这会带来其自身的问题,在您的情况下首选 for_each
。
因此,您可以使用已知的 for_each
,将其更改为 count
。或者,如果您完全不想使用其中任何一个,则必须将 for_each
放在您的模块中。
更新
新的aws_workspaces_workspace
:
resource "aws_workspaces_workspace" "this" {
directory_id = var.directory_id
bundle_id = var.bundle_id
user_name = var.aws_workspace.user_name
root_volume_encryption_enabled = var.aws_workspace.root_volume_encryption_enabled
user_volume_encryption_enabled = var.aws_workspace.user_volume_encryption_enabled
volume_encryption_key = var.aws_workspace.volume_encryption_key
workspace_properties {
compute_type_name = var.aws_workspace.compute_type_name
user_volume_size_gib = var.aws_workspace.user_volume_size_gib
root_volume_size_gib = var.aws_workspace.root_volume_size_gib
running_mode = var.aws_workspace.running_mode
running_mode_auto_stop_timeout_in_minutes = var.aws_workspace.running_mode_auto_stop_timeout_in_minutes
}
对于模块:
module "aws_workspace" {
for_each = local.final_aws_workspace
source = "./modules/aws_workspace"
aws_workspace = each.value
tags = {
Name = "cloud"
}
bundle_id = data.aws_workspaces_bundle.value_windows_10.id
directory_id = aws_workspaces_directory.example.id
}