serverless 构建_使用Serverless,StepFunctions和StackStorm Exchange构建社区注册应用程序-Episode…...

serverless 构建

by Dmitri Zimine

由Dmitri Zimine

使用Serverless,StepFunctions和StackStorm Exchange构建社区注册应用程序-第3集 (Building a community sign-up app with Serverless, StepFunctions, and StackStorm Exchange — Episode 3)

Build a real-world serverless application on AWS with Serverless framework and ready-to-use functions from StackStorm Exchange open-source catalog.

使用无服务器框架和StackStorm Exchange开源目录中的即用型功能,在AWS上构建真实的无服务器应用程序。

Episode One | Episode Two | Episode Three | Episode Four

第一集 | 第二集 | 第三集| 第四集

We are at Episode Three. Quick recap:

我们在第三集。 快速回顾:

  • In Episode One, I described the application we are building, walked you through setting up the development environment and creating a Serverless project, and showed how to build your first Lambda function from a StackStorm Exchange action with Serverless Framework.

    在第一集中 ,我描述了我们正在构建的应用程序,引导您完成了开发环境的创建并创建了无服务器项目,并展示了如何使用无服务器框架通过StackStorm Exchange操作构建第一个Lambda函数。

  • In Episode Two, we added more actions: one native Lambda to record user info to DynamoDB, and another one from StackStorm Exchange to make a call to ActiveCampaign CRM system. You learned more of serverless.yml syntax and practiced the development workflow with Lambda functions.

    在第二集中 ,我们添加了更多操作:一个本机Lambda将用户信息记录到DynamoDB,另一种Lambda从StackStorm Exchange调用ActiveCampaign CRM系统。 您了解了更多serverless.yml语法,并使用Lambda函数练习了开发工作流程。

In this third episode, I’ll show how to use AWS StepFunction to wire the actions into a workflow.

在第三集中,我将展示如何使用AWS StepFunction将操作连接到工作流中。

You can get the final code for this episode from GitHub.

您可以从GitHub获取此剧集的最终代码。

与StepFunction一起接线功能 (Wiring functions together with StepFunction)

Now that our building blocks — Lambda functions — are all lined up, it’s time to string them together. An AWS StepFunction will define the sequence of calls, maintain the state of the sign-up workflow, and carry the data between the steps. I’ll use theserverless-step-functions plugin from Serverless Champion @horike37, give him a heart:

现在我们的构建块(Lambda函数)都已对齐,是时候将它们串在一起了。 AWS StepFunction将定义调用顺序,维护注册工作流程的状态,并在步骤之间传递数据。 我将使用Serverless Champion @ horike37的serverless serverless-step-functions插件,给他一个心:

Let’s get busy. Install the plugin:

忙吧 安装插件:

npm install --save-dev serverless-step-functions

Add the plugin to the serverless.yml file:

将插件添加到serverless.yml文件:

plugins:  - serverless-plugin-stackstorm  - serverless-step-functions

The Step Function definition will require my accountID. As it is something I want to keep to myself, I add it to env.yml, which now looks like this:

步骤功能定义将需要我的accountID 。 因为这是我想保留的东西,所以将其添加到env.yml ,现在看起来像这样:

# ./env.yml# Don't commit to Github :)
slack:  admin_token: "xoxs-111111111111-..."  organization: "your-team"active_campaign:  url: "https://YOUR-COMPANY.api-us1.com"  api_key: "1234567a9012a12345z12aa2aaa..."private:  accountId: "000000000000"

Go back to serverless.yml and add the following two blocks:

返回serverless.yml并添加以下两个块:

# ./serverless.yml......custom:  private: ${file(env.yml):private}  stage: ${opt:stage, self:provider.stage}  region: ${opt:region, self:provider.region}
stepFunctions:  stateMachines:    signup:      events:        - http:            path: signup            method: POST            cors: true      definition: ${file(stepfunction.yml)}

In the custom block, I assigned the private object from the private key in env.yml. I also defined variables for stage and region so that the values are picked from CLI options, if provided, or default to the current AWS settings.

custom块中,我从env.ymlprivate分配了private对象。 我还定义了stageregion变量,以便从CLI选项(如果提供)中选择值,或者默认为当前AWS设置。

The stepFunctions block is here to define - you have already guessed - StepFunctions. Mine is called "signup".

