

• 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 Automation 支持的自动化操作


在您运行自动化时，默认情况下，自动化将在启动该自动化的用户的上下文中运行。也就是说，例如，如果您的用户拥有管理员权限，则该自动化运行时也拥有管理员权限，并且对该自动化配置的资源拥有完全访问权限。作为最佳安全实践，建议使用配置有 AmazonSSMAutomationRole 托管式策略的 IAM 服务角色（在本例中也称为*担任*角色）运行自动化。您可能需要将其他 IAM policy 添加到您的担任角色，以使用各种运行手册。使用 IAM 服务角色运行自动化称为*委托管理*。

如果使用服务角色，则允许对 AWS 资源运行自动化，但运行自动化的用户对这些资源的访问受限（或无法访问）。例如，您可以配置一个服务角色，并将其与自动化配合使用以自动重启一个或多个 Amazon Elastic Compute Cloud (Amazon EC2) 实例。Automation 是 AWS Systems Manager 中的一项工具。自动化会重启实例，但服务角色不会授予用户访问这些实例的权限。

可以在您运行自动化的运行时中指定服务角色，也可以创建自定义自动化运行手册，并直接在运行手册中指定服务角色。无论您是在运行时还是在运行手册中指定了服务角色，服务都会在所指定服务角色的上下文中运行。如果您未指定服务角色，则系统会在用户上下文中创建临时会话并运行自动化。

**注意**  
对于预计运行时长超过 12 小时的自动化，必须指定服务角色。如果您在用户环境中启动了长时间运行的自动化，用户的临时会话会在 12 小时后过期。

委托管理可确保更高的安全性，还能更好地控制您的 AWS 资源。它还能够增强审核体验，因为针对您的资源进行的操作是由集中服务角色执行的，而不是由多个 IAM 账户执行。

