AWS Step Functions是一種無伺服器的協調器,可以透過視覺化的方式來建立及執行一連串的檢查點及應用程式(可以是Lambda或AWS Service),簡化步驟型的應用開發複雜度。
此步驟參考官方教學。
官方的教學文第一步是第一步是設計工作流程,用於描述您希望如何在客服中心處理支援票證。
登入AWS console,請搜尋並進入Step Function。
點選左側的menu,並選擇「Start machines」
進入條列頁後,選擇右上角的「Create state machine」
選擇「Author with code snippets」,我們由json來建立工作流程
Definition中使用官方提供的範例json
{
"Comment": "A simple AWS Step Functions state machine that automates a call center support session.",
"StartAt": "Open Case",
"States": {
"Open Case": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "Assign Case"
},
"Assign Case": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "Work on Case"
},
"Work on Case": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "Is Case Resolved"
},
"Is Case Resolved": {
"Type" : "Choice",
"Choices": [
{
"Variable": "$.Status",
"NumericEquals": 1,
"Next": "Close Case"
},
{
"Variable": "$.Status",
"NumericEquals": 0,
"Next": "Escalate Case"
}
]
},
"Close Case": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"End": true
},
"Escalate Case": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "Fail"
},
"Fail": {
"Type": "Fail",
"Cause": "Engage Tier 2 Support." }
}
}
輸入Step function name, 然後直接Create即可
搜尋IAM並進入
選擇Role
進入Role條列頁後,選擇左上角的「Create role」
記得要選擇Step Function
Permission Policies這邊使用預設的即可
輸入role name,這邊輸入step_functions_basic_execution,並點下Create
新增成功後,在role 條列頁中搜尋這邊輸入step_functions_basic_execution,就可以看到剛剛建立的role
點進去後,可以看到該role的ARN,請先複制它
回到Step Function後,選擇剛剛建立的Step function,並點選Edit
在Permission的部分,選擇剛剛建立的role,並點下Save
搜尋「Lambda function」並進入
進入條列頁後,選擇右上角的「Create function」
name輸入OpenCaseFunction
Runtime選擇nodejs
Permission記得選擇剛剛建立的Role, lambda_basic_execution
完成後點選Create function
function code使用官方的範例並Deploy
exports.handler = (event, context, callback) => {
// Create a support case using the input as the case ID, then return a confirmation message
var myCaseID = event.inputCaseID;
var myMessage = "Case " + myCaseID + ": opened...";
var result = {Case: myCaseID, Message: myMessage};
callback(null, result);
};
重複剛剛的動作,再建立以下幾個Lambda function
name輸入AssignCaseFunction
一樣使用nodejs
Permission一直選擇前面建立的lambda_basic_execution
function code一樣使用官方的範例並Deploy
exports.handler = (event, context, callback) => {
// Assign the support case and update the status message
var myCaseID = event.Case;
var myMessage = event.Message + "assigned...";
var result = {Case: myCaseID, Message: myMessage};
callback(null, result);
};
name輸入 WorkOnCaseFunction
一樣使用nodejs
Permission一直選擇前面建立的lambda_basic_execution
function code一樣使用官方的範例並Deploy
exports.handler = (event, context, callback) => {
// Generate a random number to determine whether the support case has been resolved, then return that value along with the updated message.
var min = 0;
var max = 1;
var myCaseStatus = Math.floor(Math.random() * (max - min + 1)) + min;
var myCaseID = event.Case;
var myMessage = event.Message;
if (myCaseStatus == 1) {
// Support case has been resolved
myMessage = myMessage + "resolved...";
} else if (myCaseStatus == 0) {
// Support case is still open
myMessage = myMessage + "unresolved...";
}
var result = {Case: myCaseID, Status : myCaseStatus, Message: myMessage};
callback(null, result);
};
name輸入 CloseCaseFunction
一樣使用nodejs
Permission一直選擇前面建立的lambda_basic_execution
function code一樣使用官方的範例並Deploy
exports.handler = (event, context, callback) => {
// Close the support case
var myCaseStatus = event.Status;
var myCaseID = event.Case;
var myMessage = event.Message + "closed.";
var result = {Case: myCaseID, Status : myCaseStatus, Message: myMessage};
callback(null, result);
};
name輸入 EscalateCaseFunction
一樣使用nodejs
Permission一直選擇前面建立的lambda_basic_execution
function code一樣使用官方的範例並Deploy
exports.handler = (event, context, callback) => {
// Escalate the support case
var myCaseID = event.Case;
var myCaseStatus = event.Status;
var myMessage = event.Message + "escalating.";
var result = {Case: myCaseID, Status : myCaseStatus, Message: myMessage};
callback(null, result);
};
在都完以後,理論上你會看到5個Lambda function
回到Step Function後,選擇剛剛建立的CallCenterStateMachine,並選擇Edit
將Definition code中的arn都取代為剛剛建立Lambda arn,一共會有5個,都完成後記得點下Save
進入剛剛建立的state machine後,選擇右上角的「Start execution」
input中使用官方的範例
{
"inputCaseID": "001"
}
等待幾秒後,就會更新狀態,並且可以看到input & output相關資訊
在下面也有圖形化的方式呈現各step執行的結果
每個step也可以再點進去看詳細資訊
最下面也有執行的log
每月個有免費的4,000 個狀態轉換,若超過的話,
每 1,000 次狀態轉換價格為0.025 USD。
詳細資訊可以參考官方。
https://aws.amazon.com/tw/getting-started/hands-on/create-a-serverless-workflow-step-functions-lambda/
https://aws.amazon.com/tw/step-functions/