内容简介:首先介绍下在本文出现的几个比较重要的概念:函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。函数计算更多信息Fun: Fun 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算、API 网关、日志服务等资源。它通过一个资源配置文件(template.yml),协助您进行开发、构建、部署操
1. 前言
首先介绍下在本文出现的几个比较重要的概念:
函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。函数计算更多信息 参考 。
Fun: Fun 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算、API 网关、日志服务等资源。它通过一个资源配置文件(template.yml),协助您进行开发、构建、部署操作。Fun 的更多文档 参考 。
template.yml: template.yml 用于定义 serverless 应用的模型。无论是使用 fun local 还是 fun deploy 等功能,都是通过解析 tempalte.yml 的内容,构建出用户定义的云资源模型,进而实现本地云资源的运行调试以及发布等功能。template.yml 支持的规范文档可以 参考 。
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']
在上面的示例中,我们原意是想要描述一个叫做 local-http-demo 的服务,并在服务下定义了一个名为 nodejs8 的函数,同时为该函数配置一个匿名的 HTTP 触发器,支持 GET、POST、PUT 的 HTTP 请求。
但遗憾的是,上面的示例描述有几处比较隐蔽的问题。下面,我们就动手实践,看如何发现上面示例中包含的错误语法描述并将其修正。
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 的进一步的补充。
按照我们刚才的解释,大概就可以明白, /Resources/local-http-demo/Type
这个资源定义出了问题,原因是这个值应该是 Aliyun::Serverless::Service
、 Aliyun::Serverless::TableStore
、 Aliyun::Serverless::Api
、 Aliyun::Serverless::Log
、 Aliyun::Serverless::CustomDomain
、 Aliyun::Serverless::MNSTopic
中的一个。
再看下我们的描述(限于篇幅只列出了 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"
}
这个和我们刚才修过的问题是一样的,意思就是 /Resources/local-http-demo/nodejs8/Type
的定义不对,这个值只被允许设定为 Aliyun::Serverless::Function
。
我们按照提示的将其修正(限于篇幅只列出了 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'"
}
这里 message 的含义是,缺少了必要的属性 Runtime
,检查下我们的规范文档对 Function 资源的描述
,可以发现, 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'"
}
这两个的错误含义是,我们在 /Resources/local-http-demo/nodejs8/Events/http-test/Properties
这个路径下,定义了一个不被允许的属性 Method
,并且同样是在这个路径下,缺少了一个必选的属性 Methods
。结合这两个报错,我们就可以判断出,我们将 Methods 错写成了 Method。
我们按照提示的将其修正(限于篇幅只列出了 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 的 规范文档 描述的比较详细了,而且在 Fun 的 repo 首页,我们给出了非常多的 示例文章 ,但依旧存在很大可能性会遇到各种各样的类似上面示例的书写错误。
因此,Fun 提供了比较强大的语法校验功能,并通过精准的报错信息,让用户可以方便的将其修正。
作者:tanhe123
原文链接
本文为云栖社区原创内容,未经允许不得转载。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Making Things See
Greg Borenstein / Make / 2012-2-3 / USD 39.99
Welcome to the Vision Revolution. With Microsoft's Kinect leading the way, you can now use 3D computer vision technology to build digital 3D models of people and objects that you can manipulate with g......一起来看看 《Making Things See》 这本书的介绍吧!