user16133873
user16133873

Reputation:

For_each loop in resource block terraform

I am creating aws_workspace using the terraform. I am merging the variable in local.tf with the default values if the values are not provided in the variable. Then passing those to module. So inside resource.tf I want to eliminate the for_each loop and assign the values without any loop. 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

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

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
}

Upvotes: 0

Views: 1347

Answers (1)

Marcin
Marcin

Reputation: 238081

Your aws_workspace is a map with different values for each user. You could only change to count, but this poses its own issues, and for_each is preferred in your case.

So you either use for_each as you do know, change it to count. Or if you totally don't want to use any of that, you have to put for_each in your module instead.

Update

The new 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
}

Upvotes: 1

Related Questions