Systems Manager 自动化操作参考 - Amazon Systems Manager
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

Systems Manager 自动化操作参考

此参考介绍可在自动化运行手册中指定的自动化操作。自动化是 Amazon Systems Manager 的一项功能。这些操作不能用于其他类型的 Systems Manager (SSM) 文档。有关其他 SSM 文档类型插件的信息,请参阅 命令文档插件参考

Systems Manager 自动化会运行自动化运行手册中定义的步骤。每步都与特定操作相关。此操作确定本步的输入、行为和输出。在运行手册的 mainSteps 部分定义步骤。

您无需指定操作或步骤的输出。由与本步关联的操作预先确定输出。当您在运行手册中指定步骤输入时,可以引用前面某步中的一个或多个输出。例如,您可以使 aws:runInstances 的输出可用于后续的 aws:runCommand 操作。还可以在运行手册的 Output 部分引用前面步骤中的输出。

重要

如果您运行使用 Amazon Identity and Access Management (IAM) 服务角色调用其他服务的自动化工作流程,请注意必须使用权限将该服务角色配置为调用这些服务。该要求适用于所有 Amazon 自动化运行手册(AWS-* 运行手册),例如 AWS-ConfigureS3BucketLoggingAWS-CreateDynamoDBBackupAWS-RestartEC2Instance 运行手册等。对于您创建的任何自定义自动化运行手册,如果这些文档使用调用其他服务的操作来调用其他 Amazon Web Services,则此要求同样适用。例如,如果使用 aws:executeAwsApiaws:createStackaws:copyImage 操作,则您必须配置具有权限的服务角色来调用这些服务。您可以将 IAM 内联策略添加到该角色,从而向其他 Amazon Web Services授予权限。有关更多信息,请参阅(可选)添加自动化内联策略或客户管理型策略来调用其他 Amazon Web Services

所有操作共享的属性

通用属性是位于所有操作中的参数或选项。一些选项定义了步骤的行为,例如,等待步骤完成的时间以及在步骤失败时采取的措施。以下属性是所有操作的通用属性。

description

您提供的描述运行手册或步骤目的的信息。

类型:字符串

必需:否

name

在运行手册的所有步骤名称中必须唯一的标识符。

类型:字符串

允许的模式:[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

将一个步骤指定为成功完成自动化的关键步骤。如果具有此分派的步骤失败,自动化会将自动化的最终状态报告为失败。仅当您在步骤中明确定义此属性时,才会计算该属性。如果 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 }}"