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

1. 前言

首先介绍下在本文出现的几个比较重要的概念:html

函数计算(Function Compute): 函数计算是一个事件驱动的服务,经过函数计算,用户无需管理服务器等运行状况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。函数计算更多信息  参考

Fun: Fun 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算、API 网关、日志服务等资源。它经过一个资源配置文件(template.yml),协助您进行开发、构建、部署操做。Fun 的更多文档 参考node

template.yml: template.yml 用于定义 serverless 应用的模型。不管是使用 fun local 仍是 fun deploy 等功能,都是经过解析 tempalte.yml 的内容,构建出用户定义的云资源模型,进而实现本地云资源的运行调试以及发布等功能。template.yml 支持的规范文档能够参考git

template.yml 所描述的 Serverless 模型,是 Fun 全部功能的基石。template.yml 的正确性对后续可以顺利使用 Fun 的各项功能无疑是很是关键的。为了帮助用户更快速的修正 template.yml 中错误的描述,咱们在 Fun 2.14.0 优化了语法校验的错误信息,能够达到更精准定位报错并修复的目的。github

下面咱们就经过一个示例,学习如何根据报错信息纠正 template.yml 中的错误语法描述。json

备注:请确保 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 请求。less

但遗憾的是,上面的示例描述有几处比较隐蔽的问题。下面,咱们就动手实践,看如何发现上面示例中包含的错误语法描述并将其修正。函数

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 提供了比较强大的语法校验功能,并经过精准的报错信息,让用户能够方便的将其修正。



本文做者:tanhe123

阅读原文

本文为云栖社区原创内容,未经容许不得转载。

相关文章
相关标签/搜索