我当时正在使用Terraform在AWS上创建一个结构。一切运行顺利,但现在我陷入困境,因为我必须使用API Gateway + Lambda实现API。我确实搜索了很多,但是文档没有帮助。 HashCorp教程仅显示简单的网址。
那么,如何在API Gateway + Lambda中的URL中传递查询字符串或路径参数?对于是否应该使用Proxy_Lambda还是仅使用Lambda,我也有疑问。
这是api网关代码:
resource "aws_api_gateway_rest_api" "accountant_api" {
name = "dev-test"
description = "test"
}
resource "aws_api_gateway_resource" "proxy" {
rest_api_id = "${aws_api_gateway_rest_api.accountant_api.id}"
parent_id = "${aws_api_gateway_rest_api.accountant_api.root_resource_id}"
path_part = "emailAccountant"
}
resource "aws_api_gateway_method" "proxy" {
rest_api_id = "${aws_api_gateway_rest_api.accountant_api.id}"
resource_id = "${aws_api_gateway_resource.proxy.id}"
http_method = "GET"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "lambda" {
rest_api_id = "${aws_api_gateway_rest_api.accountant_api.id}"
resource_id = "${aws_api_gateway_method.proxy.resource_id}"
http_method = "${aws_api_gateway_method.proxy.http_method}"
integration_http_method = "POST"
type = "AWS"
uri = "${aws_lambda_function.lambda_get_email_sent.invoke_arn}"
}
resource "aws_api_gateway_deployment" "example" {
depends_on = [
"aws_api_gateway_integration.lambda"
]
rest_api_id = "${aws_api_gateway_rest_api.accountant_api.id}"
stage_name = "dev"
}
Lambda权限:
resource "aws_lambda_permission" "apigw" {
statement_id = "AllowAPIGatewayInvoke"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.lambda_get_email_sent.arn}"
principal = "apigateway.amazonaws.com"
# The /*/* portion grants access from any method on any resource
# within the API Gateway "REST API".
source_arn = "${aws_api_gateway_deployment.example.execution_arn}/*/*"
}