处理 for_each 中的空值并分配默认值 terraform [英] Handling empty value in for_each and assign default value terraform

查看:50
本文介绍了处理 for_each 中的空值并分配默认值 terraform的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建 aws_workspace,在这个 terraform 中,我正在循环主文件中提供的变量并为每个循环分配值.但是我面临的问题是我试图将用户名仅作为第二个变量传递,现在我正在尝试 for-each 循环应该选择默认值,如果该值不是从 main.tf 文件中给出的,而是它的不工作.就像它应该选择第二个用户名并默认附加其他值

main.tf

模块aws_workspace"{source = "./modules/aws_workspace";aws_workspace = {用户 1 = {user_name = "john.doe";root_volume_encryption_enabled = 真user_volume_encryption_enabled = truevolume_encryption_key =别名/aws/工作区";compute_type_name = "VALUE";user_volume_size_gib = 10root_volume_size_gib = 80running_mode = "AUTO_STOP";running_mode_auto_stop_timeout_in_minutes = 60},用户 2 = {用户名 = 詹姆斯"}}标签 = {名称 = 云"}bundle_id = data.aws_workspaces_bundle.value_windows_10.iddirectory_id = aws_workspaces_directory.example.id}

变量.tf

变量aws_workspace";{默认 = [{root_volume_encryption_enabled = 真user_volume_encryption_enabled = truevolume_encryption_key =别名/aws/工作区";compute_type_name = "VALUE";user_volume_size_gib = 10root_volume_size_gib = 80running_mode = "AUTO_STOP";running_mode_auto_stop_timeout_in_minutes = 60}]description =aws 工作空间的配置";}变量标签"{默认="description =资源标签";}变量directory_id"{默认="description = "目录的 ID";}变量bundle_id"{默认="description =捆绑包的id";}

资源.tf

资源aws_workspaces_workspace"示例"{directory_id = var.directory_idbundle_id = var.bundle_idfor_each = var.aws_workspaceuser_name = each.value.user_nameroot_volume_encryption_enabled = each.value.root_volume_encryption_enableduser_volume_encryption_enabled = each.value.user_volume_encryption_enabledvolume_encryption_key = each.value.volume_encryption_key工作空间属性{compute_type_name = each.value.compute_type_nameuser_volume_size_gib = each.value.user_volume_size_gibroot_volume_size_gib = each.value.root_volume_size_gibrunning_mode = each.value.running_moderunning_mode_auto_stop_timeout_in_minutes = each.value.running_mode_auto_stop_timeout_in_minutes}标签 = var.tags}

错误:

╷│ 错误:不支持的属性││ 在 modules/aws_workspace/main.tf 第 9 行,在资源aws_workspaces_workspace"中示例":│ 9: root_volume_encryption_enabled = each.value.root_volume_encryption_enabled│ ├──────────────────│ │ each.value 是具有 1 个属性user_name"的对象││ 该对象没有名为root_volume_encryption_enabled"的属性.╵╷│ 错误:不支持的属性││ 在 modules/aws_workspace/main.tf 第 10 行,在资源aws_workspaces_workspace"中示例":│ 10: user_volume_encryption_enabled = each.value.user_volume_encryption_enabled│ ├──────────────────│ │ each.value 是具有 1 个属性user_name"的对象││ 该对象没有名为user_volume_encryption_enabled"的属性.╵╷│ 错误:不支持的属性││ 在 modules/aws_workspace/main.tf 第 11 行,在资源aws_workspaces_workspace"中示例":│ 11:volume_encryption_key = each.value.volume_encryption_key│ ├──────────────────│ │ each.value 是具有 1 个属性user_name"的对象││ 该对象没有名为volume_encryption_key"的属性.╵╷│ 错误:不支持的属性││ 在 modules/aws_workspace/main.tf 第 14 行,在资源aws_workspaces_workspace"中示例":│ 14:compute_type_name = each.value.compute_type_name│ ├──────────────────│ │ each.value 是具有 1 个属性user_name"的对象││ 该对象没有名为compute_type_name"的属性.╵╷│ 错误:不支持的属性││ 在 modules/aws_workspace/main.tf 第 15 行,在资源aws_workspaces_workspace"中示例":│ 15: user_volume_size_gib = each.value.user_volume_size_gib│ ├──────────────────│ │ each.value 是具有 1 个属性user_name"的对象││ 该对象没有名为user_volume_size_gib"的属性.╵╷│ 错误:不支持的属性││ 在 modules/aws_workspace/main.tf 第 16 行,在资源aws_workspaces_workspace"中示例":│ 16: root_volume_size_gib = each.value.root_volume_size_gib│ ├──────────────────│ │ each.value 是具有 1 个属性user_name"的对象││ 该对象没有名为root_volume_size_gib"的属性.╵╷│ 错误:不支持的属性││ 在 modules/aws_workspace/main.tf 第 17 行,在资源aws_workspaces_workspace"中示例":│ 17: running_mode = each.value.running_mode│ ├──────────────────│ │ each.value 是具有 1 个属性user_name"的对象││ 该对象没有名为running_mode"的属性.╵╷│ 错误:不支持的属性││ 在 modules/aws_workspace/main.tf 第 18 行,在资源aws_workspaces_workspace"中示例":│ 18: running_mode_auto_stop_timeout_in_minutes = each.value.running_mode_auto_stop_timeout_in_minutes│ ├──────────────────│ │ each.value 是具有 1 个属性user_name"的对象││ 该对象没有名为running_mode_auto_stop_timeout_in_minutes"的属性.

解决方案

你必须稍微改变你的设计,以便默认值在变量之外.例如:

<预><代码>变量aws_workspace"{默认 = {用户 1 = {user_name = "john.doe";root_volume_encryption_enabled = 真user_volume_encryption_enabled = truevolume_encryption_key =别名/aws/工作区";compute_type_name = "VALUE";user_volume_size_gib = 10root_volume_size_gib = 80running_mode = "AUTO_STOP";running_mode_auto_stop_timeout_in_minutes = 60},用户 2 = {用户名 = 詹姆斯"}}description =aws 工作空间的配置";}当地人{my_defaults = {root_volume_encryption_enabled = 真user_volume_encryption_enabled = truevolume_encryption_key =别名/aws/工作区";compute_type_name = "VALUE";user_volume_size_gib = 10root_volume_size_gib = 80running_mode = "AUTO_STOP";running_mode_auto_stop_timeout_in_minutes = 60}final_aws_workspace = {对于 var.aws_workspace 中的 k,v:k=>合并(local.my_defaults,v)}}

然后你可以使用:

for_each = var.final_aws_workspace

I am creating aws_workspace, In this terraform I am looping over the variables provided in the main file and assigning the values using for each loop. But the issue that I am facing is that I am trying to pass the username only as a second variable and now I am trying that for-each loop should pick the default value if the value is not given from the main.tf file but its not working. like it should pick the second username and append other values by default with it

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
}

variable.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"
}

resource.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:

╷
│ 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".

解决方案

You have to change your design a bit, so that default values are outside of variable. For example:


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)
                        }

}

Then you can use :

for_each = var.final_aws_workspace

这篇关于处理 for_each 中的空值并分配默认值 terraform的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