

• AWS Systems Manager CloudWatch ダッシュボードは、2026 年 4 月 30 日以降は利用できなくなります。お客様は、これまでと同様に Amazon CloudWatch コンソールを使用して、Amazon CloudWatch ダッシュボードの表示、作成、管理を継続できます。詳細については、「[Amazon CloudWatch ダッシュボードのドキュメント](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Dashboards.html)」を参照してください。

# チュートリアル: Run Command で AWS CLI を使用する
<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: シェルスクリプトを実行してリソースの詳細を表示する](#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 サービスエンドポイント](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 およびコマンド ID を置き換えます。マネージド AWS IoT Greengrass コアデバイスの場合、インスタンス ID には mi-*ID\$1Number* を使用します。コマンド ID は **send-command** に対する応答として返されます。インスタンス ID は AWS Systems Manager のツールである Fleet 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: シェルスクリプトを実行してリソースの詳細を表示する
<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
```

このスクリプトにより、*AWS CodeDeploy ユーザーガイド* の「[CodeDeploy 用の Amazon EC2 インスタンスを作成する](https://docs.aws.amazon.com/codedeploy/latest/userguide/instances-ec2-create.html)」で説明されているように、Amazon Linux および Red Hat Enterprise Linux (RHEL) インスタンスに AWS CodeDeploy エージェントがインストールされます。

このスクリプトは、米国東部 (オハイオ) リージョン (us-east-2)、`aws-codedeploy-us-east-2` の AWS 管理の Amazon S3 バケットから CodeDeploy エージェントをインストールします。

**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"
        ]
    }
}
```