amazon-web-services - Terraform 错误:为 cloudfront 模块运行 tf plan 时模块参数的值无效
问题描述
我有一个创建子模块的模块,每个子模块都有一个 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.
如何修复错误?
解决方案
你的变量的类型是一个列表。您正在进行地图查找的事实表明您正在尝试使用地图或其他东西。我的建议是:使用对象地图。
如果你需要它是一个var:
variable myinput {
type = map(list(object({
error_caching_min_ttl = string
error_code = number
response_code = number
response_page_path = string
})))
}
在我用来在我的机器上证明这一点的代码中,我没有使用 var。所以在哪里for_each
,只需将 var 放在那里,然后用您的值填充 var。解决方案是使用对象映射。我喜欢地图而不是列表,因为你可以给集合中的每个项目一个描述性的标题。这有助于在该州给出有意义的名字。
dynamic "custom_error_response" {
for_each = map(
"descriptiveTitleForThis", {
error_caching_min_ttl = "60"
error_code = 403
response_code = 11
response_page_path = "page1"
},
"551directToPage2", {
error_caching_min_ttl = "60"
error_code = 551
response_code = 10
response_page_path = "page2"
}
)
content {
error_caching_min_ttl = custom_error_response.value.error_caching_min_ttl
error_code = custom_error_response.value.error_code
response_code = custom_error_response.value.response_code
response_page_path = custom_error_response.value.response_page_path
}
}
如果你想要的话,我的完整代码如下:
provider aws {
profile = "myprofile"
region = "us-west-2"
}
resource "aws_s3_bucket" "b" {
bucket = "mybucket"
acl = "private"
tags = {
Name = "My bucket"
}
}
locals {
s3_origin_id = "myS3Origin"
}
resource "aws_cloudfront_distribution" "s3_distribution" {
enabled = true
origin {
domain_name = aws_s3_bucket.b.bucket_regional_domain_name
origin_id = local.s3_origin_id
}
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = local.s3_origin_id
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "allow-all"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
restrictions {
geo_restriction {
restriction_type = "whitelist"
locations = ["US", "CA", "GB", "DE"]
}
}
viewer_certificate {
cloudfront_default_certificate = true
}
dynamic "custom_error_response" {
for_each = map(
"descriptiveTitleForThis", {
error_caching_min_ttl = "60"
error_code = 403
response_code = 11
response_page_path = "page1"
},
"551directToPage2", {
error_caching_min_ttl = "60"
error_code = 551
response_code = 10
response_page_path = "page2"
},
"552directToPage3", {
error_caching_min_ttl = "60"
error_code = 552
response_code = 12
response_page_path = "page3"
},
"553directToPage4", {
error_caching_min_ttl = "60"
error_code = 553
response_code = 13
response_page_path = "page4"
},
)
content {
error_caching_min_ttl = custom_error_response.value.error_caching_min_ttl
error_code = custom_error_response.value.error_code
response_code = custom_error_response.value.response_code
response_page_path = custom_error_response.value.response_page_path
}
}
}
编辑:我忘了打印计划
Terraform will perform the following actions:
# aws_cloudfront_distribution.s3_distribution will be created
+ resource "aws_cloudfront_distribution" "s3_distribution" {
<..ommitted for brevity...>
+ custom_error_response {
+ error_caching_min_ttl = 60
+ error_code = 403
+ response_code = 11
+ response_page_path = "page1"
}
+ custom_error_response {
+ error_caching_min_ttl = 60
+ error_code = 551
+ response_code = 10
+ response_page_path = "page2"
}
+ custom_error_response {
+ error_caching_min_ttl = 60
+ error_code = 552
+ response_code = 12
+ response_page_path = "page3"
}
+ custom_error_response {
+ error_caching_min_ttl = 60
+ error_code = 553
+ response_code = 13
+ response_page_path = "page4"
}
<..ommitted for brevity...>
}
# aws_s3_bucket.b will be created
+ resource "aws_s3_bucket" "b" {
<..ommitted for brevity...>
}
Plan: 2 to add, 0 to change, 0 to destroy.
terraform version
Terraform v0.12.20
- 提供者.aws v3.9.0
推荐阅读
- asp.net-core - Azure Devops 中应用程序和配置设置的语法
- node.js - 如何从 Twitter API 中的 POST oauth/request_token 获得成功响应?
- javascript - 本地存储没有存储多个 slideToggle 的状态
- c - How to convert an int8_t array to int32_t array in C
- python - 如何读取和执行二进制文件?
- shell - 为什么按顺序标记文件夹中的文件会创建额外的文件?
- angular - Angular 7 不会开始导航,没有看到任何错误。关于什么是错的任何提示?
- angular - TS2300:重复标识符“路由器”
- amazon-web-services - 如何在 Lexresponse 中返回超链接文本?
- django - Django 用户是 Sentry 中的 SimpleLazyObject