七牛服务器入门教程_教程:使用无服务器,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 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

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

Read on if you are:

如果您是,请继续阅读:

  • A serverless developer using Serverless framework who wants to check out ready-to use functions from the StackStorm Exchange open-source catalog,

    一位使用无服务器框架的无服务器开发人员,他希望从StackStorm Exchange开源目录中检出可立即使用的功能,

  • A StackStorm user who lives in AWS and misses the breadth of StackStorm Exchange integrations there.

    居住在AWS中的StackStorm用户,却错过了那里的StackStorm Exchange集成的广度。

  • Anyone who has 2 hours to follow-along and learn serverless with something more elaborate and real-life than a Hello-world example.

    任何有2个小时的跟踪学习和无服务器学习经验的人,都比Hello-world示例更精致,更真实。

If you only got 8 minutes to spare, skim the text and examples, spend an extra 30 seconds browsing StackStorm Exchange to see the potential, and bookmark this post to get back to it when you need it.

如果您仅剩下8分钟的时间,请浏览文本和示例,花30秒钟额外的时间浏览StackStorm Exchange以查看潜力,并为该帖子添加书签以在需要时返回它。

介绍 (Intro)

When I explored Serverless with Python, StepFunctions, and Web Front-end, one thing I missed is a catalog of reusable integrations. Something like 200 connectors for Azure Logic apps. Or 130 integration packs for StackStorm.

当我使用Python,StepFunctions和Web Front-end探索Serverless时 ,我错过的一件事是可重用集成的目录。 类似于200个用于Azure Logic应用程序的连接器 。 或130个用于StackStorm的集成包

When we need to wire in Slack, Jira, Stripe, or Nest, could we skip digging into their APIs and authentication intrinsics, and just grab a ready-to-use function?

当我们需要连接Slack,Jira,Stripe或Nest时,是否可以跳过对它们的API和身份验证内在函数的了解,而只是获取一个现成的功能?

Now we can do exactly that: StackStorm just announced a plugin for Serverless framework that turns integrations from StackStorm Exchange into AWS Lambda functions.

现在我们可以完全做到这一点:StackStorm刚刚宣布了一个用于无服务器框架的插件,该插件可以将StackStorm Exchange的集成转换为AWS Lambda函数。

In this tutorial, I’ll show how to use the plugin and Exchange integrations, in the context of building a serverless community on-boarding application from a ground-up. Let’s make this conversational and fun.

在本教程中,我将展示如何从头开始构建无服务器社区入门应用程序,以及如何使用插件和Exchange集成。 让我们变得有趣而有趣。

I assume no familiarity with either Serverless framework nor StackStorm. But you should know how to code, and be smart to compensate for mistakes and omissions I’ll inevitably make.

我假设对Serverless框架或StackStorm都不熟悉。 但是您应该知道如何编码,并且要聪明地弥补我不可避免的错误和遗漏。

We will be going slow, with excruciating details, thus it is going to be four episodes.

我们将走得很慢,细节令人毛骨悚然,因此将是四集。

In this first episode, I’ll set everything up and deploy my first StackStorm Exchange action.

在第一集中,我将设置所有内容并部署我的第一个StackStorm Exchange操作。

In the next episode, we’ll add more actions.

在下一集中,我们将添加更多操作。

In the third episode I’ll wire them together with a AWS StepFunction.

在第三集中,我将它们与AWS StepFunction连接在一起。

In the fourth episode we’ll add Web Front-end with the Reflection and Summary. Each episode will take about an hour to follow.

在第四集中,我们将在Web前端添加反射和摘要。 每个情节大约需要一个小时。

Ready? Let’s rock.

准备? 开始狂欢吧。

应用程序 (The application)

We will build a community on-boarding application. Actually, rebuilding from scratch the one we run at StackStorm. It’s like SlackIn with a multi-step customizable on-boarding workflow. The app presents a registration web-form, which passes new user info through API Gateway to the StepFunction workflow that carries on-boarding steps.

