1. 前言
首先介绍下在本文出现的几个比较重要的概念:
template.yml 所描述的 Serverless 模型,是 Fun 所有功能的基石。template.yml 的正确性对后续能够顺利使用 Fun 的各项功能无疑是非常关键的。为了帮助用户更快速的修正 template.yml 中错误的描述,我们在 Fun 2.14.0 优化了语法校验的错误信息,可以达到更精准定位报错并修复的目的。
下面我们就通过一个示例,学习如何根据报错信息纠正 template.yml 中的错误语法描述。
备注:请确保 Fun 工具版本在 2.14.0+
2. 错误的 template.yml 示例
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
local-http-demo:
Type: 'Aliyun::Serverless::InvalidService'
Properties:
Description: 'local invoke demo'
nodejs8:
Type: 'Aliyun::Serverless::InvalidFunction'
Properties:
Handler: index.handler
CodeUri: nodejs8/
Description: 'http trigger demo with nodejs8!'
Events:
http-test:
Type: HTTP
Properties:
AuthType: ANONYMOUS
Method: ['GET', 'POST', 'PUT']
但遗憾的是,上面的示例描述有几处比较隐蔽的问题。下面,我们就动手实践,看如何发现上面示例中包含的错误语法描述并将其修正。
3. 语法错误发现并修复
3.1 修复第一个错误
我们可以执行 fun validate 对 tempalte.yml 进行校验(其他的命令比如 deploy、local 等也会隐式的执行 fun validate,保证在语法描述正确的情况下才执行指定的功能)。
当执行完 fun validate 后,会看到错误信息:
[
{
"keyword": "enum",
"dataPath": "/Resources/local-http-demo/Type",
"params": {
"allowedValues": [
"Aliyun::Serverless::Service",
"Aliyun::Serverless::TableStore",
"Aliyun::Serverless::Api",
"Aliyun::Serverless::Log",
"Aliyun::Serverless::CustomDomain",
"Aliyun::Serverless::MNSTopic"
]
},
"message": "should be equal to one of the allowed values"
}
]
错误信息会以 json 的格式输出,其中的 message 就是我们的本次的错误原因,dataPath 是遇到的错误在 template.yml 中的具体位置,params 中的内容是对 message 的进一步的补充。
再看下我们的描述(限于篇幅只列出了 template.yml 的部分内容):
Resources:
local-http-demo:
Type: 'Aliyun::Serverless::InvalidService'
很明显,我们描述的 Aliyun::Serverless::InvalidService
并不在上面允许的值中。
我们将其修改正确,也就是把 Aliyun::Serverless::InvalidService
修改为 Aliyun::Serverless::Service
即可。
Resources:
local-http-demo:
- Type: 'Aliyun::Serverless::InvalidService'
+ Type: 'Aliyun::Serverless::Service'
3.2 重新进行语法校验并修复
通常情况下,我们建议的方式是修复完一个问题,就重新使用 fun validate 进行校验。
我们将上面问题修复后,重新执行 fun validate 后,可以发现,依旧有报错:
[
{
"keyword": "const",
"dataPath": "/Resources/local-http-demo/nodejs8/Type",
"params": {
"allowedValue": "Aliyun::Serverless::Function"
},
"message": "should be equal to constant"
},
{
"keyword": "required",
"dataPath": "/Resources/local-http-demo/nodejs8/Properties",
"params": {
"missingProperty": "Runtime"
},
"message": "should have required property 'Runtime'"
},
{
"keyword": "additionalProperties",
"dataPath": "/Resources/local-http-demo/nodejs8/Events/http-test/Properties",
"params": {
"additionalProperty": "Method"
},
"message": "should NOT have additional properties"
},
{
"keyword": "required",
"dataPath": "/Resources/local-http-demo/nodejs8/Events/http-test/Properties",
"params": {
"missingProperty": "Methods"
},
"message": "should have required property 'Methods'"
}
]
这一次与上一次不同,同时出现了 4 个报错。但具体的修复步骤与上一步是一致的,即先找到第一个问题进行修复就可以了。
第一个报错如下:
{
"keyword": "const",
"dataPath": "/Resources/local-http-demo/nodejs8/Type",
"params": {
"allowedValue": "Aliyun::Serverless::Function"
},
"message": "should be equal to constant"
}
我们按照提示的将其修正(限于篇幅只列出了 template.yml 的部分内容):
Resources:
local-http-demo:
nodejs8:
- Type: 'Aliyun::Serverless::InvalidFunction'
+ Type: 'Aliyun::Serverless::Function'
这时候,我们可以重新执行下 fun validate,然后再挑选第一个进行修复,我们这里限于篇幅,就继续挑选下一个报错进行修复了。
{
"keyword": "required",
"dataPath": "/Resources/local-http-demo/nodejs8/Properties",
"params": {
"missingProperty": "Runtime"
},
"message": "should have required property 'Runtime'"
}
我们按照提示的将其修正(限于篇幅只列出了 template.yml 的部分内容):
Resources:
local-http-demo:
Type: 'Aliyun::Serverless::Service'
nodejs8:
Type: 'Aliyun::Serverless::Function'
Properties:
+ Runtime: nodejs8
最后的两个错误,我们可以一起看:
{
"keyword": "additionalProperties",
"dataPath": "/Resources/local-http-demo/nodejs8/Events/http-test/Properties",
"params": {
"additionalProperty": "Method"
},
"message": "should NOT have additional properties"
},
{
"keyword": "required",
"dataPath": "/Resources/local-http-demo/nodejs8/Events/http-test/Properties",
"params": {
"missingProperty": "Methods"
},
"message": "should have required property 'Methods'"
}
我们按照提示的将其修正(限于篇幅只列出了 template.yml 的部分内容):
Resources:
local-http-demo:
nodejs8:
Events:
http-test:
Properties:
AuthType: ANONYMOUS
- Method: ['GET', 'POST', 'PUT']
+ Methods: ['GET', 'POST', 'PUT']
当将所有的错误修复完成后,我们再重新执行 fun validate,即可发现,我们的所有错误都被修正啦。
接下来,我们就使用这个 template.yml 完成后续的 fun local、fun deploy 等功能就可以了。
3.3 修改记录汇总
我们将上面所有的改错记录记录如下:
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
local-http-demo:
- Type: 'Aliyun::Serverless::InvalidService'
+ Type: 'Aliyun::Serverless::Service'
Properties:
Description: 'local invoke demo'
nodejs8:
- Type: 'Aliyun::Serverless::InvalidFunction'
+ Type: 'Aliyun::Serverless::Function'
Properties:
Handler: index.handler
CodeUri: nodejs8/
Description: 'http trigger demo with nodejs8!'
+ Runtime: nodejs8
Events:
http-test:
Type: HTTP
Properties:
AuthType: ANONYMOUS
- Method: ['GET', 'POST', 'PUT']
+ Methods: ['GET', 'POST', 'PUT']
4. 总结
因此,Fun 提供了比较强大的语法校验功能,并通过精准的报错信息,让用户可以方便的将其修正。
本文作者:tanhe123
本文为云栖社区原创内容,未经允许不得转载。