演练:将 Amazon CLI 与 Run Command 结合使用 - Amazon Systems Manager
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

演练:将 Amazon CLI 与 Run Command 结合使用

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

重要

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

步骤 1:入门

您必须具有要配置的托管式节点的管理员权限,或必须已获得 Amazon Identity and Access Management (IAM) 中的适当权限。另请注意,此示例使用美国东部(俄亥俄州)区域(us-east-2)。Run Command 在《Amazon Web Services 一般参考》Systems Manager service endpoints 列出的 Amazon Web Services 区域 中可用。有关更多信息,请参阅设置 Amazon Systems Manager

使用 Amazon CLI 运行命令
  1. 安装并配置 Amazon Command Line Interface (Amazon CLI)(如果尚未执行该操作)。

    有关信息,请参阅安装或更新 Amazon CLI 的最新版本

  2. 列出所有可用文档。

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

    aws ssm list-documents
  3. 确认托管式节点已准备好接收命令。

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

    Linux & macOS
    aws ssm describe-instance-information \ --output text --query "InstanceInformationList[*]"
    Windows
    aws ssm describe-instance-information ^ --output text --query "InstanceInformationList[*]"
  4. 运行以下命令来查看有关特定托管式节点的详细信息。

    注意

    要运行本演练中的命令,请替换实例和命令 ID。对于托管式 Amazon IoT Greengrass 核心设备,对实例 ID 使用 mi-ID_NUMBER。命令 ID 将作为对 send-command 的响应返回。实例 ID 可从 Fleet Manager(Amazon Systems Manager 的一项功能)中获得。

    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 脚本以查看资源详细信息

利用 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 文档发送简单命令

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

如果您将 Windows Server 托管式节点设为目标,请将 document-name 更改为 AWS-RunPowerShellScript,并将 commandifconfig 更改为 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 脚本

以下命令使用 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 脚本

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

有关使用 Run Command 运行存储在远程位置的脚本的示例,请参阅从 Amazon S3 运行脚本从 GitHub 运行脚本

#!/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) 实例上安装 Amazon CodeDeploy 代理,如《Amazon CodeDeploy 用户指南》中的为 CodeDeploy 创建 Amazon EC2 实例所述。

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

在 Amazon 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" ] } }