

• 2026 年 4 月 30 日之後將不再提供 AWS Systems Manager CloudWatch Dashboard。客戶可以繼續使用 Amazon CloudWatch 主控台來檢視、建立和管理其 Amazon CloudWatch 儀表板，就像現在一樣。如需詳細資訊，請參閱 [Amazon CloudWatch Dashboard 文件](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)。

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

# 從 Amazon Simple Storage Service (Amazon S3) 執行指令碼
<a name="integration-s3"></a>

本節說明如何從 Amazon Simple Storage Service (Amazon S3) 下載並執行指令碼。下列主題包含與 Amazon S3 相關的資訊和術語。若要進一步了解 Amazon S3，請參閱 [What is Amazon S3?](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) 您能夠執行不同類型的指令碼，包括 Ansible Playbook、Python、Ruby 和 PowerShell。

另外，您也能下載包含多個指令碼的目錄。當您在 目錄中執行主要指令碼時， AWS Systems Manager 也會執行 目錄中包含的任何參考指令碼。

從 Amazon Simple Storage Service (Amazon S3) 執行指令碼時，請注意以下重要詳細資訊：
+ Systems Manager 不會驗證指令碼是否能夠在受管節點上執行。請確認節點上已安裝必要軟體，然後再下載和執行指令碼。或者，您可以使用 Run Command或 State Manager、 中的工具來建立安裝軟體的複合文件 AWS Systems Manager，然後下載並執行指令碼。
+ 確認使用者、角色或群組具備讀取 S3 儲存貯體所需的 AWS Identity and Access Management (IAM) 許可。
+ 確保 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體上的執行個體設定檔具有 `s3:ListBucket` 和 `s3:GetObject` 許可。如果執行個體設定檔沒有這些許可，則系統無法從 S3 儲存貯體下載指令碼。如需詳細資訊，請參閱《IAM 使用者指南》**中的[使用執行個體設定檔](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2_instance-profiles.html)。

## 從 Amazon Simple Storage Service (Amazon S3) 執行 Shell 指令碼
<a name="integration-s3-shell"></a>

下列資訊包含使用 AWS Systems Manager 主控台或 AWS Command Line Interface () 來協助您從 Amazon Simple Storage Service (Amazon S3) 執行指令碼的程序AWS CLI。雖然範例中以 Shell 指令碼為例，但可替換為其他類型的指令碼。

### 從 Amazon Simple Storage Service (Amazon S3) (主控台) 執行 Shell 指令碼
<a name="integration-s3-ruby-console"></a>

**從 Amazon Simple Storage Service (Amazon S3) 執行 Shell 指令碼**

1. 在 https：//[https://console.aws.amazon.com/systems-manager/](https://console.aws.amazon.com/systems-manager/) 開啟 AWS Systems Manager 主控台。

1. 在導覽窗格中，選擇 **Run Command**。

1. 選擇**執行命令**。

1. 在 **Command document** (命令文件) 清單，請選擇 **`AWS-RunRemoteScript`**。

1. 在**命令參數**中，執行以下操作：
   + 在 **Source Type (來源類型)** 中，選取 **S3**。
   + 在 **Source Info** (來源資訊) 文字方塊中，按照以下格式輸入所需資訊，藉此存取來源。將每個{{範例資源預留位置}}取代為您自己的資訊。
**注意**  
用儲存貯體的 URL 取代 https://s3.{{aws-api-domain}}。您可以在 **Objects** (物件) 索引標籤上複製 Amazon S3 中的儲存貯體 URL。

     ```
     {"path":"https://s3.{{aws-api-domain}}/{{path to script}}"}
     ```

     下列是 範例。

     ```
     {"path":"https://amzn-s3-demo-bucket.s3.us-east-2.amazonaws.com/scripts/shell/helloWorld.sh"}
     ```
   + 在 **Command Line** (命令列) 欄位中，輸入指令碼執行參數。請見此處範例。

     ```
     helloWorld.sh argument-1 argument-2
     ```
   + (選用) 在 **Working Directory** (工作目錄) 欄位中，輸入節點上的目錄名稱；您將下載指令碼至該目錄並予以執行。
   + (選用) 在**執行逾時**中，指定指令碼命令執行失敗前，系統的等待時間 (秒)。

1. 在 **Targets** (目標) 區段中，透過手動指定標籤、選取執行個體或邊緣裝置，或指定資源群組，選擇您要執行這項操作的受管節點。
**提示**  
如果您預期看到的受管節點未列出，請參閱 [疑難排解受管節點的可用性](fleet-manager-troubleshooting-managed-nodes.md) 以取得疑難排解秘訣。

