自動執行您的管理任務 - AWS Snowball Edge 開發者指南

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

自動執行您的管理任務

您可以用 AWS OpsHub 來自動化您在 Snow Family 裝置上經常執行的操作工作。您可以建立任務來重複發生您可能想要在資源上執行的動作,例如重新啟動虛擬伺服器、停止與 Amazon EC2 相容的執行個體等。您提供的自動化文件可安全地執行作業工作,並在 AWS 資源上大量執行作業。您也可以排定一般 IT 工作流程。

注意

叢集不支援自動執行任務。

若要使用任務,必須先啟動 Amazon EC2 Systems Manager 服務。若要在您的 Snowball 邊緣啟動服務,請參閱在您的 Snowball 邊緣啟動服務

建立和開始任務

當您建立任務時,您可以指定任務應執行的資源類型,然後提供任務文件,當中包含執行任務的指示。任務文件是 YAML 或 JSON 格式。然後,您可以為任務提供必要的參數,並開始任務。

建立任務
  1. 在儀表板的 Launch tasks (啟動任務) 區段中,選擇 Get started (開始使用) 以開啟 Tasks (任務) 頁面。如果您已建立任務,它們會出現在 Tasks (任務) 下方。

  2. 選擇 Create task (建立任務) 並提供任務的詳細資訊。

  3. 對於 Name (名稱),輸入任務的唯一名稱。

    提示

    名稱長度必須介於 3 到 128 個字元之間。有效字元為 a-zA-Z0-9._-

  4. 您也可以選擇從 Target type-optional (目標類型-選用) 清單中選擇目標類型。這是您想要執行任務的資源類型。

    例如,您可以指定/AWS::EC2::Instance要在 Amazon EC2 相容執行個體上執行的任務,或/在所有資源類型上執行。

  5. 在 [內] 區段中,選擇 YAMLJSON,並提供執行工作的指令碼。您有兩個選項,YAML 或 JSON 格式。如需範例,請參閱 任務範例

  6. 選擇建立。接著您建立的任務會出現在 Tasks (任務) 頁面上。

開始任務
  1. 在儀表板的 Launch tasks (啟動任務) 區段中,選擇 Get started (開始使用) 以開啟 Tasks (任務) 頁面。您的任務會出現在 Tasks (任務) 下方。

  2. 選擇您的任務以開啟 Start task (開始任務) 頁面。

  3. 選擇 Simple execution (簡單執行) 在目標上執行。

    選擇 Rate control (速率控制) 以在多個目標上安全執行,並定義並行和錯誤臨界值。對於此選項,您可以在 Rate control (速率控制) 區段中提供額外的目標和錯誤臨界值資訊。

  4. 提供必要的輸入參數,然後選擇 Start taks (開始任務)

    任務的狀態為 Pending (待定),而當任務已順利執行時,會變更為 Success (成功)

任務範例

下列範例會重新啟動與 Amazon EC2 相容的執行個體。它需要兩個輸入參數:endpointinstance ID

YAML 範例

description: Restart EC2 instance schemaVersion: '0.3' parameters: Endpoint: type: String description: (Required) EC2 Service Endpoint URL Id: type: String description: (Required) Instance Id mainSteps: - name: restartInstance action: aws:executeScript description: Restart EC2 instance step inputs: Runtime: python3.7 Handler: restart_instance InputPayload: Endpoint: "{{ Endpoint }}" Id: "{{ Id }}" TimeoutSeconds: 30 Script: |- import boto3 import time def restart_instance(payload, context): ec2_endpoint = payload['Endpoint'] instance_id = payload['Id'] ec2 = boto3.resource('ec2', endpoint_url=ec2_endpoint) instance = ec2.Instance(instance_id) if instance.state['Name'] != 'stopped': instance.stop() instance.wait_until_stopped() instance.start() instance.wait_until_running() return {'InstanceState': instance.state}

JSON 範例

{ "description" : "Restart EC2 instance", "schemaVersion" : "0.3", "parameters" : { "Endpoint" : { "type" : "String", "description" : "(Required) EC2 Service Endpoint URL" }, "Id" : { "type" : "String", "description" : "(Required) Instance Id" } }, "mainSteps" : [ { "name" : "restartInstance", "action" : "aws:executeScript", "description" : "Restart EC2 instance step", "inputs" : { "Runtime" : "python3.7", "Handler" : "restart_instance", "InputPayload" : { "Endpoint" : "{{ Endpoint }}", "Id" : "{{ Id }}" }, "TimeoutSeconds" : 30, "Script" : "import boto3\nimport time\ndef restart_instance(payload, context):\n ec2_endpoint = payload['Endpoint']\n instance_id = payload['Id']\n ec2 = boto3.resource('ec2', endpoint_url=ec2_endpoint)\n instance = ec2.Instance(instance_id)\n if instance.state['Name'] != 'stopped':\n instance.stop()\n instance.wait_until_stopped()\n instance.start()\n instance.wait_until_running()\n return {'InstanceState': instance.state}" } } ] }

檢視任務的詳細資訊

您可以檢視管理任務的詳細資訊,例如執行任務所需的說明和參數。

若要檢視任務的詳細資訊
  1. 在儀表板的 Launch tasks (啟動任務) 區段中,選擇 Get started (開始使用) 以開啟 Tasks (任務) 頁面。

  2. Tasks (任務) 頁面上,找出並選擇您要查看詳細資訊的任務。

  3. 選擇 View details (檢視詳細資訊),然後選擇其中一個索引標籤以查看詳細資訊。例如,Parameters (參數) 索引標籤會顯示指令碼中的輸入參數。

刪除任務

請依照下列步驟刪除管理任務。

刪除任務
  1. 在儀表板的 Launch tasks (啟動任務) 區段中,選擇 Get started (開始使用) 以開啟 Tasks (任務) 頁面。

  2. 找出您要刪除的任務。選擇任務,然後選擇 Delete (刪除)