我们将构建一个社区入门应用程序。 实际上,从头开始重建我们在StackStorm上运行的那个。 就像SlackIn一样, 具有多步可定制的入职工作流程。 该应用程序显示一个注册网络表单,该表单通过API网关将新的用户信息传递给执行注册步骤的StepFunction工作流程。

In my case, the steps are 1) invite users to Slack 2) create contact record in ActiveCampaign CRM tool and 3) put a user record into internal DynamoDB table. Here is how it looks:

在我的情况下,步骤是1)邀请用户使用Slack; 2)在ActiveCampaign CRM工具中创建联系人记录;以及3)将用户记录放入内部DynamoDB表中。 外观如下:

You can find the previous implementation on github, or use it and join StackStorm community on Slack for questions about StackStorm Exchange integration.

您可以在github上找到以前的实现,或者使用它并在Slack上加入StackStorm社区 ,以获取有关StackStorm Exchange集成的问题。

准备好 (Getting Ready)

First, you’ll need AWS account, NodeJS, Docker, and Serverless framework. And Slack! As our first action will be inviting users to Slack.

首先,您需要AWS帐户,NodeJS,Docker和无服务器框架。 和松弛! 作为第一步,我们将邀请用户使用Slack。

  1. Make sure Node.JS is installed, and it’s version is 8.4.0 or higher.

    确保已安装Node.JS ,并且其版本为8.4.0或更高版本。

  2. Install Serverless framework, and setup AWS credentials for it following this guide.

    安装无服务器框架 ,并按照本指南为其设置AWS凭证。

  3. Install Docker. The plugin uses it for build environment to make the lambdas binary compatible to AWS execution environment no matter what OS you’re using for development. There is a way to make it work without Docker but don’t take chances.

    安装Docker 。 该插件将其用于构建环境,以使lambdas二进制文件与AWS执行环境兼容,无论您使用哪种OS进行开发。 有一种方法可以使它在没有Docker的情况下工作,但不要冒险。

  4. Slack! Our action will require admin access and will be using undocumented Slack API (docs here, pun intended) to invite new users. The easiest is to just go ahead create a new team. Takes 4 min. Slack won’t mind — they’ll show growth to their VC.

    松弛! 我们的操作将需要管理员访问权限,并将使用未记录的Slack API(此处为双关语)来邀请新用户。 最简单的方法就是继续建立新的团队 。 需要4分钟。 Slack不在乎-他们将向VC显示增长。

  5. Once the workspace is created, time to get an authentication token. Go to api.slack.com/custom-integrations/legacy-tokens. Fear not the “Legacy Warnings”: this tutorial will turn legacy before they do. Do what they say, get your token.

    一旦创建了工作空间,就该获取身份验证令牌了。 前往api.slack.com/custom-integrations/legacy-tokens 。 不要害怕“遗留警告”:本教程将先解决遗留问题。 照他们说的做,拿到你的令牌。

PRO TIP: Use this quick hack to get and use your own user’s auth token. Much faster, good for playing and debugging. But please never, never use it for production!

专业提示:使用此快速技巧来获取和使用您自己用户的身份验证令牌。 快得多,适合播放和调试。 但是请永远不要将其用于生产!

创建一个项目,添加第一个动作 (Create a project, add a first action)

Try sls --help to make sure that at least something works. sls is a shorthand for serverless, the Serverless framework CLI. Now put your coffee aside, time to create a project. Some folks like using templates that come with serverless: sls create --template. I prefer to start from scratch:

尝试使用sls --help来确保至少可以正常工作。 slsserverless (无服务器框架CLI)的简写。 现在,将咖啡放在一边,时间来创建一个项目。 一些人喜欢使用无服务器附带的模板: sls create --template 。 我更喜欢从头开始:

mkdir slack-signup-serverless-stormlesscd slack-signup-serverless-stormless
npm init
# Once you answer questions, the project is set up.