stepFunctions块在这里定义-您已经猜到了-StepFunctions。 我的被​​称为“ signup ”。

The events section here is doing exactly what events sections do in function definitions: it configures an API Gateway endpoint for invoking StepFunction from outside of AWS. We'll use it later to call the back-end from a Web form.

这里的events部分完全执行函数定义中的events部分:它配置了API网关终端节点,以从AWS外部调用StepFunction。 稍后我们将使用它从Web表单调用后端。

The definition can be written as YAML right here in serverless.yml, but I prefer to include it from a separate file, keeping the logic separate from the configuration. Here it is:

definition可以在serverless.yml写为YAML,但是我更喜欢将其包含在单独的文件中,以使逻辑与配置分开。 这里是:

StepFunction definitions are written in Amazon States Language. The spec is short, well written and worth a read. Using YAML instead of JSON is a nice perk from the plugin — it reads better and allows comments. But if you want JSON — no problem, help yourself.

StepFunction定义使用Amazon States Language编写。 该规范简短,写得很好,值得一读。 使用YAML代替JSON是插件的一个不错的选择-它读起来更好,并允许注释。 但是,如果您想要JSON-没问题,请自助。

  • Resource refers to Lambda functions by ARN. I used the variables we defined earlier to construct the ARNs matching account ID, region, and stage with the function name: arn:aws:lambda:${self:custom.region}:${self:custom.private.accountId}:function:${self:service}-${self:custom.stage}-RecordDB

    Resource是指ARN的Lambda函数。 我使用我们先前定义的变量来构造与帐户ID,区域和阶段匹配的ARN,并带有函数名称: arn:aws:lambda:${self:custom.region}:${self:custom.private.accountId}:function:${self:service}-${self:custom.stage}-RecordDB

  • ResultPath is used to pass data between steps. By default, StepFunctions work on a "need-to-know" basis: the step downstream receives only the output from the step directly upstream. If you think it logical, think again: if only RecordDB receives the workflow input, how will RecordAC and InviteSlack get it? RecordDB may just return "200 OK", not email. Changing the code of functions to return their input would make them inappropriately intimate. The trick is to use ResultPath to write the function output under a function-specific key, like ResultPath: $results.RecordDB. This preserves initial workflow input in the step output for downstream Lambda steps, while appending the output of each Lambda. Like this:

    ResultPath用于在步骤之间传递数据。 默认情况下,StepFunctions在“需要了解”的基础上工作:下游步骤仅接收直接上游步骤的输出。 如果您认为这是合乎逻辑的,请再考虑一下:如果只有RecordDB接收工作流输入,RecordAC和InviteSlack将如何获得它? RecordDB可能只返回“ 200 OK”,而不是email 。 更改功能代码以返回其输入将使它们不恰当地亲密 。 诀窍是使用ResultPath在特定于函数的键下编写函数输出,例如ResultPath: $results.RecordDB 。 这将在下游Lambda步骤的步骤输出中保留初始工作流输入,同时追加每个Lambda的输出。 像这样:

{  "body": {    "name": "Vasili Terkin",    "email": "dmitri.zimine+terkin@gmail.com",    "first_name": "Vasili",    "last_name": "Terkin"  },  "results": {    "RecordDB": {      "statusCode": 200    },    "RecordAC": ...    ...  }}

To fully grasp it, read the “Input and Output” section in the spec. Ansd entertain youself with a video from “AWS Step Functions Tutorial” by Marcia Villalba.

要完全掌握它,请阅读规范中的“输入和输出”部分 。 Ansd提供了Marcia Villalba的 “ AWS Step Functions教程 ”中的视频来娱乐自己。

PRO TIP: I made the workflow sequential to demonstrate the data passing trick. It is more proper to run all three steps in parallel: it is faster and more resilient: the failure of one step will not prevent the other function invocations. Go ahead change the StepFunctions to parallel.

专家提示:我按顺序进行了工作流程,以演示数据传递技巧。 并行运行所有三个步骤更为合适:它更快且更具弹性:一个步骤的失败不会阻止其他功能的调用。 继续,将StepFunctions更改为parallel。

That is it. Time to try. Deploy, invoke, check the logs.

这就对了。 该尝试了。 部署,调用,检查日志。

You CURL fans know what to do with the new API Gateway endpoint for our StepFunction. If you forgot the endpoint, sls info to the rescue. I’ll show off again with httpie:

