クイックリファレンス

AWS CLI 基本コマンドチートシート

AWS CLIのクイックリファレンス:EC2インスタンス操作、S3バケットとオブジェクト管理、名前付きプロファイル、ECS、Elastic Beanstalkまで網羅。

· 公開日

公式 AWS Command Line Interface (aws) を使用した、日常的なクラウドインフラ運用・管理コマンドのクイックリファレンス。

インストールと初期設定

# macOS (Homebrew) でのインストール
brew install awscli

# アクセスキーとデフォルトリージョンの初期設定
aws configure

# 名前付きプロファイル(環境別)の設定
aws configure --profile staging
aws configure --profile production

認証情報と設定内容は ~/.aws/credentials および ~/.aws/config に保存されます。


Amazon S3 (オブジェクトストレージ)

# バケット一覧またはバケット内オブジェクトの表示
aws s3 ls
aws s3 ls s3://my-bucket-name/prefix/

# ローカルファイルをS3へアップロードコピー
aws s3 cp document.pdf s3://my-bucket-name/docs/

# ローカルディレクトリとS3バケットの同期(除外指定あり)
aws s3 sync ./build s3://my-bucket-name/web --delete --exclude "*.tmp"

# ディレクトリまたはプレフィックスを再帰的に削除
aws s3 rm s3://my-bucket-name/temporary-files --recursive

Amazon EC2 (仮想サーバー)

# 稼働中インスタンスの情報を取得してテーブル表示
aws ec2 describe-instances \
  --filters "Name=instance-state-name,Values=running" \
  --query "Reservations[*].Instances[*].[InstanceId,PublicIpAddress,Tags[?Key=='Name'].Value|[0]]" \
  --output table

# インスタンスの起動と停止
aws ec2 start-instances --instance-ids i-0123456789abcdef0
aws ec2 stop-instances --instance-ids i-0123456789abcdef0

# インスタンスの終了・破棄
aws ec2 terminate-instances --instance-ids i-0123456789abcdef0

Amazon ECS (コンテナオーケストレーション)

# クラスター一覧の取得
aws ecs list-clusters

# 新しいクラスターの作成
aws ecs create-cluster --cluster-name production-cluster

# クラスター内で稼働中のタスク一覧を取得
aws ecs list-tasks --cluster production-cluster

AWS Elastic Beanstalk CLI (eb)

# Elastic Beanstalk アプリケーションの初期化
eb init

# 環境の作成とアプリケーションのデプロイ
eb create production-env
eb deploy

# ブラウザで稼働中アプリを開く
eb open

# リアルタイムアプリケーションログの確認
eb logs

便利な共通フラグとクエリ構文

  • --profile <name>: 指定したIAMプロファイル環境でコマンドを実行。
  • --region <region>: 設定済みリージョンを一時的に上書き指定。
  • --output [json|text|table]: CLIの出力形式を指定 (json, text, table)。
  • --query '<JMESPath>': JMESPath式を用いてJSON属性をフィルタリング・抽出。

関連テーマ