【AWS CLI】EC2のリソース情報取得編

記事タイトルとURLをコピーする

こんにちは。AWS CLIが好きな福島です。
今回は、EC2のリソース情報を取得するコマンドをご紹介いたします。

過去にネットワーク関連のリソース一覧を取得するコマンドをご紹介しておりますので、 ご興味がある方は、こちらもぜひ読んでみてください。

blog.serverworks.co.jp

f:id:swx-fukushima:20201022005339p:plain

実行環境

今回、コマンドを実行した環境は、以下の通りとなります。
(本記事でご紹介しているコマンドの中には、Linuxのコマンドを利用している箇所があります。)

# uname -a
Linux LAPTOP-CNM26HN6 4.4.0-18362-Microsoft #1049-Microsoft Thu Aug 14 12:01:00 PST 2020 x86_64 x86_64 x86_64 GNU/Linux
#

基本情報の取得

コマンド(インスタンスタイプ順) ヘッダー無し

aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceType,InstanceId,ImageId,KeyName,Placement.AvailabilityZone,PrivateIpAddress,State.Name,VpcId,SubnetId]" --output text | sort
  • 実行結果
m4.large        i-xxxxxxxxxxxxxxxxx     ami-xxxxxxxxxxxxxxxxx   fk-test-key     ap-northeast-1c 10.88.1.127     stopped vpc-xxxxxxxxxxxxxxxxx   subnet-xxxxxxxxxxxxxxxxx
t2.micro        i-xxxxxxxxxxxxxxxxx     ami-xxxxxxxxxxxxxxxxx   fk-test-key     ap-northeast-1c None    terminated      None    None
t2.micro        i-xxxxxxxxxxxxxxxxx     ami-xxxxxxxxxxxxxxxxx   fk-test-key     ap-northeast-1a 10.0.10.60      stopped vpc-xxxxxxxxxxxxxxxxx   subnet-xxxxxxxxxxxxxxxxx

コマンド(インスタンスタイプ順) ヘッダー有り(NameTag付き)

echo "InstanceType InstanceId NameTag ImageId AvailabilityZone KeyName PrivateIpAddress State VpcId SubnetId" > /tmp/awscli.tmp;\
aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceId,ImageId,InstanceType,Placement.AvailabilityZone,KeyName,PrivateIpAddress,State.Name,VpcId,SubnetId]" --output text | sort > /tmp/awscli-tag-jonn1.tmp ;\
aws ec2 describe-tags --filters "Name=key,Values=Name" "Name=resource-type,Values=instance" --query "Tags[].[ResourceId,Value]" --output text | sort > /tmp/awscli-tag-jonn2.tmp;\
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp | awk '{print $4,$1,$2,$3,$5,$6,$7,$8,$9,$10}' | sort >> /tmp/awscli.tmp ;\
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/ ami-/ None ami-/g'| awk '{print $4,$1,$2,$3,$5,$6,$7,$8,$9,$10}' | sort >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp
  • 実行結果
InstanceType  InstanceId           NameTag                    ImageId                AvailabilityZone  KeyName      PrivateIpAddress  State    VpcId                  SubnetId
m4.large      i-xxxxxxxxxxxxxxxxx  fk-test-asav               ami-xxxxxxxxxxxxxxxxx  ap-northeast-1c   fk-test-key  10.88.1.127       stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx
t2.medium     i-xxxxxxxxxxxxxxxxx  fk-win-ad                  ami-xxxxxxxxxxxxxxxxx  ap-northeast-1a   fk-test-key  10.88.0.240       stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx
t2.medium     i-xxxxxxxxxxxxxxxxx  fk-test-jwin2016           ami-xxxxxxxxxxxxxxxxx  ap-northeast-1a   fk-test-key  10.88.0.241       stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx

コマンド(サブネット順) ヘッダー無し

aws ec2 describe-instances --query "Reservations[].Instances[].[SubnetId,InstanceId,ImageId,InstanceType,KeyName,Placement.AvailabilityZone,PrivateIpAddress,State.Name,VpcId]" --output text | sort
  • 実行結果
subnet-xxxxxxxxxxxxxxxxx        i-xxxxxxxxxxxxxxxxx     ami-xxxxxxxxxxxxxxxxx   t2.medium       fk-test-key     ap-northeast-1a 10.88.0.240     stopped vpc-xxxxxxxxxxxxxxxxx
subnet-xxxxxxxxxxxxxxxxx        i-xxxxxxxxxxxxxxxxx     ami-xxxxxxxxxxxxxxxxx   t3.medium       fk-test-key     ap-northeast-1a 10.88.0.111     stopped vpc-xxxxxxxxxxxxxxxxx
subnet-xxxxxxxxxxxxxxxxx        i-xxxxxxxxxxxxxxxxx     ami-xxxxxxxxxxxxxxxxx   t3.micro        fk-test-key     ap-northeast-1a 10.88.0.150     stopped vpc-xxxxxxxxxxxxxxxxx

