【发布时间】:2022-12-04 06:57:11
【问题描述】:
TLDR;
我们部署了一个自定义授权器,并希望使用阶段变量来切换每个阶段/环境使用哪个(授权器)函数。例如dev阶段将使用authorizer-dev函数,acpt阶段将使用authorizer-acpt等。我们无法让它发挥作用。
更多详情
我们有一个 HTTP API (不是REST)部署在 API 网关中。这可以理解地限制了使用 REST API 可以提供给我们的一些功能,但我们目前对 REST API 提供的全部功能没有强烈的需求。
为了支持不同的环境,我们使用阶段和阶段变量来根据请求进入的阶段切换下游集成(lambda 函数、基于 k8s 的服务等)。即,在开发阶段请求的任何内容都指向部署为开发环境的服务。这一切都是通过使用开放 API 规范进行部署的,该规范将阶段变量嵌入到 AWS 集成扩展中。例如;
payloadFormatVersion: "2.0"
passthroughBehavior: when_no_match
httpMethod: POST
type: aws_proxy
credentials: "arn:aws:iam::<aws-account>:role/<role-name>"
uri: "arn:aws:apigateway:<aws-region>:lambda:path/2015-03-31/functions/arn:aws:lambda:<aws-region>:<aws-account>:function:<function-name>-${stageVariables.environment}/invocations"
这非常有效。
我们在 API 网关中针对我们的 HTTP API (apigatewayv2) 配置了一个自定义授权方。目前,无论哪个阶段的所有请求都通过单个授权方功能,这对我们来说是一个夹点,因为我们需要在每个环境中隔离授权方,因为他们需要不同的验证和配置。
我们已经手动和通过 CICD 尝试了很多事情来在自定义授权者上启用阶段变量;但无法使其正常工作。使用单个授权方有效,使用阶段变量会导致所有请求返回 500 Internal Server Error,但没有任何地方出错的详细信息。
这个问题类似于the one asked here with accepted answer,但专门针对 HTTP API。
我们尝试过的事情
- 将阶段变量放入 API 规范中的 authorizerUri,例如;
x-amazon-apigateway-authorizer: authorizerCredentials: "arn:aws:iam::<aws-account>:role/<role-name>" authorizerPayloadFormatVersion: 2.0 authorizerUri: "arn:aws:apigateway:<aws-region>:lambda:path/2015-03-31/functions/arn:aws:lambda:<aws-region>:<aws-account>:function:<authorizer-name>-${stageVariables.environment}/invocations" authorizerResultTtlInSeconds: 0 identitySource: $request.header.Authorization type: request- 使用阶段变量替换控制台和 API 规范中的整个函数名称 authorizerUri,例如
authorizerUri: "arn:aws:apigateway:<aws-region>:lambda:path/2015-03-31/functions/arn:aws:lambda:<aws-region>:<aws-account>:function:${stageVariables.authorizerFunctionName}/invocations"- 使用 AWS CLI 手动更新授权方的 uri,例如;
aws apigatewayv2 update-authorizer --api-id <api-id> --authorizer-id <authorizer-id> --authorizer-uri 'arn:aws:apigateway:<aws-region>:lambda:path/2015-03-31/functions/arn:aws:lambda:<aws-region>:<aws-account>:function:<authorizer-name>-${stageVariables.environment}/invocations我们不知道为什么这不起作用,也找不到任何文档说明它为什么不起作用。
【问题讨论】:
标签: amazon-web-services aws-api-gateway openapi api-gateway lambda-authorizer