我下面有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.
对上述代码的任何帮助将不胜感激。
预先感谢