コマンド(サブネット順) ヘッダー有り(NameTag付き)

echo "SubnetId InstanceId NameTag ImageId InstanceType AvailabilityZone KeyName PrivateIpAddress State VpcId" > /tmp/awscli.tmp;\
aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceId,ImageId,InstanceType,Placement.AvailabilityZone,KeyName,PrivateIpAddress,State.Name,VpcId,SubnetId]" --output text | sort > /tmp/awscli-tag-jonn1.tmp ;\
aws ec2 describe-tags --filters "Name=key,Values=Name" "Name=resource-type,Values=instance" --query "Tags[].[ResourceId,Value]" --output text | sort > /tmp/awscli-tag-jonn2.tmp;\
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp | awk '{print $10,$1,$2,$3,$4,$5,$6,$7,$8,$9}' | sort >> /tmp/awscli.tmp ;\
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/ ami-/ None ami-/g'| awk '{print $10,$1,$2,$3,$4,$5,$6,$7,$8,$9}' | sort >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp
  • 実行結果
SubnetId                  InstanceId           NameTag                    ImageId                InstanceType  AvailabilityZone  KeyName      PrivateIpAddress  State    VpcId
subnet-xxxxxxxxxxxxxxxxx  i-xxxxxxxxxxxxxxxxx  fk-win-ad                  ami-xxxxxxxxxxxxxxxxx  t2.medium     ap-northeast-1a   fk-test-key  10.88.0.240       stopped  vpc-xxxxxxxxxxxxxxxxx
subnet-xxxxxxxxxxxxxxxxx  i-xxxxxxxxxxxxxxxxx  fk-test-jwin2019           ami-xxxxxxxxxxxxxxxxx  t3.micro      ap-northeast-1a   fk-test-key  10.88.0.150       stopped  vpc-xxxxxxxxxxxxxxxxx
subnet-xxxxxxxxxxxxxxxxx  i-xxxxxxxxxxxxxxxxx  fk-test-jwin2016           ami-xxxxxxxxxxxxxxxxx  t2.medium     ap-northeast-1a   fk-test-key  10.88.0.241       stopped  vpc-xxxxxxxxxxxxxxxxx

コマンド(AZ順) ヘッダー無し

aws ec2 describe-instances --query "Reservations[].Instances[].[Placement.AvailabilityZone,InstanceId,ImageId,InstanceType,KeyName,PrivateIpAddress,State.Name,VpcId,SubnetId]" --output text | sort
  • 実行結果
ap-northeast-1a i-xxxxxxxxxxxxxxxxx     ami-xxxxxxxxxxxxxxxxx   t2.medium       fk-test-key     10.88.0.240     stopped vpc-xxxxxxxxxxxxxxxxx   subnet-xxxxxxxxxxxxxxxxx
ap-northeast-1a i-xxxxxxxxxxxxxxxxx     ami-xxxxxxxxxxxxxxxxx   t3.medium       fk-test-key     10.88.0.111     stopped vpc-xxxxxxxxxxxxxxxxx   subnet-xxxxxxxxxxxxxxxxx
ap-northeast-1a i-xxxxxxxxxxxxxxxxx     ami-xxxxxxxxxxxxxxxxx   t3.micro        fk-test-key     10.88.0.150     stopped vpc-xxxxxxxxxxxxxxxxx   subnet-xxxxxxxxxxxxxxxxx

コマンド(AZ順) ヘッダー有り(NameTag付き)

echo "AvailabilityZone InstanceId NameTag ImageId InstanceType KeyName PrivateIpAddress State VpcId SubnetId" > /tmp/awscli.tmp;\
aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceId,ImageId,InstanceType,Placement.AvailabilityZone,KeyName,PrivateIpAddress,State.Name,VpcId,SubnetId]" --output text | sort > /tmp/awscli-tag-jonn1.tmp ;\
aws ec2 describe-tags --filters "Name=key,Values=Name" "Name=resource-type,Values=instance" --query "Tags[].[ResourceId,Value]" --output text | sort > /tmp/awscli-tag-jonn2.tmp;\
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp | awk '{print $5,$1,$2,$3,$4,$6,$7,$8,$9,$10}' | sort >> /tmp/awscli.tmp ;\
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/ ami-/ None ami-/g'| awk '{print $5,$1,$2,$3,$4,$6,$7,$8,$9,$10}' | sort >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp
  • 実行結果