Next, install serverless-plugin-stackstorm, the one that plugs in the StackStorm Exchange actions.

接下来,安装serverless-plugin-stackstorm ,它可以插入StackStorm Exchange操作。

npm install --save-dev serverless-plugin-stackstorm

… and create a minimal serverless.ym file so that sls command will pick up the plugin:

…并创建一个最小的serverless.ym文件,以便sls命令选择该插件:

Now, create the first action. I’ll use a battle-tested Slack pack from StackStorm Exchange. Which action, you say? Ok, StackStorm Exchange is not smart enough yet to show pack’s action list, but sls stackstorm will rescue us.

现在, 创建第一个动作 。 我将使用来自StackStorm Exchange的经过战斗测试的Slack软件包 。 您说哪个动作? 好的,StackStorm Exchange还不够智能,无法显示pack的操作列表,但是sls stackstorm可以拯救我们。

sls stackstorm info --pack slack

sls stackstorm info --pack slack

Oh my! There’re so many! what are they? I guess I need a PR to print action description. Meantime, | grep admin will get us the one we need: slack.users.admin.invite. Let's query the action for it's parameters:

天啊! 有很多! 这些是什么? 我想我需要PR才能打印动作说明。 同时, | grep admin | grep admin将为我们提供所需的文件: slack.users.admin.invite 。 让我们查询动作的参数:

$ sls stackstorm info --action slack.users.admin.inviteslack.users.admin.invite ...... Send an invitation to join a Slack OrgParameters  attempts [integer]  ......... description is missing  channels [string]  .......... Channels to auto join.  email [string] (required) ... Email address to send invitation to.  first_name [string]  ........ Users first name  set_active [boolean]  ....... Should the user be active.  token [string]  ............. Slack API token.Config  action_token [string]  ...... Slack Action token.  admin [object]  ............. Admin-action specific settings.  post_message_action [object]   Post message action specific settings.  sensor [object]  ............ Sensor specific settings.

Awesome! We can see that there is only one required parameter, email, but I'll add first_name to stay conversational. The token can be passed as parameters, or as config. And if I choose to use config, my prior tribal knowledge hints that the admin [object] requires only admin_token. The very one I asked you to remember when you were setting up Slack workspace.

太棒了! 我们可以看到只有一个必需的参数email ,但是我将添加first_name来保持对话状态。 令牌可以作为参数或配置传递。 而且,如果我选择使用config,则我先前的部落知识表明admin [object]仅需要admin_token 。 我要您记住设置Slack工作区时的那个。

PRO TIP: While we are still polishing the plugin to expose all the Config details, you can find it out by exploring StackStorm Exchange pack config schema in config.schema.yaml file. For example, here is config.example.yaml for our Slack pack.

专业提示:在我们仍在完善插件以公开所有Config详细信息的同时,您可以通过探索config.schema.yaml文件中的StackStorm Exchange包配置架构来找到它。 例如,这是我们的Slack包的config.example.yaml

Now we have all we need to create the heart of any Serverless project: the serverless.yml. Here it comes:

现在,我们拥有创建任何无服务器项目的核心所需的全部内容: serverless.yml 。 它来了:

This is a good time to learn a bit of Serverless. Take a quick break to skim Core Concepts and bookmark Serverless.yml Reference.

这是学习一点无服务器的好时机。 快速浏览一下Core Concepts并为Serverless.yml Reference添加书签。

I threw in the events section in lines 9:12 so that we can invoke the function with REST call through AWS API Gateway endpoint. Serverless framework will do all the Gateway magic.

我将events部分放在9:12行中,以便我们可以通过AWS API Gateway端点通过REST调用来调用该函数。 无服务器框架将完成所有网关魔术。

