开发函数计算的正确姿势 —— Fun validate 语法校验排错指南

本文涉及的产品
简介: 1. 前言 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。

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::ServiceAliyun::Serverless::TableStoreAliyun::Serverless::ApiAliyun::Serverless::LogAliyun::Serverless::CustomDomainAliyun::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 提供了比较强大的语法校验功能,并通过精准的报错信息,让用户可以方便的将其修正。

相关实践学习
基于函数计算一键部署掌上游戏机
本场景介绍如何使用阿里云计算服务命令快速搭建一个掌上游戏机。
建立 Serverless 思维
本课程包括: Serverless 应用引擎的概念, 为开发者带来的实际价值, 以及让您了解常见的 Serverless 架构模式
目录
相关文章
|
4月前
|
运维 Serverless 云计算
云上开发新范式:Serverless 的必然与应然 | 云栖深度对话
云上开发新范式:Serverless 的必然与应然 | 云栖深度对话
|
1月前
|
人工智能 Serverless API
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
|
1月前
|
小程序 IDE Serverless
【经验分享】支付宝小程序serverless云开发拓荒
【经验分享】支付宝小程序serverless云开发拓荒
73 0
|
2月前
|
人工智能 Serverless API
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
7397 132
|
2月前
|
人工智能 Serverless API
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
为了帮助用户高效率、低成本应对企业级复杂场景,函数计算团队正式推出 Stable Diffusion API Serverless 版解决方案,通过使用该方案,用户可以充分利用 Stable Diffusion +Serverless 技术优势快速开发上线 AI 绘画应用,期待为广大开发者 AI 绘画创业及变现提供思路。
87230 4
|
3月前
|
运维 Serverless API
Serverless云开发入门
Serverless云开发入门
|
4月前
|
运维 Serverless 云计算
飞天技术观|云上开发新范式:Serverless的必然与应然
在2022年云栖大会上宣布核心产品全面Serverless化之前,阿里云在Serverless领域布局已经有六七年积累了,一直在打磨产品,夯实基础;从去年开始,阿里云进入到了Serverless的第二阶段,一路突飞猛进,相对应地,阿里云Serverless产品数量和丰富度都在快速增长,业务规模也在快速增长。
441 1
|
1月前
|
人工智能 数据管理 Serverless
阿里云数据库走向Serverless与AI驱动的一站式数据平台具有重大意义和潜力
阿里云数据库走向Serverless与AI驱动的一站式数据平台具有重大意义和潜力
189 2
|
1月前
|
人工智能 运维 Cloud Native
、你如何看待阿里云数据库走向Serverless与AI驱动的一站式数据平台?
、你如何看待阿里云数据库走向Serverless与AI驱动的一站式数据平台?
127 2
|
2月前
|
JSON 安全 Serverless
在使用阿里云函数计算(FC)服务时,您可以通过自定义域名来访问部署好的云函数
在使用阿里云函数计算(FC)服务时,您可以通过自定义域名来访问部署好的云函数【1月更文挑战第23天】【1月更文挑战第112篇】
211 7

热门文章

最新文章

相关产品

  • 函数计算