AvailabilityZone  InstanceId           NameTag                    ImageId                InstanceType  KeyName      PrivateIpAddress  State    VpcId                  SubnetId
ap-northeast-1a   i-xxxxxxxxxxxxxxxxx  fk-win-ad                  ami-xxxxxxxxxxxxxxxxx  t2.medium     fk-test-key  10.88.0.240       stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx
ap-northeast-1a   i-xxxxxxxxxxxxxxxxx  fk-test-jwin2019           ami-xxxxxxxxxxxxxxxxx  t3.micro      fk-test-key  10.88.0.150       stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx
ap-northeast-1a   i-xxxxxxxxxxxxxxxxx  fk-test-jwin2016           ami-xxxxxxxxxxxxxxxxx  t2.medium     fk-test-key  10.88.0.241       stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx

詳細情報の取得

コマンド ヘッダー無し

aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceId,ImageId,InstanceType,CpuOptions.CoreCount,CpuOptions.ThreadsPerCore,KeyName,Placement.AvailabilityZone,State.Name,VpcId,SubnetId,Architecture,EbsOptimized,EnaSupport,Hypervisor,SourceDestCheck,VirtualizationType,Monitoring.State,HibernationOptions.Configured]" --output text
  • 実行結果
i-xxxxxxxxxxxxxxxxx     ami-xxxxxxxxxxxxxxxxx   t2.medium       2       1       fk-test-key     ap-northeast-1c stopped vpc-xxxxxxxxxxxxxxxxx   subnet-xxxxxxxxxxxxxxxxx        x86_64  False   True    xen     False   hvm     disabled        False
i-xxxxxxxxxxxxxxxxx     ami-xxxxxxxxxxxxxxxxx   t2.micro        1       1       fk-test-key     ap-northeast-1c stopped vpc-xxxxxxxxxxxxxxxxx   subnet-xxxxxxxxxxxxxxxxx        x86_64  False   True    xen     True    hvm     disabled        False
i-xxxxxxxxxxxxxxxxx     ami-xxxxxxxxxxxxxxxxx   t3.large        1       2       fk-test-key     ap-northeast-1c stopped vpc-xxxxxxxxxxxxxxxxx   subnet-xxxxxxxxxxxxxxxxx        x86_64  True    True    xen     False   hvm     disabled        False

コマンド ヘッダー有り(NameTag付き)

echo "InstanceId NameTag ImageId InstanceType CoreCount ThreadsPerCore KeyName AvailabilityZone Name VpcId SubnetId Architecture EbsOptimized EnaSupport Hypervisor SourceDestCheck VirtualizationType Monitoring.State Hibernation" > /tmp/awscli.tmp;\
aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceId,ImageId,InstanceType,CpuOptions.CoreCount,CpuOptions.ThreadsPerCore,KeyName,Placement.AvailabilityZone,State.Name,VpcId,SubnetId,Architecture,EbsOptimized,EnaSupport,Hypervisor,SourceDestCheck,VirtualizationType,Monitoring.State,HibernationOptions.Configured]" --output text | sort > /tmp/awscli-tag-jonn1.tmp ;\
aws ec2 describe-tags --filters "Name=key,Values=Name" "Name=resource-type,Values=instance" --query "Tags[].[ResourceId,Value]" --output text | sort > /tmp/awscli-tag-jonn2.tmp;\
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp >> /tmp/awscli.tmp ;\
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/ ami-/ None ami-/g'| sort >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp 
  • 実行結果
InstanceId           NameTag                    ImageId                InstanceType  CoreCount  ThreadsPerCore  KeyName      AvailabilityZone  Name     VpcId                  SubnetId                  Architecture  EbsOptimized  EnaSupport  Hypervisor  SourceDestCheck  VirtualizationType  Monitoring.State  Hibernation
i-xxxxxxxxxxxxxxxxx  fk-win-ad                  ami-xxxxxxxxxxxxxxxxx  t2.medium     2          1               fk-test-key  ap-northeast-1a   stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx  x86_64        False         True        xen         True             hvm                 disabled          False
i-xxxxxxxxxxxxxxxxx  fk-test-jwin2019           ami-xxxxxxxxxxxxxxxxx  t3.micro      1          2               fk-test-key  ap-northeast-1a   stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx  x86_64        True          True        xen         True             hvm                 disabled          False
i-xxxxxxxxxxxxxxxxx  fk-test-vyos               ami-xxxxxxxxxxxxxxxxx  t3.large      1          2               fk-test-key  ap-northeast-1c   stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx  x86_64        True          True        xen         False            hvm                 disabled          False
i-xxxxxxxxxxxxxxxxx  fk-test-jwin2016           ami-xxxxxxxxxxxxxxxxx  t2.medium     2          1               fk-test-key  ap-northeast-1a   stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx  x86_64        False         True        xen         True             hvm                 disabled          False

