使用 yaml 云形成,我在 AWS::ApiGateway::RestApi 上配置了一个 lambda AWS::ApiGateway::Authorizer。我已经通过控制台成功测试了授权方和网关 API 方法,但是当我使用 curl 访问部署的 api 时,它失败并显示状态码 500:
< HTTP/2 500
< content-type: application/json
< content-length: 16
< date: Fri, 25 Mar 2022 09:45:06 GMT
< x-amzn-requestid: 07f7793c-406c-44c2-b184-dbbfd3f2a4fc
< x-amzn-errortype: AuthorizerConfigurationException
< x-amz-apigw-id: PiNNVFTTPHcFwyA=
< x-cache: Error from cloudfront
我在 API-Gateway-Execution 日志中看到了这个错误:
Execution failed due to configuration error: API Gateway does not have permission to assume the provided role arn:aws:iam::XXXXXXXXXXXX:role/auth
Execution failed due to configuration error: Authorizer error
据我了解,API Gateway 正在尝试执行 lambda 授权方,但没有信任关系允许它执行 lambda,尽管因此被授予所述信任:
lambdaAuthorizerFunctionRole:
Type: AWS::IAM::Role
Properties:
Path: /
RoleName: auth
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
# allow lambda and apigateway to assume the policies attached to this role
Service: [ lambda.amazonaws.com, apigateway.amazonaws.com ]
Action: sts:AssumeRole
Policies:
...
有人可以向我解释我在这里做错了什么吗?任何帮助将非常感激。
更新:
谢谢马尔辛。这解决了这个问题,让我摆脱了漫长而孤独的努力。
错误 API-Gateway-Execution 日志消息误导了我。它声明“API Gateway 无权承担所提供的角色”,在没有任何有意义的 AWS 文档的情况下,我的意思是它需要 AWS::IAM:: 中的 AssumeRolePolicyDocument 带来的信任关系:角色。
这个问题特别难以解决,因为我无法找到关于丢失权限的确切解释,因为它可能会在“AWS::ApiGateway::Authorizer”中引用丢失的“AuthorizerCredentials”。我试过在那里添加权限没有效果。在我的工作解决方案中,我完全删除了 AuthorizerCredentials。我不清楚这些凭据的实际用途,因为它们不是必需的。添加它们并没有伤害,但没有解决问题。
我认为缺少的权限也可能与授权者 lambda 的 AssumeRolePolicyDocument 未授予对 apigateway.amazonaws.com 的信任有关,如其他地方所述。但不是。通常情况下,只需要 lambda.amazonaws.com 作为委托人。
最后,我注意到 Cloud Formation 更新堆栈操作静默无法更新授权方这一事实加剧了这个问题:调试时,从 AWS::ApiGateway::RestApi 和 AWS::ApiGateway:: 中删除授权方方法的 AuthorizerId 属性默默地让授权者保持完好。也就是说,在更新删除授权人的情况下,我仍然遇到“API Gateway 无权承担提供的角色”错误。删除和重新创建堆栈克服了这种阻塞。
允许 API 调用您的函数的首选方式是通过AWS::Lambda::Permission,而不是 IAM 角色。因此,您可以创建如下内容:
permission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !GetAtt function.Arn
Action: lambda:InvokeFunction
Principal: apigateway.amazonaws.com
SourceArn: <arn:aws:execute-api:region>:<accountid>:<api_id>/authorizers/<authorizer_id>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句