**开始前的准备工作**  
在您完成以下过程之前，必须创建 IAM 服务角色并为 Automation（AWS Systems Manager 中的一项工具）配置信任关系。有关更多信息，请参阅 [任务 1：为自动化创建服务角色](automation-setup-iam.md#create-service-role)。

以下过程介绍了如何使用 Systems Manager 控制台或您首选的命令行工具运行简单的自动化。

## 运行简单的自动化（控制台）


以下过程介绍了如何使用 Systems Manager 控制台运行简单的自动化。

**运行简单的自动化**

1. 访问 [https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/)，打开 AWS Systems Manager 控制台。

1. 在导航窗格中，选择**自动化**，然后选择**执行自动化**。

1. 在**自动化文档**列表中，请选择运行手册。在**文档类别**窗格中选择一个或多个选项，以便根据 SSM 文档的用途对其进行筛选。要查看您拥有的运行手册，请选择**我拥有的**选项卡。要查看与您的账户共享的运行手册，请选择**与我共享**选项卡。要查看所有运行手册，请选择**所有文档**选项卡。
**注意**  
您可以通过选择运行手册名称来查看有关该手册的信息。

1. 在**文档详细信息**部分中，验证**文档版本**已设置为要运行的版本。系统包括以下版本选项：
   + **运行时的默认版本** – 如果定期更新自动化运行手册并分配新的默认版本，请选择此选项。
   + **运行时的最新版本** – 如果定期更新自动化运行手册并且想要运行最新更新的版本，请选择此选项。
   + **1（默认）** – 选择此选项可执行文档的第一个版本，即默认版本。

1. 选择**下一步**。

1. 在**执行模式**部分中，选择**简单执行**。

1. 在 **输入参数** 部分中，指定所需的输入。或者，您也可以从 **AutomationAssumeRole** 列表选择一个 IAM 服务角色。

1. （可选）选择一个 CloudWatch 警报以应用于您的自动化进行监控。要将 CloudWatch 警报附加到自动化，启动自动化的 IAM 主体必须具有 `iam:createServiceLinkedRole` 操作的权限。有关 CloudWatch 警报的更多信息，请参阅[使用 Amazon CloudWatch 警报](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/AlarmThatSendsEmail.html)。请注意，如果您的警报激活，自动化将停止。如果使用 AWS CloudTrail，您将在跟踪中看到 API 调用。

1. 选择**执行**。

控制台将显示自动化的状态。如果自动化无法运行，请参阅 [Systems Manager 自动化故障排除](automation-troubleshooting.md)。

自动化执行完成后，您可以使用相同或修改后的参数重新运行该执行。有关更多信息，请参阅 [重新运行自动化执行](automation-rerun-executions.md)。

## 运行简单的自动化（命令行）


以下过程介绍了如何使用 AWS CLI（在 Linux 或 Windows 上）或 AWS Tools for PowerShell 运行简单的自动化。

**运行简单的自动化**

1. 安装并配置 AWS CLI 或 AWS Tools for PowerShell（如果尚未执行该操作）。

   有关信息，请参阅[安装或更新 AWS CLI 的最新版本](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)以及[安装 AWS Tools for PowerShell](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-getting-set-up.html)。

1. 运行以下命令以启动一个简单的自动化。将每个*示例资源占位符*替换为您自己的信息。

------
#### [ Linux & macOS ]

   ```
   aws ssm start-automation-execution \
       --document-name runbook name \
       --parameters runbook parameters
   ```

------
#### [ Windows ]

   ```
   aws ssm start-automation-execution ^
       --document-name runbook name ^
       --parameters runbook parameters
   ```

------
#### [ PowerShell ]

   ```
   Start-SSMAutomationExecution `
     -DocumentName runbook name `
     -Parameter runbook parameters
   ```

------

   以下是使用 `AWS-RestartEC2Instance` 运行手册重新启动指定 EC2 实例的示例。

------
#### [ Linux & macOS ]

   ```
   aws ssm start-automation-execution \
       --document-name "AWS-RestartEC2Instance" \
       --parameters "InstanceId=i-02573cafcfEXAMPLE"
   ```

------
#### [ Windows ]

   ```
   aws ssm start-automation-execution ^
       --document-name "AWS-RestartEC2Instance" ^
       --parameters "InstanceId=i-02573cafcfEXAMPLE"
   ```

------
#### [ PowerShell ]

   ```
   Start-SSMAutomationExecution `
     -DocumentName AWS-RestartEC2Instance `
     -Parameter @{"InstanceId"="i-02573cafcfEXAMPLE"}
   ```

------

   系统将返回类似于以下内容的信息。

------
#### [ Linux & macOS ]

   ```
   {
       "AutomationExecutionId": "4105a4fc-f944-11e6-9d32-0123456789ab"
   }
   ```

------
#### [ Windows ]

   ```
   {
       "AutomationExecutionId": "4105a4fc-f944-11e6-9d32-0123456789ab"
   }
   ```

------
#### [ PowerShell ]

   ```
   4105a4fc-f944-11e6-9d32-0123456789ab
   ```

------

1. 运行以下命令以检索自动化的状态。

------
#### [ Linux & macOS ]

   ```
   aws ssm describe-automation-executions \
       --filter "Key=ExecutionId,Values=4105a4fc-f944-11e6-9d32-0123456789ab"
   ```

------
#### [ Windows ]

   ```
   aws ssm describe-automation-executions ^
       --filter "Key=ExecutionId,Values=4105a4fc-f944-11e6-9d32-0123456789ab"
   ```

------
#### [ PowerShell ]

   ```
   Get-SSMAutomationExecutionList | `
     Where {$_.AutomationExecutionId -eq "4105a4fc-f944-11e6-9d32-0123456789ab"}
   ```

------

   系统将返回类似于以下内容的信息。

------
#### [ Linux & macOS ]

   ```
   {
       "AutomationExecutionMetadataList": [
           {
               "AutomationExecutionStatus": "InProgress",
               "CurrentStepName": "stopInstances",
               "Outputs": {},
               "DocumentName": "AWS-RestartEC2Instance",
               "AutomationExecutionId": "4105a4fc-f944-11e6-9d32-0123456789ab",
               "DocumentVersion": "1",
               "ResolvedTargets": {
                   "ParameterValues": [],
                   "Truncated": false
               },
               "AutomationType": "Local",
               "Mode": "Auto",
               "ExecutionStartTime": 1564600648.159,
               "CurrentAction": "aws:changeInstanceState",
               "ExecutedBy": "arn:aws:sts::123456789012:assumed-role/Administrator/Admin",
               "LogFile": "",
               "Targets": []
           }
       ]
   }
   ```

------
#### [ Windows ]

   ```
   {
       "AutomationExecutionMetadataList": [
           {
               "AutomationExecutionStatus": "InProgress",
               "CurrentStepName": "stopInstances",
               "Outputs": {},
               "DocumentName": "AWS-RestartEC2Instance",
               "AutomationExecutionId": "4105a4fc-f944-11e6-9d32-0123456789ab",
               "DocumentVersion": "1",
               "ResolvedTargets": {
                   "ParameterValues": [],
                   "Truncated": false
               },
               "AutomationType": "Local",
               "Mode": "Auto",
               "ExecutionStartTime": 1564600648.159,
               "CurrentAction": "aws:changeInstanceState",
               "ExecutedBy": "arn:aws:sts::123456789012:assumed-role/Administrator/Admin",
               "LogFile": "",
               "Targets": []
           }
       ]
   }
   ```

------
#### [ PowerShell ]

   ```
   AutomationExecutionId       : 4105a4fc-f944-11e6-9d32-0123456789ab
   AutomationExecutionStatus   : InProgress
   AutomationType              : Local
   CurrentAction               : aws:changeInstanceState
   CurrentStepName             : startInstances
   DocumentName                : AWS-RestartEC2Instance
   DocumentVersion             : 1
   ExecutedBy                  : arn:aws:sts::123456789012:assumed-role/Administrator/Admin
   ExecutionEndTime            : 1/1/0001 12:00:00 AM
   ExecutionStartTime          : 7/31/2019 7:17:28 PM
   FailureMessage              : 
   LogFile                     : 
   MaxConcurrency              : 
   MaxErrors                   : 
   Mode                        : Auto
   Outputs                     : {}
   ParentAutomationExecutionId : 
   ResolvedTargets             : Amazon.SimpleSystemsManagement.Model.ResolvedTargets
   Target                      : 
   TargetMaps                  : {}
   TargetParameterName         : 
   Targets                     : {}
   ```

------