インスタンスプロファイル一覧

コマンド ヘッダー無し

aws ec2 describe-iam-instance-profile-associations --query "IamInstanceProfileAssociations[].[InstanceId,IamInstanceProfile.Arn]" --output text | sort
  • 実行結果
i-xxxxxxxxxxxxxxxxx     arn:aws:iam::xxxxxxxxxxxx:instance-profile/base-ec2-ap-northeast-1
i-xxxxxxxxxxxxxxxxx     arn:aws:iam::xxxxxxxxxxxx:instance-profile/ec2-s3

コマンド ヘッダー有り(NameTag付き)

echo "InstanceId NameTag IamInstanceProfile" > /tmp/awscli.tmp;\
aws ec2 describe-iam-instance-profile-associations --query "IamInstanceProfileAssociations[].[InstanceId,IamInstanceProfile.Arn]" --output text | sort > /tmp/awscli-tag-jonn1.tmp;\
aws ec2 describe-tags --filters "Name=key,Values=Name" "Name=resource-type,Values=instance" --query "Tags[].[ResourceId,Value]" --output text | sort > /tmp/awscli-tag-jonn2.tmp;\
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp >> /tmp/awscli.tmp ;\
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/arn/ None arn/g' | sort >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp 
  • 実行結果
InstanceId           NameTag            IamInstanceProfile
i-xxxxxxxxxxxxxxxxx  fk-test-jwin2019   arn:aws:iam::xxxxxxxxxxxx:instance-profile/base-ec2-ap-northeast-1
i-xxxxxxxxxxxxxxxxx  fk-linux           arn:aws:iam::xxxxxxxxxxxx:instance-profile/ec2-s3

削除保護チェック

コマンド ヘッダー無し(1台だけ)

INSTNACEID="" ★インスタンスIDを設定してください。
aws ec2 describe-instance-attribute --instance-id ${INSTNACEID} --attribute disableApiTermination --query "[InstanceId,DisableApiTermination.Value]" --output text
  • 実行結果
i-xxxxxxxxxxxxxxxxx     False

コマンド ヘッダー有り(NameTag付き)

※インスタンスの台数分コマンドを実行するため、少し時間がかかります。 また、NameTagが付与されていないインスタンスは、列がずれます。

echo "InstanceId NameTag disableApiTermination" > /tmp/awscli.tmp;\
aws ec2 describe-instances --query "Reservations[].Instances[].InstanceId" --output text | tr "\t" "\n" | while read line; do aws ec2 describe-instance-attribute --instance-id ${line} --attribute disableApiTermination --query "[InstanceId,DisableApiTermination.Value]" --output text; done  | sort > /tmp/awscli-tag-jonn1.tmp;\
aws ec2 describe-tags --filters "Name=key,Values=Name" "Name=resource-type,Values=instance" --query "Tags[].[ResourceId,Value]" --output text | sort > /tmp/awscli-tag-jonn2.tmp;\
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp >> /tmp/awscli.tmp ;\
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp 
  • 実行結果
InstanceId           NameTag                    disableApiTermination
i-xxxxxxxxxxxxxxxxx  fk-win-ad                  False
i-xxxxxxxxxxxxxxxxx  fk-test-jwin2019           False
i-xxxxxxxxxxxxxxxxx  fk-test-vyos               False

終了時の動作

コマンド ヘッダー無し(1台だけ)

INSTNACEID=""
aws ec2 describe-instance-attribute --instance-id ${INSTNACEID} --attribute instanceInitiatedShutdownBehavior --query "[InstanceId,InstanceInitiatedShutdownBehavior.Value]" --output text
  • 実行結果
i-xxxxxxxxxxxxxxxxx     stop

コマンド ヘッダー有り(NameTag付き)

※インスタンスの台数分コマンドを実行するため、少し時間がかかります。 また、NameTagが付与されていないインスタンスは、列がずれます。

