You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS HTTP API自定义授权器无法通过Stage Variables按环境切换

核心诉求

我们已部署自定义授权器,希望通过Stage Variables按环境/阶段切换对应的授权器函数,例如dev阶段使用authorizer-dev函数,acpt阶段使用authorizer-acpt函数等,但无法实现该功能。

详细说明

我们在API Gateway中部署了HTTP API(非REST API),这自然限制了部分REST API具备的功能,但目前我们无需REST API的完整特性。

为支持不同环境,我们结合阶段(Stage)与Stage Variables,根据请求所属阶段切换下游集成(Lambda函数、基于K8s的服务等),即dev阶段的请求会指向dev环境部署的服务。所有配置通过嵌入AWS集成扩展的Open API Specification部署,示例如下:

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"

该配置运行正常。

我们为HTTP API(apigatewayv2)在API Gateway中配置了自定义授权器,目前所有阶段的请求都通过同一个授权器函数,这造成了瓶颈,因为我们需要按环境隔离授权器,它们需具备不同的验证逻辑与配置。

我们通过手动和CI/CD尝试了多种方式,试图为自定义授权器启用Stage Variables,但均未成功。使用单个授权器时正常,使用Stage Variables则所有请求返回500内部服务器错误,且无任何错误详情。

此问题与某已解决的REST API场景问题类似,但针对的是HTTP API。

已尝试方案

  • 在API Specification的authorizerUri中加入Stage Variables,示例如下:
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 Specification中用Stage Variables替换整个函数名,示例如下:
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'

我们无法理解为何此功能无法实现,也未找到相关文档说明原因。

内容的提问来源于stack exchange,提问作者Stuart Beard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 05:55:22