地形:预期属性值,由等号 ("=" ) 引入



我正在开发一个terraform脚本,该脚本使用HTTPS/POST请求将API网关集成到DynamoDB。基本目标是在DynamoDB表中插入API请求主体中传递的记录。我使用aws_api_gateway_integration来定义集成,并在那里传递请求模板,该模板应包括两个属性(BusinessUnitIdFrequency(。我的脚本看起来像这样-

resource aws_api_gateway_integration schedule_post_integration {
rest_api_id             = aws_api_gateway_rest_api.schedule.id
resource_id             = aws_api_gateway_resource.schedule_resource.id
http_method             = aws_api_gateway_method.schedule_post_method.http_method
type                    = "AWS"
integration_http_method = "POST"
uri                     = "arn:aws:apigateway:${var.target_region}:dynamodb:action/PutItem"
credentials             = aws_iam_role.schedule_api_dynamodb_role.arn
request_templates       = {
"application/json" = EOF
{
"TableName" : "${var.environment_id}-AccountService-NotesToDatalakeSchedule",
"Item" : {
"BusinessUnitId" : {
"N" : "$input.path('$.BusinessUnitId')"
},
"Frequency" : {
"N" : "$input.path('$.Frequency')"
}
}
}
}
}

我找不到一个正确的语法来指示Item应该如何在request_templates中传递。在部署过程中,它抛出错误-

2022-09-12T11:51:22.6623262Z ##[error][1m[31mError: [0m[0m[1mMissing attribute value[0m
2022-09-12T11:51:22.6626316Z ##[error][0m  on api-gateway.tf line 47, in resource "aws_api_gateway_integration" "schedule_post_integration":
2022-09-12T11:51:22.6628770Z ##[error]  35:   request_templates       = {
2022-09-12T11:51:22.6630845Z ##[error]  36:     "application/json" = EOF
2022-09-12T11:51:22.6632935Z ##[error]  37:     {
2022-09-12T11:51:22.6636136Z ##[error]  38:         "TableName" : "${var.environment_id}-AccountService-NotesToDatalakeSchedule",
2022-09-12T11:51:22.6638305Z ##[error]  39:         "Item" : {
2022-09-12T11:51:22.6642274Z ##[error]  40:             "BusinessUnitId" : {
2022-09-12T11:51:22.6645424Z ##[error]  41:                 "N" : "$input.path('$.BusinessUnitId')"
2022-09-12T11:51:22.6647529Z ##[error]  42:             },
2022-09-12T11:51:22.6649461Z ##[error]  43:             "Frequency" : {
2022-09-12T11:51:22.6651639Z ##[error]  44:                 "N" : "$input.path('$.Frequency')"
2022-09-12T11:51:22.6653642Z ##[error]  45:             }
2022-09-12T11:51:22.6655505Z ##[error]  46:         }
2022-09-12T11:51:22.6657282Z ##[error]  47:     }
2022-09-12T11:51:22.6659045Z ##[error]  48:   }
2022-09-12T11:51:22.6661277Z ##[error]Expected an attribute value, introduced by an equals sign ("=").

有人能帮我确定这里的问题吗?

看起来您正在尝试使用HCL模板表达式,但您忘记了两件关键的事情:

  1. 模板表达式以<< MARKER开头,您选择了EOF作为标记,但忘记了<<
  2. 你忘了标记";在一条线上">

固定版本:

resource aws_api_gateway_integration schedule_post_integration {
rest_api_id             = aws_api_gateway_rest_api.schedule.id
resource_id             = aws_api_gateway_resource.schedule_resource.id
http_method             = aws_api_gateway_method.schedule_post_method.http_method
type                    = "AWS"
integration_http_method = "POST"
uri                     = "arn:aws:apigateway:${var.target_region}:dynamodb:action/PutItem"
credentials             = aws_iam_role.schedule_api_dynamodb_role.arn
request_templates       = {
"application/json" = << EOF
{
"TableName" : "${var.environment_id}-AccountService-NotesToDatalakeSchedule",
"Item" : {
"BusinessUnitId" : {
"N" : "$input.path('$.BusinessUnitId')"
},
"Frequency" : {
"N" : "$input.path('$.Frequency')"
}
}
}
EOF
}
}

尽管关于如何使用多行模板表达式,其他现有答案是正确的,但通常最好使用jsonencode函数生成JSON字符串,而不是使用字符串模板,因为这样可以始终保证结果是有效的JSON,而无需考虑引号、转义和逗号的正确位置。

例如:

request_templates = {
"application/json" = jsonencode({
"TableName" = "${var.environment_id}-AccountService-NotesToDatalakeSchedule"
"Item" = {
"BusinessUnitId" = {
"N" = "$input.path('$.BusinessUnitId')"
}
"Frequency" = {
"N" = "$input.path('$.Frequency')"
}
}
})
}

这里jsonencode的自变量是构造对象值的Terraform表达式。jsonencode文档描述了函数如何将Terraform值转换为相应的JSON数据类型,根据该表,上面的示例应该生成与您在问题中试图编写的模板等效的JSON数据结构。

相关内容

  • 没有找到相关文章

最新更新