echo "InstanceId NameTag instanceInitiatedShutdownBehavior" > /tmp/awscli.tmp;\
aws ec2 describe-instances --query "Reservations[].Instances[].InstanceId" --output text | tr "\t" "\n" | while read line; do aws ec2 describe-instance-attribute --instance-id ${line} --attribute instanceInitiatedShutdownBehavior --query "[InstanceId,InstanceInitiatedShutdownBehavior.Value]" --output text; done  | sort > /tmp/awscli-tag-jonn1.tmp;\
aws ec2 describe-tags --filters "Name=key,Values=Name" "Name=resource-type,Values=instance" --query "Tags[].[ResourceId,Value]" --output text | sort > /tmp/awscli-tag-jonn2.tmp;\
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp >> /tmp/awscli.tmp ;\
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp 
  • 実行結果
InstanceId           NameTag                    instanceInitiatedShutdownBehavior
i-xxxxxxxxxxxxxxxxx  fk-win-ad                  stop
i-xxxxxxxxxxxxxxxxx  fk-test-jwin2019           stop
i-xxxxxxxxxxxxxxxxx  fk-test-vyos               stop

ステータスチェック

コマンド ヘッダー無し

aws ec2 describe-instance-status --query "InstanceStatuses[].[InstanceId,InstanceState.Name,InstanceStatus.Status,SystemStatus.Status]" --output text | sort
  • 実行結果
i-xxxxxxxxxxxxxxxxx     running ok      ok

コマンド ヘッダー有り(NameTag付き)

※NameTagが付与されていないインスタンスは、列がずれます。

echo "InstanceId NameTag State InstanceStatus SystemStatus" > /tmp/awscli.tmp;\
aws ec2 describe-instance-status --query "InstanceStatuses[].[InstanceId,InstanceState.Name,InstanceStatus.Status,SystemStatus.Status]" --output text | sort > /tmp/awscli-tag-jonn1.tmp;\
aws ec2 describe-tags --filters "Name=key,Values=Name" "Name=resource-type,Values=instance" --query "Tags[].[ResourceId,Value]" --output text | sort > /tmp/awscli-tag-jonn2.tmp;\
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp >> /tmp/awscli.tmp ;\
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp 
  • 実行結果
InstanceId           NameTag   State    InstanceStatus  SystemStatus
i-xxxxxxxxxxxxxxxxx  fk-linux  running  ok              ok

ENI情報の取得

※プライマリとセカンダリENIのみ出力。

コマンド ヘッダー無し

aws ec2 describe-instances --query "Reservations[].Instances[].\
[InstanceId,VpcId,SubnetId,\
NetworkInterfaces[0].Association.PublicIp,\
NetworkInterfaces[0].NetworkInterfaceId,NetworkInterfaces[0].PrivateIpAddresses[0].PrivateIpAddress,\
NetworkInterfaces[1].NetworkInterfaceId,NetworkInterfaces[0].PrivateIpAddresses[1].PrivateIpAddress]" --output text
  • 実行結果
i-xxxxxxxxxxxxxxxxx     vpc-xxxxxxxxxxxxxxxxx   subnet-xxxxxxxxxxxxxxxxx        None    eni-xxxxxxxxxxxxxxxxx   10.88.1.83      None    None
i-xxxxxxxxxxxxxxxxx     vpc-xxxxxxxxxxxxxxxxx   subnet-xxxxxxxxxxxxxxxxx        None    eni-xxxxxxxxxxxxxxxxx   10.88.1.153     None    None
i-xxxxxxxxxxxxxxxxx     vpc-xxxxxxxxxxxxxxxxx   subnet-xxxxxxxxxxxxxxxxx        None    eni-xxxxxxxxxxxxxxxxx   10.88.1.249     None    None

コマンド ヘッダー有り(NameTag付き)

echo "InstanceId NameTag VpcId SubnetId PublicIP PrimaryENI PrimaryPrivateIpAddress SeconderyENI SseconderyPrivateIpAddress" > /tmp/awscli.tmp;\
aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceId,VpcId,SubnetId,\
NetworkInterfaces[0].Association.PublicIp,\
NetworkInterfaces[0].NetworkInterfaceId,NetworkInterfaces[0].PrivateIpAddresses[0].PrivateIpAddress,\
NetworkInterfaces[1].NetworkInterfaceId,NetworkInterfaces[1].PrivateIpAddresses[0].PrivateIpAddress]" \
--output text  | sort > /tmp/awscli-tag-jonn1.tmp ;\
aws ec2 describe-tags --filters "Name=key,Values=Name" "Name=resource-type,Values=instance" --query "Tags[].[ResourceId,Value]" --output text | sort > /tmp/awscli-tag-jonn2.tmp;\
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp >> /tmp/awscli.tmp ;\
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/ vpc-/ None vpc-/g'>> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp
  • 実行結果
