amazon-web-services - Terraform 添加 aws_api_gateway_integration 响应 json
问题描述
我需要帮助将 post 方法配置为没有代理的 lambda 类型和 200 响应类型 application / json => Empty
这是我在 aws 的 Terraform 文件,我是 Terraform 的新手,如果有人可以帮助我,它只是缺少这种配置才能工作。
我在 terraform apply 中有错误响应
创建 API Gateway 集成时出错响应:NotFoundException:指定的集成标识符无效 创建 API Gateway 部署时出错:BadRequestException:没有为方法定义集成
resource "aws_dynamodb_table" "basic-dynamodb-table" {
name = "stone-test"
billing_mode = "PROVISIONED"
read_capacity = 20
write_capacity = 20
hash_key = "id"
attribute {
name = "id"
type = "N"
}
ttl {
attribute_name = "TimeToExist"
enabled = false
}
tags = {
Name = "dynamodb-table-1"
Environment = "dev"
}
}
resource "aws_iam_role_policy" "lambda_policy" {
name = "lambda_policy"
role = aws_iam_role.role_for_LDC.id
policy = file("policy.json")
}
resource "aws_iam_role" "role_for_LDC" {
name = "myrole"
assume_role_policy = file("assume_role_policy.json")
}
resource "aws_lambda_function" "stone_register2" {
filename = "stone_register.zip"
function_name = "stone_register3"
role = aws_iam_role.role_for_LDC.arn
handler = "stone_register.lambda_handler"
# The filebase64sha256() function is available in Terraform 0.11.12 and later
# For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
# source_code_hash = "${base64sha256(file("stone_register.zip"))}"
source_code_hash = filebase64sha256("stone_register.zip")
runtime = "python3.6"
environment {
variables = {
DB_TABLE_NAME = "stone-test"
}
}
}
resource "aws_lambda_function" "stone_delete2" {
filename = "stone_delete.zip"
function_name = "stone_delete3"
role = aws_iam_role.role_for_LDC.arn
handler = "stone_delete.lambda_handler"
# The filebase64sha256() function is available in Terraform 0.11.12 and later
# For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
# source_code_hash = "${base64sha256(file("stone_delete.zip"))}"
source_code_hash = filebase64sha256("stone_delete.zip")
runtime = "python3.6"
environment {
variables = {
DB_TABLE_NAME = "stone-test"
}
}
}
resource "aws_lambda_function" "stone_search2" {
filename = "stone_search.zip"
function_name = "stone_search3"
role = aws_iam_role.role_for_LDC.arn
handler = "stone_search.lambda_handler"
# The filebase64sha256() function is available in Terraform 0.11.12 and later
# For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
# source_code_hash = "${base64sha256(file("stone_search.zip"))}"
source_code_hash = filebase64sha256("stone_search.zip")
runtime = "python3.6"
environment {
variables = {
DB_TABLE_NAME = "stone-test"
}
}
}
resource "aws_lambda_function" "stone2" {
filename = "bot.zip"
function_name = "stone3"
role = aws_iam_role.role_for_LDC.arn
handler = "bot.lambda_handler"
# The filebase64sha256() function is available in Terraform 0.11.12 and later
# For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
# source_code_hash = "${base64sha256(file("bot.zip"))}"
source_code_hash = filebase64sha256("bot.zip")
runtime = "python3.6"
environment {
variables = {
DB_TABLE_NAME = "stone-test"
}
}
}
resource "aws_api_gateway_rest_api" "apiLambda" {
name = "myAPI"
}
resource "aws_api_gateway_resource" "Resource" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
parent_id = aws_api_gateway_rest_api.apiLambda.root_resource_id
path_part = "bot"
}
resource "aws_api_gateway_method" "Method" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
resource_id = aws_api_gateway_resource.Resource.id
http_method = "POST"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "lambdaInt" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
resource_id = aws_api_gateway_resource.Resource.id
http_method = aws_api_gateway_method.Method.http_method
integration_http_method = "POST"
type = "AWS"
uri = aws_lambda_function.stone2.invoke_arn
}
resource "aws_api_gateway_method_response" "response_200" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
resource_id = aws_api_gateway_resource.Resource.id
http_method = aws_api_gateway_method.Method.http_method
status_code = "200"
}
resource "aws_api_gateway_integration_response" "MyDemoIntegrationResponse" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
resource_id = aws_api_gateway_resource.Resource.id
http_method = aws_api_gateway_method.Method.http_method
status_code = aws_api_gateway_method_response.response_200.status_code
}
resource "aws_api_gateway_resource" "Resource2" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
parent_id = aws_api_gateway_rest_api.apiLambda.root_resource_id
path_part = "register"
}
resource "aws_api_gateway_method" "Method2" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
resource_id = aws_api_gateway_resource.Resource2.id
http_method = "POST"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "lambdaInt2" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
resource_id = aws_api_gateway_resource.Resource2.id
http_method = aws_api_gateway_method.Method2.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.stone_register2.invoke_arn
}
resource "aws_api_gateway_resource" "Resource3" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
parent_id = aws_api_gateway_rest_api.apiLambda.root_resource_id
path_part = "delete"
}
resource "aws_api_gateway_method" "Method3" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
resource_id = aws_api_gateway_resource.Resource3.id
http_method = "POST"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "lambdaInt3" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
resource_id = aws_api_gateway_resource.Resource3.id
http_method = aws_api_gateway_method.Method3.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.stone_delete2.invoke_arn
}
resource "aws_api_gateway_resource" "Resource4" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
parent_id = aws_api_gateway_rest_api.apiLambda.root_resource_id
path_part = "search"
}
resource "aws_api_gateway_method" "Method4" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
resource_id = aws_api_gateway_resource.Resource4.id
http_method = "POST"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "lambdaInt4" {
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
resource_id = aws_api_gateway_resource.Resource4.id
http_method = aws_api_gateway_method.Method4.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.stone_search2.invoke_arn
}
resource "aws_api_gateway_deployment" "apideploy" {
depends_on = [aws_api_gateway_integration.lambdaInt]
rest_api_id = aws_api_gateway_rest_api.apiLambda.id
stage_name = "Prod"
}
resource "aws_lambda_permission" "apigw" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.stone2.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.apiLambda.execution_arn}/Prod/POST/bot"
}
resource "aws_lambda_permission" "apigw2" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.stone_register2.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.apiLambda.execution_arn}/Prod/POST/register"
}
resource "aws_lambda_permission" "apigw3" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.stone_delete2.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.apiLambda.execution_arn}/Prod/POST/delete"
}
resource "aws_lambda_permission" "apigw4" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.stone_search2.function_name
principal = "apigateway.amazonaws.com"
source_arn = "${aws_api_gateway_rest_api.apiLambda.execution_arn}/Prod/POST/search"
}
output "base_url" {
value = aws_api_gateway_deployment.apideploy.invoke_url
}
解决方案
您已经在正确的路径上,您需要做的就是创建method_response 并使用它来创建integration_response
并更改集成类型
有一个完整的类型列表,它们可以做什么以及如何在文档中利用它们
我只调整了您共享的代码中的一些设置,如下所示:
...
resource "aws_api_gateway_integration" "integration" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.resource.id
http_method = aws_api_gateway_method.method.http_method
integration_http_method = "POST"
type = "AWS"
uri = aws_lambda_function.lambda.invoke_arn
}
....
resource "aws_api_gateway_method_response" "response_200" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.resource.id
http_method = aws_api_gateway_method.method.http_method
status_code = "200"
}
resource "aws_api_gateway_integration_response" "MyDemoIntegrationResponse" {
rest_api_id = aws_api_gateway_rest_api.api.id
resource_id = aws_api_gateway_resource.resource.id
http_method = aws_api_gateway_method.method.http_method
status_code = aws_api_gateway_method_response.response_200.status_code
}
当我从 AWS 控制台测试该功能时,我得到以下信息:
推荐阅读
- swift - 为什么动画视图从错误的位置开始?
- elasticsearch - Kibana:计算具有相同id字段的count(a)和count(a')之间的差异
- rust - 传递对函数参数的引用(书 v1)
- javascript - 如何更改按钮中的文本颜色?
- python - 使用 Django Paginator 的不需要/错误的 URL
- javascript - 如何使用 JavaScript 自动计算数据库中的值
- javascript - 如何从nodejs中的外部文件一起加载MongoDb连接和查询
- kubernetes - GKE 入口;将重写应用于特定服务
- python - 使用梯度下降算法时的初始值误差
- python - 为什么同一行代码偶尔会失败?