您的CURL粉丝知道如何使用我们的StepFunction的新API Gateway端点。 如果您忘记了端点,请向救援人员发送sls info 。 我将再次通过httpie炫耀 :

# DON'T COPY! Use YOUR ENDPOINT!
http POST https://YOUR.ENDPOINT.amazonaws.com/dev/signup \body:='{"email":"santa@mad.russian.xmas.com", "first_name":"Santa", "last_name": "Claus"}'

Ok, http or curl, either way it returns the StepFunction execution ARN so that we can check on it to see how our StepFunction is being executed. How do we check on it? I’m afraid you gotta open a browser and login to your AWS Console. If you want to use AWS CLI first, fine, don’t say I didn’t show you how:

好的, httpcurl ,都可以通过它返回StepFunction执行ARN的方式,以便我们可以对其进行检查以查看StepFunction的执行方式。 我们如何检查呢? 恐怕您必须打开浏览器并登录到AWS控制台。 如果您想首先使用AWS CLI,请不要说我没有向您展示如何:

aws stepfunctions describe-execution --execution-arn arn:aws:states:us-east-1:00000000000:execution:SignupStepFunctionsStateMac-seo9CrijATLU:cbeda709-e530-11e7-86d3-49cbe4261318 --output json{    "status": "FAILED",     "startDate": 1513738340.18,     "name": "cbeda709-e530-11e7-86d3-49cbe4261318",     "executionArn": "arn:aws:states:us-east-1:00000000000:execution:SignupStepFunctionsStateMac-seo9CrijATLU:cbeda709-e530-11e7-86d3-49cbe4261318",     "stateMachineArn": "arn:aws:states:us-east-1:00000000000:stateMachine:SignupStepFunctionsStateMac-seo9CrijATLU",     "stopDate": 1513738370.481,     "input": "{\"body\":{\"email\":\"santa@mad.russian.xmas.com\",\"first_name\":\"Santa\",\"last_name\":\"Claus\"}}"}

This is the output for an execution that failed because the RecordAC function timed out. Can you infer this from the output? The only valuable info here is FAILED. No kidding! I must say AWS don't give StepFunction the love it deserves. Not in CLI. If you think I missed something, check the CLI docs, find it and tell me.

这是执行失败的结果,因为RecordAC函数超时。 您可以从输出中推断出来吗? 唯一有价值的信息是FAILED 。 别开玩笑了! 我必须说AWS不会给StepFunction它应有的爱。 不在CLI中。 如果您认为我错过了什么,请查看CLI文档 ,找到并告诉我。

The most irritating part is that the CLI doesn’t tell me which step failed. They make me call the logs on every Lambda, one by one. Luckily I only have 3 functions, what if there were more?

最令人烦恼的部分是CLI不会告诉我哪一步失败了。 他们让我逐一调用每个Lambda上的日志。 幸运的是,我只有3个功能,如果还有更多功能呢?

Or, open a browser and hop on AWS Console.

或者,打开浏览器并跳至AWS Console。

Even there, debugging some Lambda failures, like timeouts, is tricky. StepFunction execution “Exception” report says "The cause could not be determined because Lambda did not return an error type."Go to the lambda logs to see what happened there.

即使在那儿,调试一些Lambda故障(例如超时)也很棘手。 StepFunction执行“异常”报告显示"The cause could not be determined because Lambda did not return an error type." 转到lambda日志以查看发生了什么。

There I find the line which I wish I saw in the StepFunction exception:

在那找到了希望在StepFunction异常中看到的行:

2017-12-20T04:21:44.298Z 4230a73b-e53d-11e7-be6b-bff82b9b3572 Task timed out after 6.00 seconds

PRO TIP: For debugging, invoke the StepFunction with sls invoke stepf: it creates the execution, waits for completion, and prints the output to the terminal. Three AWS CLI commands in one.

专业提示:要进行调试,请使用sls invoke stepf :它创建执行,等待完成,然后将输出打印到终端。 包含三个AWS CLI命令。

sls invoke stepf --name signup \--data  '{"body": {"email":"santa@mad.russian.xmas.com", "first_name":"Santa", "last_name": "Clause"}}'

