AWS CLIチートシート

中級 | 15分 read | 2025.01.10

セットアップ

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

# 設定
aws configure
# AWS Access Key ID: AKIAIOSFODNN7EXAMPLE
# AWS Secret Access Key: ****
# Default region name: ap-northeast-1
# Default output format: json

# プロファイル切り替え
aws configure --profile production
export AWS_PROFILE=production

# 現在の設定確認
aws configure list
aws sts get-caller-identity

S3

バケット操作

# バケット一覧
aws s3 ls

# バケット作成
aws s3 mb s3://my-bucket

# バケット削除(空の場合)
aws s3 rb s3://my-bucket

# バケット削除(中身ごと)
aws s3 rb s3://my-bucket --force

ファイル操作

# ファイル一覧
aws s3 ls s3://my-bucket/
aws s3 ls s3://my-bucket/path/ --recursive

# アップロード
aws s3 cp file.txt s3://my-bucket/
aws s3 cp file.txt s3://my-bucket/path/file.txt

# ダウンロード
aws s3 cp s3://my-bucket/file.txt ./
aws s3 cp s3://my-bucket/path/ ./ --recursive

# ディレクトリ同期
aws s3 sync ./local s3://my-bucket/remote
aws s3 sync s3://my-bucket/remote ./local
aws s3 sync ./local s3://my-bucket/remote --delete

# 削除
aws s3 rm s3://my-bucket/file.txt
aws s3 rm s3://my-bucket/path/ --recursive

# 移動
aws s3 mv s3://my-bucket/old.txt s3://my-bucket/new.txt

# プレサインドURL
aws s3 presign s3://my-bucket/file.txt --expires-in 3600

EC2

インスタンス

# インスタンス一覧
aws ec2 describe-instances

# 特定の情報のみ
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,State.Name,PublicIpAddress]' --output table

# 実行中のみ
aws ec2 describe-instances --filters "Name=instance-state-name,Values=running"

# インスタンス起動
aws ec2 start-instances --instance-ids i-1234567890abcdef0

# インスタンス停止
aws ec2 stop-instances --instance-ids i-1234567890abcdef0

# インスタンス終了
aws ec2 terminate-instances --instance-ids i-1234567890abcdef0

# インスタンス作成
aws ec2 run-instances \
  --image-id ami-12345678 \
  --instance-type t3.micro \
  --key-name my-key \
  --security-group-ids sg-12345678 \
  --subnet-id subnet-12345678

セキュリティグループ

# 一覧
aws ec2 describe-security-groups

# 作成
aws ec2 create-security-group \
  --group-name my-sg \
  --description "My security group"

# ルール追加
aws ec2 authorize-security-group-ingress \
  --group-id sg-12345678 \
  --protocol tcp \
  --port 443 \
  --cidr 0.0.0.0/0

Lambda

# 関数一覧
aws lambda list-functions

# 関数情報
aws lambda get-function --function-name my-function

# 関数呼び出し
aws lambda invoke \
  --function-name my-function \
  --payload '{"key": "value"}' \
  output.json

# 関数更新(コード)
aws lambda update-function-code \
  --function-name my-function \
  --zip-file fileb://function.zip

# 環境変数更新
aws lambda update-function-configuration \
  --function-name my-function \
  --environment "Variables={KEY1=value1,KEY2=value2}"

# ログ確認
aws logs tail /aws/lambda/my-function --follow

IAM

# ユーザー一覧
aws iam list-users

# ロール一覧
aws iam list-roles

# ポリシー一覧
aws iam list-policies --scope Local

# アクセスキー一覧
aws iam list-access-keys --user-name my-user

# アクセスキー作成
aws iam create-access-key --user-name my-user

# アクセスキー削除
aws iam delete-access-key --user-name my-user --access-key-id AKIAIOSFODNN7EXAMPLE

CloudFormation

# スタック一覧
aws cloudformation list-stacks

# スタック作成
aws cloudformation create-stack \
  --stack-name my-stack \
  --template-body file://template.yaml \
  --parameters ParameterKey=Env,ParameterValue=prod

# スタック更新
aws cloudformation update-stack \
  --stack-name my-stack \
  --template-body file://template.yaml

# スタック削除
aws cloudformation delete-stack --stack-name my-stack

# イベント確認
aws cloudformation describe-stack-events --stack-name my-stack

RDS

# インスタンス一覧
aws rds describe-db-instances

# スナップショット作成
aws rds create-db-snapshot \
  --db-instance-identifier my-db \
  --db-snapshot-identifier my-snapshot

# スナップショット一覧
aws rds describe-db-snapshots --db-instance-identifier my-db

CloudWatch

# ログ確認
aws logs tail /aws/lambda/my-function --since 1h
aws logs tail /aws/lambda/my-function --follow

# ロググループ一覧
aws logs describe-log-groups

# メトリクス取得
aws cloudwatch get-metric-statistics \
  --namespace AWS/EC2 \
  --metric-name CPUUtilization \
  --dimensions Name=InstanceId,Value=i-1234567890abcdef0 \
  --start-time 2025-01-01T00:00:00Z \
  --end-time 2025-01-02T00:00:00Z \
  --period 3600 \
  --statistics Average

Secrets Manager

# シークレット一覧
aws secretsmanager list-secrets

# シークレット取得
aws secretsmanager get-secret-value --secret-id my-secret

# シークレット作成
aws secretsmanager create-secret \
  --name my-secret \
  --secret-string '{"username":"admin","password":"secret"}'

# シークレット更新
aws secretsmanager update-secret \
  --secret-id my-secret \
  --secret-string '{"username":"admin","password":"newsecret"}'

便利なオプション

# 出力形式
--output json    # JSON(デフォルト)
--output table   # テーブル
--output text    # テキスト

# フィルタリング(JMESPath)
--query 'Items[*].{Name:name,ID:id}'
--query 'Items[?status==`active`]'

# ドライラン
--dry-run

# リージョン指定
--region us-east-1

関連記事

← Back to list