Как оценить for_each в ресурсе terraform - PullRequest
0 голосов
/ 09 июля 2020

У меня ниже код Terraform, который создает несколько ресурсов с помощью for_each. Я написал код, сославшись на множество примеров for_each, но все равно получаю ошибку. Может кто-нибудь, пожалуйста, проверьте код и помогите.

variables.tf

variable "user_ds" {
  default = ["a-b", "c-d", "d-e", "f-g"]
}

main.tf

resource "aws_sagemaker_notebook_instance_lifecycle_configuration" "life_cycle" {
  for_each = toset(var.users)
  name     = "lifecycle-${var.users[each.key]}"
  on_start = var.on_start
  on_create = var.on_create
}

resource "aws_sagemaker_notebook_instance" "sagemaker" {
  for_each              = toset(var.users)
  name                  = "${var.notebook_prefix}-${var.users[each.key]}-notebook"
  role_arn              = length(var.role_arn) > 1 ? element(var.role_arn, [each.key]) : ""
  instance_type         = var.instance_type
  kms_key_id            = var.kms_key_id
  direct_internet_access = "Disabled"
  subnet_id             = var.subnet_id
  security_groups       = var.security_groups
  lifecycle_config_name = "lifecycle-${var.users[each.key]}"

  tags = {
    Name                  = "${var.notebook_prefix}-${var.users[each.key]}-notebook"
    aws-glue-dev-endpoint = var.glue_endpoint
  }
}

Когда я запускаю terraform apply, я получаю ошибку ниже : -


  on ../../../modules/sagemaker-new/main.tf line 8, in resource "aws_sagemaker_notebook_instance_lifecycle_configuration" "life_cycle":
   8:   name     = "lifecycle-${var.users[each.key]}"
    |----------------
    | each.key is "a-b"
    | var.users is list of string with 4 elements

The given key does not identify an element in this collection value: a number
is required.

on ../../../modules/sagemaker-new/main.tf line 15, in resource "aws_sagemaker_notebook_instance" "sagemaker":
  15:   name                  = "${var.notebook_prefix}-${var.users[each.key]}-notebook"
    |----------------
    | each.key is "a-b"
    | var.users is list of string with 4 elements

The given key does not identify an element in this collection value: a number
is required.

 on ../../../modules/sagemaker-new/main.tf line 16, in resource "aws_sagemaker_notebook_instance" "sagemaker":
  16:   role_arn              = length(var.role_arn) > 1 ? element(var.role_arn, [each.key]) : ""
    |----------------
    | each.key is "a-b"

Invalid value for "index" parameter: number required.

 on ../../../modules/sagemaker-new/main.tf line 22, in resource "aws_sagemaker_notebook_instance" "sagemaker":
  22:   lifecycle_config_name = "lifecycle-${var.users[each.key]}"
    |----------------
    | each.key is "a-b"
    | var.users is list of string with 4 elements

The given key does not identify an element in this collection value: a number
is required.

 25:     Name                  = "${var.notebook_prefix}-${var.users[each.key]}-notebook"
    |----------------
    | each.key is "a-b"
    | var.users is list of string with 4 elements

The given key does not identify an element in this collection value: a number
is required.

Любая помощь по вышеуказанному коду будет оценена.

Заранее спасибо

...