Note that this default configuration instructs API Gateway to pass the REST POST call with POST body under the body key (details here. When we POST {"first_name": "Santa", "email": "santa@mad.russian.xmas.com"}, the event passed to the Lambda is:

请注意,此默认配置指示API网关通过body密钥下的POST正文传递REST POST调用( 详细信息此处 ,当我们POST {"first_name": "Santa", "email": "santa@mad.russian.xmas.com"} ,传递给Lambda的事件是:

..."body": {    "first_name": "Santa",     "email": "santa@mad.russian.xmas.com"}

Knowing the input data structure is important to map it to the action input parameters. It’s intuitive: input represents event parameter of AWS Lambda programming model(BTW should we call it event? Vote with a PR!).

知道输入数据结构对于将其映射到动作输入参数很重要。 直观: input代表AWS Lambda编程模型的 event参数(顺便说一句,我们应该将其称为event吗?请投票给PR!)。

Jinja is used to map the inputs; our JavaScript friends who're less familiar with this common Python tool find it intuitive enough in simple cases; and Stack-overflow is full of magic Jinja tricks.

Jinja用于映射输入; 不熟悉此通用Python工具JavaScript朋友会发现它在简单情况下足够直观; 而Stack-overflow充满了神奇的Jinja技巧。

In lines 16:17 of serverless.yml I map the two parameters from input body to desired action input parameters. Optionally, you can also form a function output from action results. I’ll keep it simple for now (line 20:22) and save more tricks for later.

中的行16:17 serverless.yml我从输入体的两个参数所需的操作输入参数映射。 您也可以选择从操作结果中形成函数输出。 我现在将其保持简单(第20:22行),并保存更多技巧以供日后使用。

To keep the config separate, I created a file env.yml that and put my config parameters in it:

为了使配置分开,我创建了一个文件env.yml并将其配置参数放入其中:

# ./env.yml# WARNING: Don't commit to Github :)slack:  admin_token: "xoxs-111111111111-..."  organization: "my-team"

Then I used it in serverless.yml like this: admin: ${file(env.yml):slack}. Note how this syntax puts the object from the key in the file to the key in serverless.yml.

然后,我用它在serverless.yml这样的: admin: ${file(env.yml):slack} 。 请注意,此语法如何将对象从文件中的密钥放入serverless.yml的密钥。

Ok, that’s it! The function is ready to fly to AWS with sls deploy. But I take it sloooow. Step by step. First, I’ll package it locally.

好的,就是这样! 该功能已准备就绪,可以通过sls deploy转到AWS。 但是我觉得这很糟糕 。 一步步。 首先,我将其打包在本地。

sls package

The very first time takes a long time as this is the time when the plugin installs its runtime dependencies. It pulls the Docker images from the Hub. It installs StackStorm runners — the code that knows how to run StackStorm Exchange packs. It pulls the slack pack from Exchange. It installs slack pack python dependencies. It does a lot of work for us, and it takes time. Good news: it's only the first time.

第一次需要很长时间,因为这是插件安装其运行时依赖项的时间。 它从集线器中提取Docker映像。 它安装了StackStorm运行程序—该代码知道如何运行StackStorm Exchange包。 它将slack包从Exchange中拉出来。 它安装了slack pack python依赖项。 它为我们做了很多工作,而且需要时间。 好消息:这只是第一次。

Oh, did I mention that you must be connected? Or do we assume internet connection a basic commodity like breathing air and electric power? At least before FCC repeals Network Neutrality? So yes, you need internet connection to live in the serverless world.

哦,我是否提到您必须连接? 还是我们假设互联网连接是呼吸空气和电力之类的基本商品? 至少在FCC取消网络中立性之前? 因此,是的,您需要Internet连接才能生活在无服务器的世界中。

Now let’s run this locally.

现在让我们在本地运行它。

sls stackstorm docker run --function InviteSlack --verbose \--data \'{"body": {"first_name": "Santa", "email": "santa@mad.russian.xmas.com"}}'

Local runs happen in a container — you’ll see Spin Docker container to run a function CLI output. It takes a bit longer, but ensures that the execution environment matches AWS lambda very closely, so better safe than sorry.

本地运行在容器中进行-您将看到Spin Docker container to run a function CLI输出。 这需要花费更长的时间,但是可以确保执行环境非常紧密地匹配AWS lambda,因此比遗憾更安全。

When I debug input and output parameter transformations, I may not want to call the actual function, like in case of Slack rate-limiting API. Use --passthrough parameter that tells the plugin to do the dry-run and skip the action invocation.

当我调试输入和输出参数转换时,我可能不想调用实际函数,例如使用Slack速率限制API的情况。 使用--passthrough参数,告诉插件进行空运行并跳过动作调用。

Now we are really ready. Let’s deploy the function to AWS, and run it “serverless”.

现在我们已经准备好了。 让我们将该功能部署到AWS上,然后“无服务器”运行它。

sls deploy

It will take some while — now it’s serverless (and honestly, our bundle is a bit bloated, patience! plugin developers are currently busy solving other problems, we will optimize it as soon as we can)

这将需要一些时间-现在它已经是无服务器的了(老实说,我们的捆绑包有点,肿,耐心!插件开发人员目前正在忙于解决其他问题,我们将尽快对其进行优化)

PRO TIP: if something goes wrong at this point, most likely something is not right with your AWS setup. Go back to “Getting Ready, step 2”. Read Serverless Installation doc. Google, Stack-overflow, Serverless Gitter channel or Forum.

专业提示:如果此时出现问题,则很可能是您的AWS设置不正确。 返回到“准备就绪,第2步”。 阅读无服务器安装文档。 Google,堆栈溢出,无服务器Gitter频道论坛

You might be curious to see how it looks in the AWS console. If the PRO in you is saying “no, you should stay cool and use CLI”, don’t listen. Go indulge yourself, open a browser and take a good look at your Lambda. While there, you might also inspect the API Gateway endpoint that sls created for you.

您可能想知道它在AWS控制台中的外观。 如果您的PRO在说“不,您应该保持冷静并使用CLI”,请不要听。 沉迷于自己,打开浏览器,仔细看一下Lambda。 在此处时,您可能还会检查sls为您创建的API Gateway端点。

But to test it, we’ll go back to terminal. Here is how to run your Lambda with sls:

但是要进行测试,我们将回到终端。 这是使用sls运行Lambda的方法:

sls invoke --function InviteSlack --log --data '{"body": {"first_name": "Santa", "email": "santa@mad.russian.xmas.com"}}'

Finally, let’s POST to the API endpoint. The endpoint was printed at the end of sls deploy and you should have taken notice, but it's OK if you didn't: you can always get it by typing sls info.

最后,让我们发布到API端点。 端点是在sls deploy结束时打印的,您应该已经注意到了,但是如果没有,也可以:通过输入sls info可以始终获得端点。

You curl lovers go ahead use it to POST; be sure to set Content-Type=application/json header. Me - I'll show off with httpie, aka CURL for humans:

curl恋人请继续使用它进行发布; 确保设置Content-Type=application/json标头。 我-我将使用httpie (也称为人类的CURL)进行炫耀:

# DON'T copy-paste! Use YOUR endpoint!
http --json POST  https://YOUR-ENDPOINT.amazonaws.com/dev/invite \email=test@example.com first_name=Dmitri

How did it go? Everything worked, at least for me. Let’s fire yet another most useful slscommand to check the CloudWatch logs:

怎么样了 一切正常,至少对我而言。 让我们启动另一个最有用的sls命令来检查CloudWatch日志:

sls logs --function InviteSlack

Success! And the end of the first episode. Enough for now: Christmas time is here, take it slow, enjoy!

成功! 第一集的结尾。 现在已经足够:圣诞节到了,慢慢来,尽情享受吧!

The code example so far is on Github at 1-add-first-action.

到目前为止的代码示例在Github上的1-add-first-action上

Episode 2: Adding more actions

第2集添加更多操作

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/tutorial-building-a-community-on-boarding-app-with-serverless-stepfunctions-and-stackstorm-b2f7cf2cc419/

七牛服务器入门教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值