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.pswp.cn/news/395165.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395165.shtml
英文地址,請注明出處:http://en.pswp.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.去年秋天,當我…