本文以编写Hello World函数为例,介绍如何使用Funcraft管理函数资源。
本文介绍的内容后期将不再维护。如果您的函数计算资源是使用Funcraft管理的,建议您将资源迁移至Serverless Devs管理。
关于如何将函数计算的相关资源从Funcraft迁移到Serverless Devs进行管理的详细操作,请参见从Funcraft迁移到Serverless Devs。
由此带来的不便,敬请谅解!
关于如何使用Serverless Devs管理函数资源,请参见通过Serverless Devs管理函数
背景信息
Funcraft是函数计算提供的应用部署工具,可以帮助您便捷地管理函数计算、API网关及日志服务等资源,快速部署应用。关于Funcraft的更多信息,请参见功能概览。
前提条件
您已完成以下操作:
操作步骤
-
创建初始化模板。
-
执行以下命令初始化项目模板。
fun init -n demo -
根据提示选择一个项目模板。
? Select a template to init (Use arrow keys or type to search) > event-nodejs12 event-nodejs10 event-nodejs8 event-nodejs6 event-python3 event-python2.7 event-java8 event-php7.2 event-dotnetcore2.1 http-trigger-nodejs12 http-trigger-nodejs10 http-trigger-nodejs8 http-trigger-nodejs6 http-trigger-python3 http-trigger-python2.7 http-trigger-java8 (Move up and down to reveal more choices)项目模板类型如下:
-
以
event-为前缀的模板是普通的事件函数。 -
以
http-trigger为前缀的模板会默认为您创建HTTP触发器。HTTP触发器以Request、Response为入参,帮助您快速搭建Web应用。
本示例中以
event-nodejs10为例。Funcraft在执行命令的目录下,创建了一个demo的目录,并添加了两个文件,分别是index.js和template.yml。
-
index.js包含了函数的示例代码。
-
template.yml指定了函数资源的相关信息。
-
本示例为您创建了一个名为
demo的服务与一个名为demo的函数。 -
template.yml文件支持的配置项,请参见Serverless Application Model 。
-
-
-
-
可选:本地调试。
本地调试需要您本地安装Docker,详细信息,请参见安装Docker。如果您本地无法安装Docker,可以跳过此步骤,在云端调试。
-
执行以下命令进入Demo函数中。
cd demo -
本地执行以下命令调试函数。
fun local invoke demobegin pulling image registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-nodejs10:1.9.0, you can also use 'docker pull registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-nodejs10:1.9.0' to pull image by yours elf. 1.9.0: Pulling from aliyunfc/runtime-nodejs10 db0035920883: Pull complete a9ebd83b4a47: Pull complete 4cf624e5b311: Pull complete cb64b577f537: Pull complete 44dfa950eac4: Pull complete 1c201c55c971: Pull complete 1615c3bd235a: Pull complete ff2857aeb815: Pull complete 8b69b7e44eeb: Pull complete 58fa12757f79: Pull complete 56e625e73a04: Pull complete 6878ae707f92: Pull complete e20f27926b7a: Pull complete 3e8083f9143c: Pull complete feeb06770255: Pull complete 6fff1914e51c: Pull complete 27752f2ae2e9: Pull complete c7cacc29f7d0: Pull complete 22f59c8ad56a: Pull complete 1063f09fd6c4: Pull complete 64ac48d762d8: Pull complete Digest: sha256:c76b95f73ef01c90ffc8a5bca0e58c2c7b799afc547b3fb6d709dafb40a4729f Status: Downloaded newer image for registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-nodejs8:1.9.0 FC Invoke Start RequestId: b37217ac-62fc-4593-9878-4bfd7136d179 load code for handler:index.handler 2020-03-02T03:34:40.506Z b37217ac-62fc-4593-9878-4bfd7136d179 [verbose] hello world FC Invoke End RequestId: b37217ac-62fc-4593-9878-4bfd7136d179 hello world RequestId: b37217ac-62fc-4593-9878-4bfd7136d179 Billed Duration: 185 ms Memory Size: 1998 MB Max Memory Used: 57 MB说明第一次执行会拉取执行环境的镜像到本地,耗时较长。
-
-
部署到云端。
-
执行以下命令将函数部署到云端。
fun deploy -
部署过程中,输入
Y确认需要创建的资源。➜ demo fun deploy using template: template.yml using region: cn-hangzhou using accountId: ***********3360 using accessKeyId: ***********zA9Z using timeout: 100 Collecting your services information, in order to caculate devlopment changes... Resources Changes(Beta version! Only FC resources changes will be displayed): ┌──────────┬─────────────────────────────────┬────────┬─────────────┐ │ Resource │ ResourceType │ Action │ Property │ ├──────────┼─────────────────────────────────┼────────┼─────────────┤ │ │ │ │ Role │ │ demo │ Aliyun::Serverless::Service │ Delete ├─────────────┤ │ │ │ │ LogConfig │ ├──────────┼─────────────────────────────────┼────────┼─────────────┤ │ demo │ Aliyun::Serverless::Service │ Add │ Description │ ├──────────┼─────────────────────────────────┼────────┼─────────────┤ │ │ │ │ Handler │ │ demo │ Aliyun::Serverless::Function │ Modify ├─────────────┤ │ │ │ │ Runtime │ │ │ │ ├─────────────┤ │ │ │ │ CodeUri │ └──────────┴─────────────────────────────────┴────────┴─────────────┘ ? Please confirm to continue. Yes Waiting for service demo to be deployed... Waiting for function demo to be deployed... Waiting for packaging function demo code... The function demo has been packaged. A total of 1 file files were compressed and the final size was 302 B function demo deploy success service demo deploy success创建完成后,提示
service demo deploy success代表您的资源部署成功。
-
-
云端测试。
您可以登录函数计算控制台,查看是否部署成功。
登录函数计算控制台。
-
在左侧导航栏,单击服务及函数。
在顶部菜单栏,选择地域。
-
在服务列表页面,单击目标服务。
-
在函数管理页面,单击目标函数名称。
-
在目标函数详情页面的函数代码页签,单击测试函数,即可在函数计算控制台执行函数。
-
查看日志。
每次执行完毕,您可以在当前页面查看本次执行日志。如果需要查看历史执行日志,可以单击调用日志页签,查看历史日志时,需要您为函数配置日志仓库。详细信息,请参见配置日志。
进阶教程
完成以上教程后您可以根据使用场景查阅以下文档。