【发布时间】:2021-01-20 21:13:27
【问题描述】:
我有一个创建子模块的模块,并且每个子模块都有一个 custom_error_response,它会到达不同的 response_page_path。
在我添加的主模块中;
custom_error_response = [
{
error_code = "403"
error_caching_min_ttl = "30"
response_code = "200"
response_page_path = "/${var.default_root_object}"
},
]
而变量 default_root_object 是:
variable "default_root_object" {
description = "Default root object at origin for CloudFront distribution"
}
在子模块中,我在 variables.tf 中添加了以下内容:
variable "custom_error_response" {
description = "(Optional) - List of one or more custom error response element maps"
type = "list"
default = []
}
在每个子模块的 main.tf 中,我还定义了 custom_error_response,如下所示:
dynamic "custom_error_response" {
for_each = var.custom_error_response
content {
error_caching_min_ttl = lookup(custom_error_response.value, "error_caching_min_ttl", null)
error_code = custom_error_response.value.error_code
response_code = lookup(custom_error_response.value, "response_code", null)
response_page_path = lookup(custom_error_response.value, "response_page_path", null)
}
}
当我运行 terraform init 时没问题,但是当我运行 terraform plan 时,对于我制作的 8 个子模块,它给了我 8 次相同的错误。
Error: Invalid value for module argument
on portal_distribution/main.tf line 32, in module "**mainmodule**":
32: custom_error_response = [
33: {
34: error_code = "403"
35: error_caching_min_ttl = "30"
36: response_code = "200"
37: response_page_path = "/${var.default_root_object}"
38: },
39: ]
The given value is not suitable for child module variable
"custom_error_response" defined at
.terraform/modules/**submodule**_portal.**mainmodule**/modules/aws-terraform-cloudfront_s3_origin/variables.tf:302,1-33:
element 0: string required.
如何修复错误?
【问题讨论】:
-
您的 `for_each = var.custom_error_response` 只能用于字符串集,不能用于映射集。因此,您能澄清一下您实际在做什么吗?
-
@Marcin 我实际上有 8 个云端分发模块是从一个名为 aws-terraform-cloudfront_s3_origin 的模块创建的。对于每个云端分发模块,response_page_path 是不同的,可以是任何类似 forexample.html 的内容。
标签: amazon-web-services syntax-error terraform terraform-provider-aws terraform-modules