

• AWS Systems Manager CloudWatch 控制面板在 2026 年 4 月 30 日之后将不再可用。客户可以像现在一样继续使用 Amazon CloudWatch 控制台来查看、创建和管理其 Amazon CloudWatch 控制面板。有关更多信息，请参阅 [Amazon CloudWatch 控制面板文档](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)。

# Systems Manager 自动化操作参考
<a name="automation-actions"></a>

此参考介绍可在自动化运行手册中指定的自动化操作。Automation 是 AWS Systems Manager 中的一项工具。这些操作不能用于其他类型的 Systems Manager (SSM) 文档。有关其他 SSM 文档类型插件的信息，请参阅 [命令文档插件参考](documents-command-ssm-plugin-reference.md)。

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

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

**重要**  
如果您运行使用 AWS Identity and Access Management (IAM) 服务角色调用其他服务的自动化工作流程，请注意必须使用权限将该服务角色配置为调用这些服务。该要求适用于所有 AWS 自动化运行手册（`AWS-*` 运行手册），例如 `AWS-ConfigureS3BucketLogging`、`AWS-CreateDynamoDBBackup` 和 `AWS-RestartEC2Instance` 运行手册等。对于您创建的任何自定义自动化运行手册，如果这些文档使用调用其他服务的操作来调用其他 AWS 服务，则此要求同样适用。例如，如果使用 `aws:executeAwsApi`、`aws:createStack` 或 `aws:copyImage` 操作，则您必须配置具有权限的服务角色来调用这些服务。您可以将 IAM 内联策略添加到该角色，从而向其他 AWS 服务授予权限。有关更多信息，请参阅 [（可选）添加自动化内联策略或客户管理型策略来调用其他 AWS 服务](automation-setup-iam.md#add-inline-policy)。

**Topics**
+ [所有操作共享的属性](#automation-common)
+ [`aws:approve` – 暂停自动化以进行手动批准](automation-action-approve.md)
+ [`aws:assertAwsResourceProperty` - 断言 AWS 资源状态或事件状态](automation-action-assertAwsResourceProperty.md)
+ [`aws:branch` – 运行条件自动化步骤](automation-action-branch.md)
+ [`aws:changeInstanceState` – 更改或声明实例状态](automation-action-changestate.md)
+ [`aws:copyImage` – 复制或加密Amazon Machine Image](automation-action-copyimage.md)
+ [`aws:createImage` – 创建亚马逊机器映像](automation-action-create.md)
+ [`aws:createStack` – 创建 CloudFormation 堆栈。](automation-action-createstack.md)
+ [`aws:createTags` - 为 AWS 资源创建标签](automation-action-createtag.md)
+ [`aws:deleteImage` – 删除亚马逊机器映像](automation-action-delete.md)
+ [`aws:deleteStack` - 删除 CloudFormation 堆栈。](automation-action-deletestack.md)
+ [`aws:executeAutomation` - 运行另一个自动化](automation-action-executeAutomation.md)
+ [`aws:executeAwsApi` - 调用并运行 AWS API 操作](automation-action-executeAwsApi.md)
+ [`aws:executeScript` - 运行脚本](automation-action-executeScript.md)
+ [`aws:executeStateMachine` - 运行 AWS Step Functions 状态机。](automation-action-executeStateMachine.md)
+ [`aws:invokeWebhook` – 调用 Automation Webhook 集成](invoke-webhook.md)
+ [`aws:invokeLambdaFunction` – 调用 AWS Lambda 函数](automation-action-lamb.md)
+ [`aws:loop` – 迭代自动化中的步骤](automation-action-loop.md)
+ [`aws:pause` - 暂停自动化](automation-action-pause.md)
+ [`aws:runCommand` - 在托管实例上运行命令](automation-action-runcommand.md)
+ [`aws:runInstances` – 启动 Amazon EC2 实例](automation-action-runinstance.md)
+ [`aws:sleep` - 延迟自动化](automation-action-sleep.md)
+ [`aws:updateVariable` – 更新运行手册变量的值](automation-action-update-variable.md)
+ [`aws:waitForAwsResourceProperty` - 等待 AWS 资源属性](automation-action-waitForAwsResourceProperty.md)
+ [自动化系统变量](automation-variables.md)

## 所有操作共享的属性
<a name="automation-common"></a>

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

[description](#descriptProp)  
您提供的描述运行手册或步骤目的的信息。  
类型：字符串  
必需：否

[name](#nameProp)  
在运行手册的所有步骤名称中必须唯一的标识符。  
类型：字符串  
允许的模式：[a-zA-Z0-9\_]\+$  
必需：是

[action](#actProp)  
步骤要运行的操作的名称。[`aws:runCommand` - 在托管实例上运行命令](automation-action-runcommand.md) 是可在此处指定的操作的示例。本文档提供有关所有可用操作的详细信息。  
类型：字符串  
必需：是

[maxAttempts](#maxProp)  
在发生故障的情况下应重试步骤的次数。如果值大于 1，则直到所有重试尝试失败后，才会将此步骤视为失败。默认值是 1。  
类型：整数  
必需：否

[timeoutSeconds](#timeProp)  
步骤的超时值。如果超时并且 `maxAttempts` 的值大于 1，则本步未考虑超时，直至已尝试所有重试。  
类型：整数  
必需：否

[onFailure](#failProp)  
指示自动化在失败时是应停止、继续还是转到其他步骤。该选项的默认值为 abort。  
类型：字符串  
有效值：Abort \| Continue \| step:{{step\_name}}  
必需：否

[onCancel](#canProp)  
指示在用户取消自动化时，自动化应该转到哪个步骤。自动化将最多运行两分钟的取消工作流程。  
类型：字符串  
有效值：Abort \| step:{{step\_name}}  
必需：否  
`onCancel` 属性不支持移动到以下操作：  
+ `aws:approve`
+ `aws:copyImage`
+ `aws:createImage`
+ `aws:createStack`
+ `aws:createTags`
+ `aws:loop`
+ `aws:pause`
+ `aws:runInstances`
+ `aws:sleep`

[isEnd](#endProp)  
此选项在特定步骤结束时停止自动化。如果步骤失败或成功，自动化执行将停止。默认值为 False。  
类型：布尔值  
有效值：true \| false  
必需：否

[nextStep](#nextProp)  
指定在成功完成自动化中的一个步骤后，接下来处理哪个步骤。  
类型：字符串  
必需：否

[isCritical](#critProp)  
将一个步骤指定为成功完成自动化的关键步骤。如果具有此分派的步骤失败，自动化会将自动化的最终状态报告为失败。仅当您在步骤中明确定义此属性时，才会计算该属性。如果 `onFailure` 属性在某个步骤中设定为 `Continue`，则此值默认为 False。否则，该选项的默认值为 True。  
类型：布尔值  
有效值：true \| false  
必需：否

[inputs](#inProp)  
特定于操作的属性。  
类型：映射  
必需：是

### 示例
<a name="automation-demo"></a>

```
---
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 }}"
```