Your StepFunction executions may work just fine — we already adjusted the timeouts. I took you on this debugging detour for a taste of StepFunction troubleshooting, admittedly a bit bitter. On the sweet side, once debugged, StepFunctions run reliably like Toyota cars.

您的StepFunction执行可能工作得很好-我们已经调整了超时。 我带着您绕过了这个调试弯路,以品尝一下StepFunction的故障排除功能,虽然有点苦。 从好的方面说,一旦调试,StepFunctions就可以像丰田汽车一样可靠地运行。

As a back-end developer, I am tempted to call it done here. But to make it a “complete example” we need one more thing. The Web front-end.

作为后端开发人员,我很想在这里称呼它。 但是要使其成为“完整的例子”,我们还需要做一件事。 Web前端。

Let’s call it a day and save the web part and conclusions for the next and final episode.

让我们称之为一天,保存下一部分和最后一集的网络部分和结论。

Episode 4: Adding Web Front-end, Reflection and Summary

第4集 :添加Web前端,反射和摘要

Hope this helped you learn something new, find something interesting, or provoked some good thoughts. Please share your thoughts in the comments here, or tweet me @dzimine.

希望这可以帮助您学习新知识,发现有趣事物或激发一些好的想法。 请在此处的评论中分享您的想法,或在推特上给我@dzimine 。

翻译自: https://www.freecodecamp.org/news/building-a-community-sign-up-app-with-serverless-stepfunctions-and-stackstorm-exchange-episode-6efb9c102b0a/

serverless 构建

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/395165.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

AlfaLaval公司采用低速通风技术冷却数据中心

日前,瑞典热交换专家AlfaLaval公司推出了遵循低速通风原则的一系列数据中心冷却解决方案,其方案需要大量的风扇,而使空气以相当慢的速度流动,取得了与计算机机房空调(CRAC)一样有效的制冷效果。 该公司表示…

java pdf 首页 缩略图_Java中将上传的文件首页生成缩略图(先将上传的文件转成pdf,然后将pdf转成jpg)...

