Home > database >  Terraform workspaces creation
Terraform workspaces creation

Time:10-04

I am trying to write a terraform code for creating workspaces and will be using the same for future creation as well. I am facing an issue while referencing the bundle_ids since there are multiple bundles available and it changes according to the req. each time. if someone can suggest a better approach to this.

resource "aws_workspaces_workspace" "this" {

directory_id = var.directory_id

for_each = var.workspace_user_names

user_name = each.key
bundle_id = [local.bundle_ids["${each.value}"]]
root_volume_encryption_enabled = true
user_volume_encryption_enabled = true
volume_encryption_key          = var.volume_encryption_key
workspace_properties {
user_volume_size_gib                      = 50
root_volume_size_gib                      = 80
running_mode                              = "AUTO_STOP"
running_mode_auto_stop_timeout_in_minutes = 60
}
tags = var.tags
}

terraform.tfvars

directory_id          = "d-xxxxxxx"
##Add the Workspace Username & bundle_id; 
workspace_user_names = {
"User1"  = "n"
"User2"  = "y"
"User3"  = "k"
}

locals.tf

locals {
bundle_ids = {
"n" = "wsb-nn"
"y" = "wsb-yy"
"k" = "wsb-kk"
 }
}

Terraform plan

Error: Incorrect attribute value type
│
│   on r_aws_workspaces.tf line 8, in resource "aws_workspaces_workspace" "this":
│    8:   bundle_id = [local.bundle_ids["${each.value}"]]
│     ├────────────────
│     │ each.value will be known only after apply
│     │ local.bundle_ids is object with 3 attributes
│
│ Inappropriate value for attribute "bundle_id": string required.

CodePudding user response:

At the movement you have a list, but it should be string. Assuming everything else is correct, the following should address your error:

bundle_id = local.bundle_ids[each.value]
  • Related