🧑‍💻

[AWS]CLI便利コマンド

2021/11/08に公開約2,700字

複数AWSアカウント運用していてEC2やRDSの状態を確認したい場合、

  • マネージメントコンソール (AWS SSO運用でも)ログインしなおすのが面倒
  • CLI コマンドを調べるのが面倒 以下コマンドをalias/usr/local/binなどで登録してあると確認が楽。

前提条件

  • aws cli v2 (v1でも動くかもしれないが未検証)
  • jq インストール済

EC2一覧

aws ec2 describe-instances  \
|       jq -r '.[][].Instances[]
         | [[.Tags[] | select(.Key == "Name").Value][],
            .InstanceId,
            .PrivateIpAddress,
            .InstanceType,
            .LaunchTime,
            .State.Name,
            .PublicIpAddress
            ]
         | @tsv' | \
         column -t

RDS一覧

echo "======= RDS Cluster =========="
aws rds describe-db-clusters | \
jq -r '.[][]
| [.DBClusterIdentifier,
.Status,
.Endpoint]
| @tsv' | \
column -t

echo "======= RDS Instance =========="
aws rds describe-db-instances | \
    jq -r '.[][]
         | [.DBInstanceIdentifier,
            .DBInstanceClass,
            .DBClusterIdentifier,
            .DBInstanceStatus]
         | @tsv' | \
    column -t


echo "==== RDS Writer Instance ===="
aws rds describe-db-clusters --query "DBClusters[].[DBClusterIdentifier,DBClusterMembers[].[DBInstanceIdentifier,IsClusterWriter]]" --output text

ElastiCache一覧

aws elasticache describe-cache-clusters | \
    jq -r '.[][]
         | [.CacheClusterId,
            .CacheNodeType,
            .CacheClusterStatus,
            .ReplicationGroupId]
         | @tsv' | \
    column -t

おまけ

RI/SavingsPlans一覧

#!/bin/bash

echo " =========== EC2 RI 確認 ============="
aws ec2 describe-reserved-instances \
  | jq -r '.ReservedInstances[] |
  [
	  .InstanceType,
	  .InstanceCount,
	  .Start,
	  .End,
      .State
	  ] | @tsv' | \
         column -t | sort -k5


echo " =========== RDS RI 確認 ============="
aws rds describe-reserved-db-instances \
  | jq -r '.ReservedDBInstances[] |
  [
.DBInstanceClass,
.DBInstanceCount,
.StartTime,
.Duration,
.State
	  ] | @tsv' | \
         column -t | sort -k5


echo " =========== ElastiCache RI 確認 ============="
aws elasticache describe-reserved-cache-nodes \
  | jq -r '.ReservedCacheNodes[] |
  [
.CacheNodeType,
.CacheNodeCount,
.StartTime,
.Duration,
.State
	  ] | @tsv' | \
         column -t | sort -k5



echo " =========== OpenSearch RI 確認 ============="
aws opensearch describe-reserved-instances \
  | jq -r '.ReservedInstances[] |
  [
.InstanceType,
.InstanceCount,
.StartTime,
.Duration,
.State
	  ] | @tsv' | \
         column -t | sort -k5

echo " =========== SavingsPlans確認 ============="
_DATE=$(date +%Y-%m-%d)
_DATE_1Y=$(date -v-1y +%Y-%m-%d)
aws ce get-savings-plans-utilization-details --time-period Start=${_DATE_1Y},End=${_DATE} | jq -r '.SavingsPlansUtilizationDetails[].Attributes |
[
.InstanceFamily,
.HourlyCommitment,
.StartDateTime,
.EndDateTime,
.Status
] | @tsv'
GitHubで編集を提案

Discussion

ログインするとコメントできます