amazon-web-services - 处理 for_each 中的空值并分配默认值 terraform

标签 amazon-web-services amazon-ec2 terraform terraform-provider-aws

我正在创建 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".

最佳答案

您必须稍微更改您的设计,以便默认值位于变量之外。例如:


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

关于amazon-web-services - 处理 for_each 中的空值并分配默认值 terraform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68494549/

相关文章:

node.js - 如何使用node js合并两个json的数据

node.js - 使用 NodeJs 的 AWS DynamoDB 查询

hadoop - 如何为 'hadoop-ec2' 正确配置 Amazon EC2 AMI?

azure - Terraform Azure 扩展问题

bash - 使用 bash 脚本的输出作为 Terraform 中的变量

azure - 通过 Terraform 部署应用程序网关时出错

javascript - 使用 EJS 的 ExpressJS 在 AWS Amplify 上部署时无法加载静态 Assets

amazon-web-services - 获取 Lambda 上经过身份验证(确认)的用户的用户名?

amazon-web-services - EC2 实例连接 : There was a problem setting up the instance connection

amazon-web-services - 需要通过cloudformation模板根据region添加内容到resolv.conf中