InstanceId           NameTag                    VpcId                  SubnetId                  PublicIP       PrimaryENI             PrimaryPrivateIpAddress  SeconderyENI           SseconderyPrivateIpAddress
i-xxxxxxxxxxxxxxxxx  fk-win-ad                  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx  None           eni-xxxxxxxxxxxxxxxxx  10.88.0.240              eni-xxxxxxxxxxxxxxxxx  10.88.0.220
i-xxxxxxxxxxxxxxxxx  fk-test-jwin2019           vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx  None           eni-xxxxxxxxxxxxxxxxx  10.88.0.150              None                   None
i-xxxxxxxxxxxxxxxxx  fk-test-vyos               vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx  None           eni-xxxxxxxxxxxxxxxxx  10.88.1.249              None                   None

ボリューム情報の取得

※3つのボリュームだけ出力。

コマンド ヘッダー無し

aws ec2 describe-instances --query "Reservations[].Instances[].\
[InstanceId,\
BlockDeviceMappings[0].DeviceName,BlockDeviceMappings[0].Ebs.DeleteOnTermination,BlockDeviceMappings[0].Ebs.VolumeId, \
BlockDeviceMappings[1].DeviceName,BlockDeviceMappings[1].Ebs.DeleteOnTermination,BlockDeviceMappings[1].Ebs.VolumeId, \
BlockDeviceMappings[2].DeviceName,BlockDeviceMappings[2].Ebs.DeleteOnTermination,BlockDeviceMappings[2].Ebs.VolumeId]" \
--output text
  • 実行結果
i-xxxxxxxxxxxxxxxxx     /dev/xvda       True    vol-0a6092980c505e7ca   None    None    None    None    None    None
i-xxxxxxxxxxxxxxxxx     /dev/sda1       True    vol-0f9005c084d48a632   None    None    None    None    None    None
i-xxxxxxxxxxxxxxxxx     /dev/xvda       True    vol-0e0f2f0c98bf36694   None    None    None    None    None    None

コマンド ヘッダー有り(NameTag付き)

echo "InstanceId NameTag DeviceName[1] DeleteOnTermination[1] VolumeId[1] DeviceName[2] DeleteOnTermination[2] VolumeId[2] DeviceName[3] DeleteOnTermination[3] VolumeId[3]" > /tmp/awscli.tmp;\
aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceId,BlockDeviceMappings[0].DeviceName,BlockDeviceMappings[0].Ebs.DeleteOnTermination,BlockDeviceMappings[0].Ebs.VolumeId,BlockDeviceMappings[1].DeviceName,BlockDeviceMappings[1].Ebs.DeleteOnTermination,BlockDeviceMappings[1].Ebs.VolumeId,BlockDeviceMappings[2].DeviceName,BlockDeviceMappings[2].Ebs.DeleteOnTermination,BlockDeviceMappings[2].Ebs.VolumeId]" --output text | sort > /tmp/awscli-tag-jonn1.tmp ;\
aws ec2 describe-tags --filters "Name=key,Values=Name" "Name=resource-type,Values=instance" --query "Tags[].[ResourceId,Value]" --output text | sort > /tmp/awscli-tag-jonn2.tmp;\
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp >> /tmp/awscli.tmp ;\
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/\/dev/None \/dev/g' >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp 
  • 実行結果
InstanceId           NameTag                    DeviceName[1]  DeleteOnTermination[1]  VolumeId[1]            DeviceName[2]  DeleteOnTermination[2]  VolumeId[2]            DeviceName[3]  DeleteOnTermination[3]  VolumeId[3]
i-xxxxxxxxxxxxxxxxx  fk-win-ad                  /dev/sda1      True                    vol-0ed26b83c38d909c8  None           None                    None                   None           None                    None
i-xxxxxxxxxxxxxxxxx  fk-test-jwin2019           /dev/sda1      True                    vol-085a475bc6367330e  None           None                    None                   None           None                    None
i-xxxxxxxxxxxxxxxxx  fk-linux                   /dev/xvda      True                    vol-0172865e01b54cad9  /dev/sdb       True                    vol-03a8f440ea016b119  None           None                    None

ボリュームの詳細情報取得

コマンド ヘッダー無し

aws ec2 describe-volumes --query "Volumes[].[Attachments[0].InstanceId,AvailabilityZone,Encrypted,Size,State,VolumeId,Iops,VolumeType]" --output text
  • 実行結果
