要建立狀態機器,您可使用 AWS Step Functions 主控台。Step Functions 是一種 Web 服務,可讓您使用視覺化的工作流程,輕鬆地協調分散式應用程式與微型服務的元件。當執行狀態機器並達到任務狀態時,會叫用 Lambda 函數。
針對本專案,狀態機器會執行以下任務:
- 建立 AppStream 2.0 映像產生器。
- 獲取 AppStream 2.0 映像產生器和叢集的狀態。
- 傳送串流 URL。
- 獲取 AppStream 2.0 映像名稱。
- 建立 AppStream 叢集和堆疊,並建立叢集與堆疊的關聯。
- 停止 AppStream 2.0 叢集和映像產生器。
若要建立狀態機器,請執行以下步驟:
- 在以下網址開啟 AWS Step Functions 主控台:https://console.aws.amazon.com/states/。
- 執行以下其中一項:
- 如果您尚未建立任何狀態機器函數,則會顯示入門頁面。選擇入門,然後選擇狀態機器。
- 如果您已建立狀態機器函數,則在狀態機器頁面的右上角,選擇建立狀態機器。
- 在定義狀態機器頁面上,將從程式碼片段編寫保持選取的狀態。
- 在類型區段中,將標準保持選取的狀態。
- 在狀態機器定義區段中,刪除預留位置程式碼並在編輯視窗中貼上以下程式碼。
{
"StartAt": "Create Image Builder",
"States": {
"Create Image Builder": {
"Type": "Task",
"Resource":
"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_createimagebuilder",
"ResultPath": "$.type",
"Next": "Resource Created?"
},
"Resource Created?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.type",
"NumericEquals": -1,
"Next": "Creation Failed"
},
{
"Variable": "$.type",
"NumericEquals": 1,
"Next": "Sleep"
},
{
"Variable": "$.type",
"NumericEquals": 2,
"Next": "Sleep"
}
],
"Default": "Sleep"
},
"Sleep": {
"Type": "Wait",
"Seconds": 300,
"Next": "Get Status"
},
"Get Status": {
"Type": "Task",
"Resource":
"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_getstatus",
"Next": "Instance Status",
"ResultPath": "$.status"
},
"Instance Status": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.status",
"NumericEquals": -1,
"Next": "Creation Failed"
},
{
"Variable": "$.status",
"NumericEquals": 1,
"Next": "Send StreamingURL"
}
],
"Default": "Sleep"
},
"Send StreamingURL": {
"Type": "Task",
"Resource":
"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_sendstreamingurl",
"Next": "Split",
"ResultPath": "$.status"
},
"Split": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.type",
"NumericEquals": 1,
"Next": "Wait For Snapshot"
},
{
"Variable": "$.type",
"NumericEquals": 2,
"Next": "1 Hour Timer"
}
],
"Default": "1 Hour Timer"
},
"Wait For Snapshot": {
"Type": "Wait",
"Seconds": 1500,
"Next": "IB Status"
},
"IB Status": {
"Type": "Task",
"Resource":"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_getstatus",
"ResultPath": "$.status",
"Next": "Snapshotting?"
},
"Snapshotting?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.status",
"NumericEquals": 2,
"Next": "Get ImageName"
},
{
"Variable": "$.status",
"NumericEquals": 3,
"Next": "Get ImageName"
}
],
"Default": "Wait For Snapshot"
},
"Get ImageName": {
"Type": "Task",
"Resource":"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_getimagename",
"ResultPath": "$.imagename",
"Next": "Wait For ImageReady"
},
"Wait For ImageReady": {
"Type": "Wait",
"Seconds": 300,
"Next": "Image Status"
},
"Image Status": {
"Type": "Task",
"Resource":"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_getstatus",
"ResultPath": "$.status",
"Next": "ImageReady?"
},
"ImageReady?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.status",
"NumericEquals": 3,
"Next": "Create Fleet"
}
],
"Default": "Wait For ImageReady"
},
"Create Fleet": {
"Type": "Task",
"Resource":"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_createfleet",
"ResultPath": "$.type",
"Next": "Resource Created?"
},
"1 Hour Timer": {
"Type": "Wait",
"Seconds": 3600,
"Next": "StopResources"
},
"Creation Failed": {
"Type": "Fail",
"Cause": "Process Failed",
"Error": "Process Failed"
},
"StopResources": {
"Type": "Task",
"Resource":
"arn:aws:lambda:<REGION-CODE>:<AWS-ACCOUNT-ID>:function:examplecorp_eao_stopresources",
"End": true
}
}
}
6.使用您自己的值取代以下內容:
• 使用您的 AWS 帳號取代 <AWS-ACCOUNT-ID>。
• 使用您選擇的 AWS 區域取代 <REGION-CODE>。值必須全部為小寫。
7.選擇
下一步。
8.針對
名稱,鍵入
examplecorp_eao。
9.在
許可區段上,選取
選擇現有角色。
10.在
現有角色下拉式清單中,選擇
examplecorp_eao_role,即您在單元 2 中建立的 IAM 角色。
11.選擇
建立狀態機器。
12.在頁面頂部的
詳細資訊區段中,將會顯示狀態機器的
ARN。記下此 ARN。此 ARN 稍後在專案中需要用到,且格式為 arn:aws:states:<REGION-CODE>:<AWS_ACCOUNT-ID>:stateMachine:<STATE-MACHINE-NAME>