2

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

1 Answers1

4

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
Marcin
  • 215,873
  • 14
  • 235
  • 294