

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

# 演练：将 AWS CLI 与 Run Command 结合使用
<a name="walkthrough-cli"></a>

以下示例演练介绍了如何使用 AWS Command Line Interface (AWS CLI) 查看有关命令和命令参数的信息、如何运行命令以及如何查看这些命令的状态。

**重要**  
仅允许受信任的管理员使用本主题中所示的 AWS Systems Manager 预配置文档。在 Systems Manager 文档中指定的命令和脚本需要管理权限才能在您的托管式节点上运行。如果用户有权运行任何预定义的 Systems Manager 文档（任何以 `AWS-` 开头的文档），则该用户也具有节点的管理员访问权限。对于所有其他用户，您应创建限制性文档并与特定用户共享这些文档。

**Topics**
+ [步骤 1：入门](#walkthrough-cli-settings)
+ [步骤 2：运行 Shell 脚本以查看资源详细信息](#walkthrough-cli-run-scripts)
+ [步骤 3：使用 `AWS-RunShellScript` 文档发送简单命令](#walkthrough-cli-example-1)
+ [步骤 4：使用 Run Command 运行简单 Python 脚本](#walkthrough-cli-example-2)
+ [步骤 5：使用 Run Command 运行 Bash 脚本](#walkthrough-cli-example-3)

## 步骤 1：入门
<a name="walkthrough-cli-settings"></a>

您必须具有要配置的托管式节点的管理员权限，或必须已获得 AWS Identity and Access Management (IAM) 中的适当权限。另请注意，此示例使用美国东部（俄亥俄州）区域（us-east-2）。Run Command 在《Amazon Web Services 一般参考》**的 [Systems Manager service endpoints](https://docs.aws.amazon.com/general/latest/gr/ssm.html#ssm_region) 列出的 AWS 区域 中可用。有关更多信息，请参阅 [为 AWS Systems Manager 设置托管节点](systems-manager-setting-up-nodes.md)。

**使用 AWS CLI 运行命令**

1. 安装并配置 AWS Command Line Interface（AWS CLI）（如果尚未执行该操作）。

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

1. 列出所有可用文档。

   此命令将基于 IAM 权限列出您的账户可用的所有文档。

   ```
   aws ssm list-documents
   ```

1. 确认托管式节点已准备好接收命令。

   以下命令的输出会显示托管式节点是否处于联机状态。

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

   ```
   aws ssm describe-instance-information \
       --output text --query "InstanceInformationList[*]"
   ```

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

   ```
   aws ssm describe-instance-information ^
       --output text --query "InstanceInformationList[*]"
   ```

------

1. 运行以下命令来查看有关特定托管式节点的详细信息。
**注意**  
要运行本演练中的命令，请替换实例和命令 ID。对于托管式 AWS IoT Greengrass 核心设备，对实例 ID 使用 mi-*ID\$1NUMBER*。命令 ID 将作为对 **send-command** 的响应返回。可从 Fleet Manager（AWS Systems Manager 中的一项工具）中获得实例 ID。

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

   ```
   aws ssm describe-instance-information \
       --instance-information-filter-list key=InstanceIds,valueSet=instance-ID
   ```

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

   ```
   aws ssm describe-instance-information ^
       --instance-information-filter-list key=InstanceIds,valueSet=instance-ID
   ```

------

## 步骤 2：运行 Shell 脚本以查看资源详细信息
<a name="walkthrough-cli-run-scripts"></a>

利用 Run Command 和 `AWS-RunShellScript` 文档，您可以在托管式节点上运行任何命令或脚本，就像您已在本地登录一样。

**查看说明和可用参数**

运行以下命令，查看 Systems Manager JSON 文档的描述。

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

```
aws ssm describe-document \
    --name "AWS-RunShellScript" \
    --query "[Document.Name,Document.Description]"
```

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

```
aws ssm describe-document ^
    --name "AWS-RunShellScript" ^
    --query "[Document.Name,Document.Description]"
```

------

运行以下命令，查看可用参数和有关这些参数的详细信息。

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

```
aws ssm describe-document \
    --name "AWS-RunShellScript" \
    --query "Document.Parameters[*]"
```

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

```
aws ssm describe-document ^
    --name "AWS-RunShellScript" ^
    --query "Document.Parameters[*]"
```

------

## 步骤 3：使用 `AWS-RunShellScript` 文档发送简单命令
<a name="walkthrough-cli-example-1"></a>

运行以下命令，获取 Linux 托管式节点的 IP 信息。

如果您将 Windows Server 托管式节点设为目标，请将 `document-name` 更改为 `AWS-RunPowerShellScript`，并将 `command` 从 `ifconfig` 更改为 `ipconfig`。

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

```
aws ssm send-command \
    --instance-ids "instance-ID" \
    --document-name "AWS-RunShellScript" \
    --comment "IP config" \
    --parameters commands=ifconfig \
    --output text
```

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

```
aws ssm send-command ^
    --instance-ids "instance-ID" ^
    --document-name "AWS-RunShellScript" ^
    --comment "IP config" ^
    --parameters commands=ifconfig ^
    --output text
```

------

**使用响应数据获取命令信息**  
以下命令使用从上一个命令返回的命令 ID 来获取命令执行的详细信息和响应数据。如果命令已完成，系统将返回响应数据。如果命令执行显示 `"Pending"` 或 `"InProgress"`，您再次运行此命令来查看响应数据。

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

```
aws ssm list-command-invocations \
    --command-id $sh-command-id \
    --details
```

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

```
aws ssm list-command-invocations ^
    --command-id $sh-command-id ^
    --details
```

------

**标识用户**

以下命令将显示运行命令的默认用户。

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

```
sh_command_id=$(aws ssm send-command \
    --instance-ids "instance-ID" \
    --document-name "AWS-RunShellScript" \
    --comment "Demo run shell script on Linux managed node" \
    --parameters commands=whoami \
    --output text \
    --query "Command.CommandId")
```

------

**获取命令状态**  
以下命令使用命令 ID 获取托管式节点上的命令执行状态。此示例使用上一个命令中返回的命令 ID。

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

```
aws ssm list-commands \
    --command-id "command-ID"
```

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

```
aws ssm list-commands ^
    --command-id "command-ID"
```

------

**获取命令详细信息**  
以下命令使用上一命令中的命令 ID 来获取每个托管式节点的命令执行状态。

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

```
aws ssm list-command-invocations \
    --command-id "command-ID" \
    --details
```

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

```
aws ssm list-command-invocations ^
    --command-id "command-ID" ^
    --details
```

------

**获取包含特定托管式节点的响应数据的命令信息**  
以下命令会返回特定托管式节点的原始 `aws ssm send-command` 请求的输出。

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

```
aws ssm list-command-invocations \
    --instance-id instance-ID \
    --command-id "command-ID" \
    --details
```

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

```
aws ssm list-command-invocations ^
    --instance-id instance-ID ^
    --command-id "command-ID" ^
    --details
```

------

**显示 Python 版本**

以下命令会返回在节点上运行的 Python 的版本。

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

```
sh_command_id=$(aws ssm send-command \
    --instance-ids "instance-ID" \
    --document-name "AWS-RunShellScript" \
    --comment "Demo run shell script on Linux Instances" \
    --parameters commands='python -V' \
    --output text --query "Command.CommandId") \
    sh -c 'aws ssm list-command-invocations \
    --command-id "$sh_command_id" \
    --details \
    --query "CommandInvocations[].CommandPlugins[].{Status:Status,Output:Output}"'
```

------

## 步骤 4：使用 Run Command 运行简单 Python 脚本
<a name="walkthrough-cli-example-2"></a>

以下命令使用 Run Command 运行一个简单的 Python“Hello World”脚本。

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

```
sh_command_id=$(aws ssm send-command \
    --instance-ids "instance-ID" \
    --document-name "AWS-RunShellScript" \
    --comment "Demo run shell script on Linux Instances" \
    --parameters '{"commands":["#!/usr/bin/python","print \"Hello World from python\""]}' \
    --output text \
    --query "Command.CommandId") \
    sh -c 'aws ssm list-command-invocations \
    --command-id "$sh_command_id" \
    --details \
    --query "CommandInvocations[].CommandPlugins[].{Status:Status,Output:Output}"'
```

------

## 步骤 5：使用 Run Command 运行 Bash 脚本
<a name="walkthrough-cli-example-3"></a>

此部分中的示例演示如何使用 Run Command 运行以下 Bash 脚本。

有关使用 Run Command 运行存储在远程位置的脚本的示例，请参阅[从 Amazon S3 运行脚本](integration-s3.md)和[从 GitHub 运行脚本](integration-remote-scripts.md)。

```
#!/bin/bash
yum -y update
yum install -y ruby
cd /home/ec2-user
curl -O https://aws-codedeploy-us-east-2.s3.amazonaws.com/latest/install
chmod +x ./install
./install auto
```

此脚本将在 Amazon Linux 和 Red Hat Enterprise Linux (RHEL) 实例上安装 AWS CodeDeploy 代理，如《AWS CodeDeploy 用户指南》中的[为 CodeDeploy 创建 Amazon EC2 实例](https://docs.aws.amazon.com/codedeploy/latest/userguide/instances-ec2-create.html)所述。**

该脚本将从 AWS 托管的 S3 存储桶安装 CodeDeploy 代理程序，该存储桶位于美国东部（俄亥俄州）区域（us-east-2）（`aws-codedeploy-us-east-2`）。

**在 AWS CLI 命令中运行 Bash 脚本**

以下示例演示了如何使用 `--parameters` 选项在 CLI 命令中包含 Bash 脚本。

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

```
aws ssm send-command \
    --document-name "AWS-RunShellScript" \
    --targets '[{"Key":"InstanceIds","Values":["instance-id"]}]' \
    --parameters '{"commands":["#!/bin/bash","yum -y update","yum install -y ruby","cd /home/ec2-user","curl -O https://aws-codedeploy-us-east-2.s3.amazonaws.com/latest/install","chmod +x ./install","./install auto"]}'
```

------

**在 JSON 文件中运行 Bash 脚本**

在以下示例中，Bash 脚本的内容存储在一个 JSON 文件中，该文件通过使用 `--cli-input-json` 选项包含在命令中。

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

```
aws ssm send-command \
    --document-name "AWS-RunShellScript" \
    --targets "Key=InstanceIds,Values=instance-id" \
    --cli-input-json file://installCodeDeployAgent.json
```

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

```
aws ssm send-command ^
    --document-name "AWS-RunShellScript" ^
    --targets "Key=InstanceIds,Values=instance-id" ^
    --cli-input-json file://installCodeDeployAgent.json
```

------

引用的 `installCodeDeployAgent.json` 文件的内容如以下示例所示。

```
{
    "Parameters": {
        "commands": [
            "#!/bin/bash",
            "yum -y update",
            "yum install -y ruby",
            "cd /home/ec2-user",
            "curl -O https://aws-codedeploy-us-east-2.s3.amazonaws.com/latest/install",
            "chmod +x ./install",
            "./install auto"
        ]
    }
}
```