i-xxxxxxxxxxxxxxxxxxx     ap-northeast-1a False   8       in-use  vol-xxxxxxxxxxxxxxxxxxxx   100     gp2
i-xxxxxxxxxxxxxxxxxxx     ap-northeast-1a False   8       in-use  vol-xxxxxxxxxxxxxxxxxxxx   100     gp2
None    ap-northeast-1a False   10      available       vol-xxxxxxxxxxxxxxxxxxxx   100     gp2

コマンド ヘッダー有り(NameTag付き)

echo "InstanceId NameTag VolumeId AvailabilityZone Encrypted State Size Iops VolumeType" > /tmp/awscli.tmp;\
aws ec2 describe-volumes --query "Volumes[].[Attachments[0].InstanceId,VolumeId,AvailabilityZone,Encrypted,Size,State,Iops,VolumeType]" --output text | sort > /tmp/awscli-tag-jonn1.tmp ;\
aws ec2 describe-tags --filters "Name=key,Values=Name" "Name=resource-type,Values=instance" --query "Tags[].[ResourceId,Value]" --output text | sort > /tmp/awscli-tag-jonn2.tmp;\
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp >> /tmp/awscli.tmp ;\
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/ vol-/ None vol-/g' | sort -r >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp 
  • 実行結果
InstanceId           NameTag                    VolumeId               AvailabilityZone  Encrypted  State  Size       Iops  VolumeType
i-xxxxxxxxxxxxxxxxxxx  yamazon-win-2019-english   vol-xxxxxxxxxxxxxxxxxxx  ap-northeast-1a   False      30     in-use     100   gp2
i-xxxxxxxxxxxxxxxxxxx  fk-test-jwin2019           vol-xxxxxxxxxxxxxxxxxxx  ap-northeast-1a   False      30     in-use     100   gp2
i-xxxxxxxxxxxxxxxxxxx  fk-test-vyos               vol-xxxxxxxxxxxxxxxxxxx  ap-northeast-1c   False      4      in-use     100   gp2

セキュリティグループ情報の取得

※5個だけ出力。

コマンド ヘッダー無し

aws ec2 describe-instances --query "Reservations[].Instances[].\
[InstanceId,PrivateIpAddress,\
SecurityGroups[0].GroupName,SecurityGroups[0].GroupId,\
SecurityGroups[1].GroupName,SecurityGroups[1].GroupId,\
SecurityGroups[2].GroupName,SecurityGroups[2].GroupId,\
SecurityGroups[3].GroupName,SecurityGroups[3].GroupId,\
SecurityGroups[4].GroupName,SecurityGroups[4].GroupId]" --output text
  • 実行結果
i-xxxxxxxxxxxxxxxxx     10.88.1.83      jira-t-5569     sg-024ecbe396a3b1803    fk-test-public-sg       sg-00c78fbf089ace674    fk-test-redis-sg        sg-01d8e3364ff438d84    None    None    None    None
i-xxxxxxxxxxxxxxxxx     10.88.1.153     fk-test-public-sg       sg-00c78fbf089ace674    None    None    None    None    None    None    None    None
i-xxxxxxxxxxxxxxxxx     10.88.1.249     fk-test-vpn-sg  sg-0bf94f91d38a95edf    None    None    None    None    None    None    None    None

コマンド ヘッダー有り(NameTag付き)

echo "InstanceId NameTag PrivateIpAddress GroupName[1] GroupId[1] GroupName[2] GroupId[2] GroupName[3] GroupId[3] GroupName[4] GroupId[4] GroupName[5] GroupId[5]" > /tmp/awscli.tmp;\
aws ec2 describe-instances --query "Reservations[].Instances[].[InstanceId,PrivateIpAddress,SecurityGroups[0].GroupName,SecurityGroups[0].GroupId,SecurityGroups[1].GroupName,SecurityGroups[1].GroupId,SecurityGroups[2].GroupName,SecurityGroups[2].GroupId,SecurityGroups[3].GroupName,SecurityGroups[3].GroupId,SecurityGroups[4].GroupName,SecurityGroups[4].GroupId]" --output text | sort > /tmp/awscli-tag-jonn1.tmp ;\
aws ec2 describe-tags --filters "Name=key,Values=Name" "Name=resource-type,Values=instance" --query "Tags[].[ResourceId,Value]" --output text | sort > /tmp/awscli-tag-jonn2.tmp;\
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp >> /tmp/awscli.tmp ;\
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/ 1/ None 1/g' >> /tmp/awscli.tmp ;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp 
  • 実行結果
