Systems Manager Automation 動作參考 - AWS Systems Manager

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

Systems Manager Automation 動作參考

此參考描述您可在 Automation Runbook 中指定的自動化動作。自動化是 AWS Systems Manager 的功能。這些動作無法用於其他類型的 Systems Manager (SSM) 文件中。如需其他 SSM 文件類型的外掛程式詳細資訊,請參閱 命令文件外掛程式參考

Systems Manager Automation 會執行 Automation Runbook 中定義的步驟。每個步驟皆與一個特定動作關聯。動作會決定輸入、行為和步驟的輸出。Runbook 的 mainSteps 章節中會定義步驟。

您不需要指定動作或步驟的輸出。輸出是由與步驟關聯的動作預先定義。在 Runbook 中指定步驟輸入時,您可以參考先前步驟的一個或多個輸出。例如,您可以讓 aws:runInstances 輸出用於後續的 aws:runCommand 動作。您也可以參考 Runbook Output 區段中先前步驟的輸出。

重要

如果您執行可使用 AWS Identity and Access Management (IAM) 服務角色叫用其他服務的自動化工作流程,請注意您必須為該服務角色設定可叫用這些服務的許可。此要求適用於所有 AWS Automation Runbook (AWS-* Runbook),例如 AWS-ConfigureS3BucketLoggingAWS-CreateDynamoDBBackupAWS-RestartEC2Instance Runbook 等。此要求也適用於您所建立會透過呼叫其他服務的動作來叫用其他 AWS 服務 的任何自訂自動化 Runbooks。例如,如果您使用 aws:executeAwsApiaws:createStackaws:copyImage 動作,為服務角色設定可叫用這些服務的許可。您可新增 IAM 內嵌政策到角色,以啟用其他 AWS 服務 的許可。如需更多詳細資訊,請參閱 (選用) 新增 Automation 內嵌政策或客戶管理政策以調用其他 AWS 服務

依所有動作共用的屬性

一般屬性是可在所有動作中找到的參數或選項。某些選項會定義步驟的行為,例如,等待步驟完成的時間,以及如果步驟失敗時該怎麼做。以下為所有動作中常見的屬性。

description

您提供用於描述執行手冊或步驟之目的的資訊。

類型:字串

必要:否

name

一種識別符,在 Runbook 的所有步驟名稱中必須獨一無二。

類型:字串

允許的模式:[a-zA-Z0-9_]+$

必要:是

action

步驟要執行的動作名稱。aws:runCommand – 在受管執行個體上執行命令 是您可以在此指定的動作範例。此文件提供所有可用動作的詳細資訊。

類型:字串

必要:是

maxAttempts

步驟在故障時應重試的次數。如果值大於 1,則在所有重試嘗試失敗之前,步驟不會視為失敗。預設值為 1。

類型:整數

必要:否

timeoutSeconds

步驟的逾時值。如果達到逾時且 maxAttempts 的值大於 1,則在嘗試完所有重試之前,步驟不會視為逾時。

類型:整數

必要:否

onFailure

指示自動化在失敗時應中止、繼續或前往不同的步驟。此選項的預設值為 abort。

類型:字串

有效值:Abort | Continue | step:step_name

必要:否

onCancel

指出當使用者取消自動化時,自動化應移至哪個步驟。自動化會執行取消工作流程最多兩分鐘。

類型:字串

有效值:Abort | step:step_name

必要:否

onCancel 屬性不支援移至下列動作:

  • aws:approve

  • aws:copyImage

  • aws:createImage

  • aws:createStack

  • aws:createTags

  • aws:loop

  • aws:pause

  • aws:runInstances

  • aws:sleep

isEnd

此選項會在特定步驟結束時停止自動化。如果步驟執行失敗或成功,自動化就會停止。預設值為 false。

類型:布林值

有效值:true | false

必要:否

nextStep

指定在成功完成步驟後要接著處理自動化中的哪個步驟。

類型:字串

必要:否

isCritical

指定某個步驟是成功完成自動化的關鍵。如果此指定步驟失敗,則自動化會將自動化的最終狀態回報為 Failed (失敗)。只有當您在步驟中明確定義此屬性時,才會評估此屬性。如果 onFailure 屬性在步驟中已設為 Continue,則該值預設為 false。否則,此選項的預設值為 true。

類型:布林值

有效值:true | false

必要:否

inputs

專屬於動作的屬性。

類型:映射

必要:是

範例

