无服务器:使用自定义授权方部署端点 - 找不到名称错误
Serverless: Deploy Endpoint with Custom Authorizer - Name Not Found Error
我正在使用 Serverless framework deploying a function with a custom authorizer. The issue is similar to the 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": []
}
}
不确定这是否重要,但函数和自定义授权方在同一个项目中但文件夹不同(即授权方不是函数的 sub-folder)。
最后,如果我手动添加自定义授权方,一切正常。
感谢您的帮助或指导!
编辑:
经过进一步研究,我认为该问题与 s-function.json 的 'authorizer' 部分有关。这是在文件的 header 中,而不是在端点中。我没有看到此设置的示例,也不确定要在此处放置什么。任何想法或示例将不胜感激!
因此,我的 s-function.json 中的错误是由于自定义授权函数中的 'authorizer' 字段(不在实现授权代码的端点或函数中)。
我需要将其添加到 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": []
}
}
我还没有想出如何添加令牌验证表达式,而且我知道控制台反映区域和名称存在问题,但除此之外它工作完美。
我正在使用 Serverless framework deploying a function with a custom authorizer. The issue is similar to the 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": []
}
}
不确定这是否重要,但函数和自定义授权方在同一个项目中但文件夹不同(即授权方不是函数的 sub-folder)。
最后,如果我手动添加自定义授权方,一切正常。
感谢您的帮助或指导!
编辑: 经过进一步研究,我认为该问题与 s-function.json 的 'authorizer' 部分有关。这是在文件的 header 中,而不是在端点中。我没有看到此设置的示例,也不确定要在此处放置什么。任何想法或示例将不胜感激!
因此,我的 s-function.json 中的错误是由于自定义授权函数中的 'authorizer' 字段(不在实现授权代码的端点或函数中)。
我需要将其添加到 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": []
}
}
我还没有想出如何添加令牌验证表达式,而且我知道控制台反映区域和名称存在问题,但除此之外它工作完美。