在无服务器框架中设置AWS API网关自定义授权者

Sha*_*era 0 amazon-web-services aws-lambda aws-api-gateway serverless-framework

我需要将AWS API网关自定义授权者添加到Lambda函数中。目前,我已经为每个端点添加了授权者。如下serverless.yml

无服务器

service: test-service

provider:
    name: aws
    runtime: nodejs6.10
    stage: dev
    region: us-east-1

functions:
    bff:
        handler: app.handler
        events:
            - http:
                path: /home
                method: get
                cors: true
                authorizer :
                    arn: arn:aws:lambda:us-east-1:xxxxxx:function:token-verifier
                    resultTtlInSeconds: 0
                    identitySource: method.request.header.Authorization
                    identityValidationExpression: '.*'
Run Code Online (Sandbox Code Playgroud)

如何将自定义授权者添加到整个lambda函数,而不是分别添加到每个端点?

das*_*mug 6

您会混淆AWS API Gateway和AWS Lambda之间的界限。这不是你的错。无服务器框架是如此出色,几乎使这两件事变得模糊了。


严格来说,AWS lambda函数不要需要定制的授权人。

授权人用于保护API网关终端节点,而不用于AWS Lambda函数。

因此,您需要为需要授权的每个端点定义授权者。


如果serverless.yml不通过多次重复授权者定义来简化您的内容,则可以定义一次,然后在端点中引用它。

service: test-service

custom:
    authorizer:
        arn: arn:aws:lambda:us-east-1:xxxxxx:function:token-verifier
        resultTtlInSeconds: 0
        identitySource: method.request.header.Authorization
        identityValidationExpression: '.*'

provider:
    name: aws
    runtime: nodejs6.10
    stage: dev
    region: us-east-1

functions:
    bff:
        handler: app.handler
        events:
            - http:
                path: /home
                method: get
                cors: true
                authorizer: ${self:custom.authorizer}
Run Code Online (Sandbox Code Playgroud)