---
description: "Custom Automation Example" schemaVersion: '0.3' assumeRole: "{{ AutomationAssumeRole }}" parameters: AutomationAssumeRole: type: String description: "(Required) The ARN of the role that allows Automation to perform the actions on your behalf. If no role is specified, Systems Manager Automation uses your IAM permissions to run this runbook." default: '' InstanceId: type: String description: "(Required) The Instance Id whose root EBS volume you want to restore the latest Snapshot." default: '' mainSteps:
- name: getInstanceDetails action: aws:executeAwsApi onFailure: Abort inputs: Service: ec2 Api: DescribeInstances InstanceIds: - "{{ InstanceId }}" outputs: - Name: availabilityZone Selector: "$.Reservations[0].Instances[0].Placement.AvailabilityZone" Type: String - Name: rootDeviceName Selector: "$.Reservations[0].Instances[0].RootDeviceName" Type: String nextStep: getRootVolumeId - name: getRootVolumeId
action: aws:executeAwsApi
maxAttempts: 3 onFailure: Abort inputs: Service: ec2 Api: DescribeVolumes Filters: - Name: attachment.device Values: ["{{ getInstanceDetails.rootDeviceName }}"] - Name: attachment.instance-id Values: ["{{ InstanceId }}"] outputs: - Name: rootVolumeId Selector: "$.Volumes[0].VolumeId" Type: String nextStep: getSnapshotsByStartTime - name: getSnapshotsByStartTime action: aws:executeScript
timeoutSeconds: 45
onFailure: Abort inputs: Runtime: python3.8 Handler: getSnapshotsByStartTime InputPayload: rootVolumeId : "{{ getRootVolumeId.rootVolumeId }}" Script: |- def getSnapshotsByStartTime(events,context): import boto3 #Initialize client ec2 = boto3.client('ec2') rootVolumeId = events['rootVolumeId'] snapshotsQuery = ec2.describe_snapshots( Filters=[ { "Name": "volume-id", "Values": [rootVolumeId] } ] ) if not snapshotsQuery['Snapshots']: noSnapshotFoundString = "NoSnapshotFound" return { 'noSnapshotFound' : noSnapshotFoundString } else: jsonSnapshots = snapshotsQuery['Snapshots'] sortedSnapshots = sorted(jsonSnapshots, key=lambda k: k['StartTime'], reverse=True) latestSortedSnapshotId = sortedSnapshots[0]['SnapshotId'] return { 'latestSnapshotId' : latestSortedSnapshotId } outputs: - Name: Payload Selector: $.Payload Type: StringMap - Name: latestSnapshotId Selector: $.Payload.latestSnapshotId Type: String - Name: noSnapshotFound Selector: $.Payload.noSnapshotFound Type: String nextStep: branchFromResults - name: branchFromResults action: aws:branch onFailure: Abort
onCancel: step:startInstance inputs: Choices: - NextStep: createNewRootVolumeFromSnapshot Not: Variable: "{{ getSnapshotsByStartTime.noSnapshotFound }}" StringEquals: "NoSnapshotFound"
isEnd: true - name: createNewRootVolumeFromSnapshot action: aws:executeAwsApi onFailure: Abort inputs: Service: ec2 Api: CreateVolume AvailabilityZone: "{{ getInstanceDetails.availabilityZone }}" SnapshotId: "{{ getSnapshotsByStartTime.latestSnapshotId }}" outputs: - Name: newRootVolumeId Selector: "$.VolumeId" Type: String
nextStep: stopInstance - name: stopInstance action: aws:executeAwsApi onFailure: Abort inputs: Service: ec2 Api: StopInstances InstanceIds: - "{{ InstanceId }}" nextStep: verifyVolumeAvailability - name: verifyVolumeAvailability action: aws:waitForAwsResourceProperty timeoutSeconds: 120 inputs: Service: ec2 Api: DescribeVolumes VolumeIds: - "{{ createNewRootVolumeFromSnapshot.newRootVolumeId }}" PropertySelector: "$.Volumes[0].State" DesiredValues: - "available" nextStep: verifyInstanceStopped - name: verifyInstanceStopped action: aws:waitForAwsResourceProperty timeoutSeconds: 120 inputs: Service: ec2 Api: DescribeInstances InstanceIds: - "{{ InstanceId }}" PropertySelector: "$.Reservations[0].Instances[0].State.Name" DesiredValues: - "stopped" nextStep: detachRootVolume - name: detachRootVolume action: aws:executeAwsApi onFailure: Abort
isCritical: true inputs: Service: ec2 Api: DetachVolume VolumeId: "{{ getRootVolumeId.rootVolumeId }}" nextStep: verifyRootVolumeDetached - name: verifyRootVolumeDetached action: aws:waitForAwsResourceProperty timeoutSeconds: 30 inputs: Service: ec2 Api: DescribeVolumes VolumeIds: - "{{ getRootVolumeId.rootVolumeId }}" PropertySelector: "$.Volumes[0].State" DesiredValues: - "available" nextStep: attachNewRootVolume - name: attachNewRootVolume action: aws:executeAwsApi onFailure: Abort inputs: Service: ec2 Api: AttachVolume Device: "{{ getInstanceDetails.rootDeviceName }}" InstanceId: "{{ InstanceId }}" VolumeId: "{{ createNewRootVolumeFromSnapshot.newRootVolumeId }}" nextStep: verifyNewRootVolumeAttached - name: verifyNewRootVolumeAttached action: aws:waitForAwsResourceProperty timeoutSeconds: 30
inputs: Service: ec2 Api: DescribeVolumes VolumeIds: - "{{ createNewRootVolumeFromSnapshot.newRootVolumeId }}" PropertySelector: "$.Volumes[0].Attachments[0].State" DesiredValues: - "attached" nextStep: startInstance - name: startInstance action: aws:executeAwsApi onFailure: Abort inputs: Service: ec2 Api: StartInstances InstanceIds: - "{{ InstanceId }}"