InstanceId           NameTag                    PrivateIpAddress  GroupName[1]           GroupId[1]            GroupName[2]       GroupId[2]            GroupName[3]      GroupId[3]            GroupName[4]  GroupId[4]  GroupName[5]  GroupId[5]
i-xxxxxxxxxxxxxxxxx  fk-win-ad                  10.88.0.240       fk-test-ad-sg          sg-0754867b9033b3199  None               None                  None              None                  None          None        None          None
i-xxxxxxxxxxxxxxxxx  fk-test-jwin2019           10.88.0.150       fk-test-public-sg      sg-00c78fbf089ace674  None               None                  None              None                  None          None        None          None
i-xxxxxxxxxxxxxxxxx  fk-test-vyos               10.88.1.249       fk-test-vpn-sg         sg-0bf94f91d38a95edf  None               None                  None              None                  None          None        None          None

タグ情報の取得

コマンド ヘッダー無し

aws ec2 describe-instances --query "Reservations[].Instances[].\
[InstanceId,\
Tags[0].Key,Tags[0].Value,\
Tags[1].Key,Tags[1].Value,\
Tags[2].Key,Tags[2].Value,\
Tags[3].Key,Tags[3].Value,\
Tags[4].Key,Tags[4].Value,\
Tags[5].Key,Tags[5].Value,\
Tags[6].Key,Tags[6].Value,\
Tags[7].Key,Tags[7].Value,\
Tags[8].Key,Tags[8].Value,\
Tags[9].Key,Tags[9].Value]" --output text
  • 実行結果
i-xxxxxxxxxxxxxxxxx     Name    fk-linux-jira   None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None
i-xxxxxxxxxxxxxxxxx     Name    fk-test-jwin2019-02     None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None
i-xxxxxxxxxxxxxxxxx     Name    fk-test-vyos    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None
i-xxxxxxxxxxxxxxxxx     Name    fk-linux        Inspector       True    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None    None

コマンド ヘッダー有り

echo "InstanceId \
Tag-Key[1] Tag-Value[1] \
Tag-Key[2] Tag-Value[2] \
Tag-Key[3] Tag-Value[3] \
Tag-Key[4] Tag-Value[4] \
Tag-Key[5] Tag-Value[5] \
Tag-Key[6] Tag-Value[6] \
Tag-Key[7] Tag-Value[7] \
Tag-Key[8] Tag-Value[8] \
Tag-Key[9] Tag-Value[9] \
Tag-Key[10] Tag-Value[10]" > /tmp/awscli.tmp;\
aws ec2 describe-instances --query "Reservations[].Instances[].\
[InstanceId,\
Tags[0].Key,Tags[0].Value,\
Tags[1].Key,Tags[1].Value,\
Tags[2].Key,Tags[2].Value,\
Tags[3].Key,Tags[3].Value,\
Tags[4].Key,Tags[4].Value,\
Tags[5].Key,Tags[5].Value,\
Tags[6].Key,Tags[6].Value,\
Tags[7].Key,Tags[7].Value,\
Tags[8].Key,Tags[8].Value,\
Tags[9].Key,Tags[9].Value]" --output text >> /tmp/awscli.tmp;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
InstanceId           Tag-Key[1]  Tag-Value[1]               Tag-Key[2]  Tag-Value[2]  Tag-Key[3]  Tag-Value[3]  Tag-Key[4]  Tag-Value[4]  Tag-Key[5]  Tag-Value[5]  Tag-Key[6]  Tag-Value[6]  Tag-Key[7]  Tag-Value[7]  Tag-Key[8]  Tag-Value[8]  Tag-Key[9]  Tag-Value[9]  Tag-Key[10]  Tag-Value[10]
i-xxxxxxxxxxxxxxxxx  Name        fk-linux-jira              None        None          None        None          None        None          None        None          None        None          None        None          None        None          None        None          None         None
i-xxxxxxxxxxxxxxxxx  Name        fk-test-jwin2019-02        None        None          None        None          None        None          None        None          None        None          None        None          None        None          None        None          None         None
i-xxxxxxxxxxxxxxxxx  Name        fk-linux                   Inspector   True          None        None          None        None          None        None          None        None          None        None          None        None          None        None          None         None

おわりに

今回は、EC2関連のリソースを一覧取得するコマンドをご紹介いたしました。
次回は、これら情報をCSV形式で出力する方法をご紹介したいと思います。

福島 和弥 (記事一覧)

SRE2課

2019/10 入社

AWS CLIが好きです。