1. 概要
実は、本章で扱っている「Ubuntu 26.04」は、「AWS」のものなのです。
「AWS」には、「aws-cli」という便利なものがあるらしい。
インストールして、使ってみます。
2. インストール
準備。
sudo apt update
sudo apt upgrade -y
sudo apt install -y unzip curl
ダウンロード、インストール。
cd /tmp
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
下記が出力されます。
You can now run: /usr/local/bin/aws --version
ちゅうことで、バージョン確認します。
aws --version
aws-cli/x.xx.xx Python/3.xx.x Linux/x.x.x-xxxx-aws exe/x86_64.ubuntu.26
これで、インストールは、完了です。
3. CloudWatch
「aws-cli」を利用して、「AWS」の使用状況を調べるのですが、それには、権限が必要で、それは「AWS」のクラウドコンソールを使います。
権限の設定の仕方は、機会があれば、別途記述します。
権限の有無は。
aws sts get-caller-identity
で調べられます。
なければ。
aws: [ERROR]: An error occurred (NoCredentials): Unable to locate credentials. You can configure credentials by running "aws login".
てな表示になりますし、権限があれば、下記のような表示になります。
{
"UserId": "伏字:伏字",
"Account": "伏字",
"Arn": "arn:aws:sts::伏字:assumed-role/伏字/伏字"
}
権限がある状態であれば、現在の状態を調べます。
まず、リージョンの確認。
aws configure get region
結果に何も表示されなければ、リージョンを設定します。
東京リージョンであれば。
export AWS_DEFAULT_REGION=ap-northeast-1
対象「EC2」の「Instance ID」を設定します。
export INSTANCE_ID=[Instance ID]
「Instance ID」は、「get-caller-identity」の最下行、末尾のものです。
これらの「export」は、「.bashrc」で設定しておいてもいいでしょう。
ここまでできたら、「CPU」データを取得してみます。
aws cloudwatch get-metric-statistics \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=InstanceId,Value=$INSTANCE_ID \
--start-time "$(date -u -d '1 hour ago' '+%Y-%m-%dT%H:%M:%SZ')" \
--end-time "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" \
--period 300 \
--statistics Average \
--output table
この結果。
-------------------------------------------------------------------
| GetMetricStatistics |
+--------------------+--------------------------------------------+
| Label | CPUUtilization |
+--------------------+--------------------------------------------+
|| Datapoints ||
|+----------------------+-----------------------------+----------+|
|| Average | Timestamp | Unit ||
|+----------------------+-----------------------------+----------+|
|| 0.21166534717867394 | 2026-08-27T13:42:00+09:00 | Percent ||
・・・ 略 ・・・
|| 0.22165463662610527 | 2026-08-27T13:37:00+09:00 | Percent ||
|+----------------------+-----------------------------+----------+|