简体   繁体   English

资源块 terraform 中的 For_each 循环

[英]For_each loop in resource block terraform

I am creating aws_workspace using the terraform.我正在使用 terraform 创建 aws_workspace。 I am merging the variable in local.tf with the default values if the values are not provided in the variable.如果变量中未提供值,我会将 local.tf 中的变量与默认值合并。 Then passing those to module.然后将它们传递给模块。 So inside resource.tf I want to eliminate the for_each loop and assign the values without any loop.所以在 resource.tf 中,我想消除 for_each 循环并在没有任何循环的情况下分配值。 Is it possible to do it?有可能做到吗?

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

}

Module.tf模块.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
}

Resource.tf资源.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
}

Your aws_workspace is a map with different values for each user.您的aws_workspace是一个地图,每个用户都有不同的值。 You could only change to count , but this poses its own issues, and for_each is preferred in your case.您只能更改为count ,但这会带来其自身的问题,并且在您的情况下首选for_each

So you either use for_each as you do know, change it to count .所以你要么像你知道的那样使用for_each ,要么把它改成count Or if you totally don't want to use any of that, you have to put for_each in your module instead.或者,如果您完全不想使用其中任何一个,则必须将for_each放入您的模块中。

Update更新

The new aws_workspaces_workspace :新的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
  }

and for module:和模块:

  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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM