【问题标题】:Unable to configure terraform dynamic block无法配置 terraform 动态块
【发布时间】:2021-09-29 11:22:09
【问题描述】:

我正在尝试通过将值传递给动态块来创建 aws_workspace_workspaces,但它没有从那里获取值。主要目的是自动将价值分配给资源。它显示了这个错误

on modules/aws_workspace/main.tf line 4, in resource "aws_workspaces_workspace" "example": │ 4: dynamic "aws_workspace" { │ │ Blocks of type "aws_workspace" are not expected here.

dev.tfvars

aws_workspace={    
  user_name    = "john.doe"
  root_volume_encryption_enabled = true
  user_volume_encryption_enabled = true
  volume_encryption_key          = "alias/aws/workspaces"

  workspace_properties = {
    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
  }
}
tags = {
    Department = "IT"
}
resource "aws_workspaces_workspace" "example" {
    directory_id = var.directory_id
    bundle_id    = var.bundle_id
    dynamic "aws_workspace" {
        for_each = var.aws_workspace
        content {
            user_name    = aws_workspace.value.user_name

            root_volume_encryption_enabled = aws_workspace.value.root_volume_encryption_enabled
            user_volume_encryption_enabled = aws_workspace.value.user_volume_encryption_enabled
            volume_encryption_key          = aws_workspace.value.volume_encryption_key

            workspace_properties {
                compute_type_name                         = aws_workspace.value.compute_type_name
                user_volume_size_gib                      = aws_workspace.value.user_volume_size_gib
                root_volume_size_gib                      = aws_workspace.value.root_volume_size_gib
                running_mode                              = aws_workspace.value.running_mode
                running_mode_auto_stop_timeout_in_minutes = aws_workspace.value.running_mode_auto_stop_timeout_in_minutes
            }
        }
    }
  tags = {
    Department = "IT"
  }
}

因此,如果我将值传递给另一个用户,它也会创建它,这就是我想要实现的基本目标

module "my_workspaces" {
    source = "./modules/workspaces"

    hosts = {
        "user1" = {
            "user_name"                                 = "user1.last1",
            "compute_type_name"                         = var.compute_type_name
            "user_volume_size_gib"                      = var.user_volume_size_gib,
            "root_volume_size_gib"                      = var.root_volume_size_gib,
            "running_mode"                              = var.running_mode,
            "running_mode_auto_stop_timeout_in_minutes" = var.auto_stop_timeout_min
        },
        "user2" = {
            "user_name"                                 = "user2.last2",
            "compute_type_name"                         = var.compute_type_name
            "user_volume_size_gib"                      = var.user_volume_size_gib,
            "root_volume_size_gib"                      = var.root_volume_size_gib,
            "running_mode"                              = var.running_mode,
            "running_mode_auto_stop_timeout_in_minutes" = var.auto_stop_timeout_min
        },
    }
}

【问题讨论】:

  • aws_workspaces_workspace中没有aws_workspace这样的块。你想让我做什么?也许您使用了错误的资源?
  • 我正在尝试循环变量并将值分配给 aws_workspace,我已经用我想要实现的目标更新了问题
  • 因此,如果我将值传递给另一个用户,它也会创建它,这是我试图实现的基本目标。只需从 dev.tfvars 传递值就会创建新用户

标签: amazon-web-services amazon-ec2 terraform terraform-provider-aws


【解决方案1】:

在 aws_workspaces_workspace 中没有 aws_workspace 这样的块。你应该直接使用for_each

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 = {
    Department = "IT"
  }
}

【讨论】:

    猜你喜欢
    • 2020-08-18
    • 2018-03-17
    • 2021-09-24
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    相关资源
    最近更新 更多