1、首先将上传的非jpg,pdf格式的文件转成pdf,这个是采用OpenOffice进行转的,具体代码如下:private void officeToPdf(){OpenOfficeConnection connection new SocketOpenOfficeConnection(8100);try {connection.connect();} cat…

1.2 如何在visual studio 中建立C#程序

这一节简单介绍一下怎么在visual studio 2015中建立第一个C#程序,我使用的是2015版的visual studio,不同版本可能有一些差异,不过大体上是相同的,这些信息仅供新手参考,大牛请自动跳过。 首先双击visual studio 2015的…

javascript在html中的延迟与异步

1.相同点:延迟与异步都会同时加载script 2.不同点:延迟是script加载完成后,待HTML执行完毕后,才会接着执行script; 异步是script加载完成后,接着就执行该程序,HTML等到script完全执行完毕后&…

三星全速进军物联网 所有产品都将内置互联功能

韩媒报道,近日消息传出,三星电子的家电部门,当前生产的产品都将内建Wi-Fi。相关高层表示,此种做法可替更先进的功能预作准备,因为家电寿命较长,至少可用五年。目前为止,三星家电只有部分具备Wi-…

2d手机游戏开发_我的手机游戏如何在2周内获得365K应用商店下载(以及为什么我退出独立游戏开发公司…...

2d手机游戏开发by William Kwan关冠伟 我的手机游戏如何在2周内获得365K应用商店下载(以及为什么以后我退出独立游戏开发者) (How My Mobile Game Got 365K App Store Downloads in 2 Weeks (And Why I Quit Indie Game Dev Afterwards)) I’m not a successful game develope…

Python ValueError: IO operation on closed file

ValueError IO operation on closed file表示处理了已经被关闭的数据,在python 中 with语句的上下文会帮助处理,也就是说,当python的处理代码不对齐的时候会出现这种情况。例子如下: header那一行,突出,也就…

java面向字符的输入流_详细解读Java编程中面向字符的输入流

字符流是针对字符数据的特点进行过优化的,因而提供一些面向字符的有用特性,字符流的源或目标通常是文本文件。 Reader和Writer是java.io包中所有字符流的父类。由于它们都是抽象类,所以应使用它们的子类来创建实体对象,利用对象来…

任务信号量

在实际任务间的通信中,一个或多个任务发送一个信号量或者消息给另一个任务是比常见的,而一个任务给多个任务发送信号量和消息相对比较少。前面所讲的信号量和消息队列均是单独的内核对象,是独立于任务存在的。这两章要讲述的任务信号量和任务…

域名服务商GoDaddy第四季度扭亏为盈

2月18日消息,据财经网站MarketWatch报道,域名服务提供商GoDaddy周三公布了第四季度财报。公司期内利润与营收均好于预期,给出的营收指导亦符合预测水平。 财报显示,第四季度中GoDaddy营收同比增长14%,为4.254亿美元&am…

易于使用的人工智能_需求分析:如何使用这种易于启动的方法+一个案例研究...

易于使用的人工智能by Turgay elik由Turgayelik 需求分析:如何使用这种易于启动的方法一个案例研究 (Requirement Analysis: how to use this startup-friendly approach a case study) In our previous blog posts, we explained why we decided to develop the …

java writeboolean_Java DataOutputStream writeBoolean()方法(带示例)

DataOutputStream类writeBoolean()方法writeBoolean()方法在java.io包中可用。writeBoolean()方法用于将给定的布尔字节写入基本输出流,因此成功执行后写入的变量计数器为1。writeBoolean()方法是一种非静态方法,只能通过类对象访问,如果尝试…

【BZOJ4300】—绝世好题(二进制dp)

传送门 考虑到只需要bi&bi−1̸0b_i\&b_{i-1} \not0bi​&bi−1​̸​0 由于&\&&,我们考虑二进制下只需要一位不为0就可以了f[i]f[i]f[i]表示当前数下,第iii位不为0的最优长度 那就是需要枚举当前这个数所有位就…

爱立信与中国联通成功完成国内首个LTE三载波聚合大规模部署测试

近日,爱立信与中国联通网络技术研究院、联通四川省公司、联通成都市分公司、Qualcomm Incorporated子公司Qualcomm Technologies, Inc.合作成功实现了国内首个三载波聚合大规模部署和运行测试,下行单用户峰值速率达到375Mbps。该项目充分验证了载波聚合大…

七牛服务器入门教程_教程:使用无服务器,StepFunction和StackStorm构建社区的入门应用程序…...

七牛服务器入门教程by Dmitri Zimine由Dmitri Zimine 使用无服务器,StepFunction和StackStorm Exchange构建社区注册应用 (Building a community sign-up app with Serverless, StepFunctions, and StackStorm Exchange) Build a real-world serverless applicatio…

devexpress java_DevExpress使用心得一:换肤

最近要用到界面控件DevExpress。一句话:很好很强大,比起VS自带的winform界面,种类和花样要多了不少。然而,强力的功能带来了庞大的信息量,所以我打算通过一些小模块来和大家一起对它进行探讨和研究。今天先研究一下它的…

《低功耗蓝牙开发权威指南》——第3章低功耗蓝牙的体系结构

本节书摘来自华章社区《低功耗蓝牙开发权威指南》一书中的第3章低功耗蓝牙的体系结构,作者 (英)Robin Heydon,更多章节内容可以访问云栖社区“华章社区”公众号查看 第3章低功耗蓝牙的体系结构专注简单是我一直以来信奉的价值观。…

[福建集训2011][LOJ10111]相框

这题主要还是分类讨论欧拉回路 首先对于导线一端没有东西的新建一个节点 由于原图不一定连通所以需要用到并查集判断有多少个连通块 将一条导线连接的两个焊点连接 然后先对于只有一个连通块考虑 1.如果一个焊点是孤立点 它对于导线无影响跳过 2.如果一个焊点度数大于2 它必须被…

TJpgDec—轻量级JPEG解码器

TJpgDec—轻量级JPEG解码器 本文由乌合之众lym瞎编,欢迎转载blog.cnblogs.net/oloroso 下文中解码一词皆由decompression/decompress翻译而来。 TJpgDec是一个为小型嵌入式系统高度优化的创建JPEG图像的解码模块。它工作时占用的内存非常低,以便它可以集…

帮助中心 开源_对开源的贡献帮助我获得了Microsoft的实习机会。 这就是它可以为您提供帮助的方式。

帮助中心 开源“Accomplished X by implementing Y which led to Z.” “通过实现导致Z的Y来完成X。” When I interviewed for software engineering internships this past fall, my open source contributions helped me stand out from the crowd.去年秋天,当我…