问题描述
我正在使用 HTTP API 网关来触发 lambda 调用。当我使用邮递员提供的网址时,没有问题。当我从我的浏览器使用它时,它总是对网站图标发出第二个请求。
网关本身是否可以阻止网站图标请求到达 lambda?
我正在使用以下地形:
resource "aws_apigatewayv2_api" "retry_api" {
name = "${var.environment}_${var.cdp_domain}_retry_api"
protocol_type = "HTTP"
description = "To pass commands into the retry lambda."
target = module.retry-support.etl_lambda_arn
}
resource "aws_lambda_permission" "allow_retry_api" {
statement_id = "AllowAPIgatewayInvokation"
action = "lambda:InvokeFunction"
function_name = module.retry-support.etl_lambda_arn
principal = "apigateway.amazonaws.com"
source_arn = "${aws_apigatewayv2_api.retry_api.execution_arn}/*/*"
}
解决方法
这不会阻止来自浏览器的网站图标请求,也不会为这些请求调用 Lambda。
假设 API 端点是 /hello
并且 http 方法是 GET
,您可以限制 api-gateway
只为这个 URL 调用 lambda。格式应该是这样的。
arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/hello
所以 source_arn
中的 aws_lambda_permission
会变成这样
source_arn = "${aws_apigatewayv2_api.retry_api.execution_arn}/*/*/GET/hello"
答案假设现有的 / 最终分别用于 apiId
和 stage
。否则检查 ${aws_apigatewayv2_api.retry_api.execution_arn}
的值并进行相应的修改。
This 答案也有帮助。您只能为受支持的 openapi specification
提供 body 中的 path
。对于上述情况,调用名为 openapi specification
的 Lambda 的 HelloWorldFunction
的相关路径部分看起来像
"paths": {
"/hello": {
"get": {
"x-amazon-apigateway-integration": {
"httpMethod": "POST","type": "aws_proxy","uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloWorldFunction.Arn}/invocations"
},"payloadFormatVersion": "2.0"
},"responses": {} //Provide the expected response model
}
}
}
Here 是 OpenApi 规范的链接。
,通常,我会通过将 cloudfront 放在 API 网关前面来实现,并将 favicon.ico 映射到 S3 存储桶。
如果你真的想在 API GW 级别处理它,你可以创建一个 /favicon.ico 路由,并将集成设置为 MOCK - 这将返回一个特定的值,而不是调用 lambda(或任何其他后端).