1. 對於**其他參數**：
   + 在 **Comment** (註解) 中，輸入此命令的相關資訊。
   + 在**逾時 (秒)** 中，指定在命令執行全面失敗之前，系統要等候的秒數。

1. 對於 **Rate control** (速率控制)：
   + 在**並行**中，指定可同時執行命令的受管節點數目或百分比。
**注意**  
如果您透過指定套用至受管節點的標籤或指定 AWS 資源群組來選取目標，而且不確定目標的受管節點數量，則透過指定百分比來限制可同時執行文件的目標數量。
   + 在 **Error threshold** (錯誤閾值) 中，指定在特定數目或百分比之節點上的命令失敗之後，停止在其他受管節點上執行命令。例如，如果您指定三個錯誤，則 Systems Manager 會在收到第四個錯誤時停止傳送命令。仍在處理命令的受管節點也可能會傳送錯誤。

1. (選用) 針對**輸出選項**，若要將命令輸出儲存至檔案，請選取**將命令輸出寫入至 S3 儲存貯體**方塊。在方塊中輸入儲存貯體和字首 (資料夾) 名稱。
**注意**  
授予能力以將資料寫入至 S3 儲存貯體的 S3 許可，會是指派給執行個體之執行個體設定檔 (適用於 EC2 執行個體) 或 IAM 服務角色 (啟用混合模式的機器) 的許可，而不是執行此任務之 IAM 使用者的許可。如需詳細資訊，請參閱[設定 Systems Manager 所需的執行個體許可](setup-instance-permissions.md)或[建立混合環境的 IAM 服務角色](hybrid-multicloud-service-role.md)。此外，若指定的 S3 儲存貯體位於不同的 AWS 帳戶內，請確保與受管節點相關聯的執行個體設定檔或 IAM 服務角色是否具有寫入該儲存貯體的必要許可。

1. 在**SNS 通知**區段中，如果您要傳送有關命令執行狀態的通知，請選取**啟用 SNS 通知**核取方塊。

   如需為 Run Command 設定 Amazon SNS 通知的詳細資訊，請參閱 [使用 Amazon SNS 通知監控 Systems Manager 狀態變更](monitoring-sns-notifications.md)。

1. 選擇**執行**。

### 從 Amazon Simple Storage Service (Amazon S3) (命令行) 執行 Shell 指令碼
<a name="integration-s3-shell-cli"></a>

1. 如果您尚未安裝和設定 AWS Command Line Interface (AWS CLI)。

   如需相關資訊，請參閱[安裝或更新最新版本的 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)。

1. 執行下列命令。將每個{{範例資源預留位置}}取代為您自己的資訊。
**注意**  
使用儲存貯體的 URL 取代 https://s3.{{aws-api-domain}}/{{指令碼路徑}}。您可以在 **Objects** (物件) 索引標籤上複製 Amazon S3 中的儲存貯體 URL。

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

   ```
   aws ssm send-command \
       --document-name "AWS-RunRemoteScript" \
       --output-s3-bucket-name "{{amzn-s3-demo-bucket}}" \
       --output-s3-key-prefix "{{key-prefix}}" \
       --targets "Key=InstanceIds,Values={{instance-id}}" \
       --parameters '{"sourceType":["S3"],"sourceInfo":["{\"path\":\"https://s3.{{aws-api-domain}}/{{script path}}\"}"],"commandLine":["{{script name and arguments}}"]}'
   ```

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

   ```
   aws ssm send-command ^
       --document-name "AWS-RunRemoteScript" ^
       --output-s3-bucket-name "{{amzn-s3-demo-bucket}}" ^
       --output-s3-key-prefix "{{key-prefix}}" ^
       --targets "Key=InstanceIds,Values={{instance-id}}" ^
       --parameters "sourceType"="S3",sourceInfo='{\"path\":\"https://s3.{{aws-api-domain}}/{{script path}}\"}',"commandLine"="{{script name and arguments}}"
   ```

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

   ```
   Send-SSMCommand `
       -DocumentName "AWS-RunRemoteScript" `
       -OutputS3BucketName "{{amzn-s3-demo-bucket}}" `
       -OutputS3KeyPrefix "{{key-prefix}}" `
       -Target @{Key="InstanceIds";Values=@("{{instance-id}}")} `
       -Parameter @{
           sourceType = "S3"; 
           sourceInfo = '{"path": "s3://{{bucket-name/path/to/script}}"}'; 
           commandLine = "{{script name and arguments}}"
       }
   ```

------