逐步解說: AWS CLI 搭配 使用 Run Command - AWS Systems Manager

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

逐步解說: AWS CLI 搭配 使用 Run Command

下列範例逐步解說說明如何使用 AWS Command Line Interface (AWS CLI) 檢視命令和命令參數的相關資訊、如何執行命令,以及如何檢視這些命令的狀態。

重要

只有信任的管理員才能使用本主題中顯示的 AWS Systems Manager 預先設定文件。Systems Manager 文件中指定的命令或指令碼會使用在您的受管節點上的管理許可執行。如果使用者具備執行任何預先定義的 Systems Manager 文件 (任何以 AWS- 為開頭的文件) 的許可,則該使用者還具有該節點的管理員存取權。對於所有其他使用者,您應該建立嚴格的文件和並將它們分享給特定使用者。

步驟 1:入門

您必須在您想要設定的受管節點上具有管理員許可,或已在 AWS Identity and Access Management (IAM) 中被授予適當的許可。另請注意,此範例使用美國東部 (俄亥俄) 區域 (us-east-2)。 Run Command 可在 中 Systems Manager 服務端點中 AWS 區域 列出的 中使用Amazon Web Services 一般參考。如需詳細資訊,請參閱設定 AWS Systems Manager的受管節點

使用 執行命令 AWS CLI
  1. 如果您尚未安裝並設定 AWS Command Line Interface (AWS CLI),請安裝並設定 。

    如需相關資訊,請參閱安裝或更新最新版本的 AWS 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。對於受管 AWS IoT Greengrass 核心裝置,請使用執行個體 ID 的 mi-ID_number。會將命令 ID 傳回做為 send-command 的回應。執行個體 IDs可從 中的Fleet Manager工具 取得 AWS 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 文件,在受管節點上執行任何命令或指令碼,如同您在本機登入一樣。

View the description and available parameters (查看描述和可用參數)

請執行下列命令以檢視 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-nameAWS-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 Simple Storage Service (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) 執行個體上安裝 AWS CodeDeploy 代理程式,如AWS CodeDeploy 《 使用者指南》中的為 CodeDeploy 建立 Amazon EC2 執行個體中所述。

指令碼會從位於美國東部 (俄亥俄) 區域 (us-east-2) 的 AWS 受管 S3 儲存貯體安裝 CodeDeploy 代理程式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" ] } }