使用 API 密钥了解 AWS API 网关中的客户上​​下文

Knowing customer context in AWS API Gateway with API Key

我正在为我的 lambda 函数设置 API 网关并使用代理集成。我还为此 API 创建了使用计划和 API 密钥,并与两个客户 c1 和 c2 共享 API 密钥 k1 和 k2。

当客户使用密钥 k2 进行调用时,有没有办法从我的 lambda 函数中找到请求是由 c2 发出的?

我有一个 java 处理程序实现 RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent>

但不幸的是,我在 APIGatewayProxyRequestEvent

中找不到客户上下文

我会在方法请求和集成请求中将 API 密钥添加到 HTTP header,以便 Lambda 接收提供的 API 密钥,然后 Lambda 中的函数选择代码以运行 或根据读取的API键打印用户号

您不会在 API keys 事件中获得客户上下文。 API 密钥可以在多个 usage plans 之间共享,用于不同的 API。因此它们不能绑定到任何一个 API 部署。如果您想这样做,您将自行管理(例如,将 1 API 限制为 1 个客户,API 键映射到客户)。

我认为 AWS 不提供这个的原因之一是因为 API keys 纯粹是为了 usage plans。任何类型的 authentication/authorization(其中用户标识很重要)都需要使用其他技术来完成,例如 IAMCognito user poolscustom authorizer。请参阅 this

To include API methods in a usage plan, you must configure individual API methods to require an API key. For user authentication and authorization, don't use API keys. Use an IAM role, a Lambda authorizer, or an Amazon Cognito user pool.