无服务器:使用自定义授权程序部署端点 - 找不到名称错误

时间:2016-07-14 00:11:14

标签: amazon-web-services lambda serverless-framework

我正在使用Serverless框架使用自定义授权程序部署函数。该问题类似于one described in this thread,但没有详细解决方案。

基本上,我在文档中设置了自定义授权器和函数,但是当我部署函数(带端点)时,我得到的错误就是这个错误:

Serverless:   POST - exampleTest: Endpoint exampleTest~POST has an 'authorizerFunction' specified that does not exist in this project.  Make sure the function's 'authorizer' property is filled in

这是我的端点的s-function.json部分:

  "endpoints": [
{
  "path": "exampleTest",
  "method": "POST",
  "type": "AWS",
  "authorizationType": "custom",
  "authorizerFunction": "exampleAuth",
  "apiKeyRequired": false,
  "requestParameters": {},
  "requestTemplates": "$${apiRequestTemplate}",
  "responses": {
    "400": {
      "statusCode": "400"
    },
    "default": {
      "statusCode": "200",
      "responseParameters": {},
      "responseModels": {
        "application/json;charset=UTF-8": "Empty"
      },
      "responseTemplates": {
        "application/json;charset=UTF-8": ""
      }
    }
  }
}

以下是自定义授权程序的整个s-function.json:

    {
  "name": "exampleAuth",
  "runtime": "nodejs4.3",
  "description": "Custom Auth Function for API",
  "customName": false,
  "customRole": false,
  "handler": "handler.handler",
  "timeout": 30,
  "memorySize": 256,
  "authorizer": {},
  "custom": {
    "excludePatterns": []
  },
  "endpoints": [],
  "events": [],
  "environment": {
    "SERVERLESS_PROJECT": "${project}",
    "SERVERLESS_STAGE": "${stage}",
    "SERVERLESS_REGION": "${region}"
  },
  "vpc": {
    "securityGroupIds": [],
    "subnetIds": []
  }
}

不确定是否重要,但功能和自定义授权器在同一个项目中但是不同的文件夹(即授权者不是该功能的子文件夹)。

最后,如果我手动添加自定义授权程序,一切正常。

感谢您的帮助或指导!

修改 经过进一步的研究后,我认为这个问题与s-function.json的'authorizer'部分有关。这是在文件的标题中,而不是在端点中。我没有看到这个设置的样本,我不知道该放什么。任何想法或一个例子将不胜感激!

1 个答案:

答案 0 :(得分:3)

所以,我的s-function.json中的错误是由于'授权者'自定义授权功能中的字段(不在实现授权代码的端点或函数中)。

我需要将它添加到s-function.json:

"authorizer": {
        "type": "TOKEN",
        "identitySource": "method.request.header.Authorization",
        "authorizerResultTtlInSeconds": "300"
      },

对于遇到同样问题的人,这里是完成的自定义授权的s-function.json。

    {
  "name": "exampleAuth",
  "runtime": "nodejs4.3",
  "description": "Custom Auth Function for exampleAPI",
  "customName": false,
  "customRole": false,
  "handler": "handler.handler",
  "timeout": 30,
  "memorySize": 256,
  "authorizer": {
    "type": "TOKEN",
    "identitySource": "method.request.header.Authorization",
    "authorizerResultTtlInSeconds": "300"
  },
  "custom": {
    "excludePatterns": []
  },
  "endpoints": [],
  "events": [],
  "environment": {
    "SERVERLESS_PROJECT": "${project}",
    "SERVERLESS_STAGE": "${stage}",
    "SERVERLESS_REGION": "${region}"
  },
  "vpc": {
    "securityGroupIds": [],
    "subnetIds": []
  }
}

我还没有弄清楚如何添加令牌验证表达式,我知道控制台反映了区域和名称存在问题,但是它确实很完美。