こんにちは。AWS CLIが好きな福島です。
今回は、以下のブログで紹介したコマンドを全部一撃で取得するコマンドをご紹介いたします。
全部といいつつ、上記ブログでご紹介した以下は除きます。
- インスタンスプロファイル一覧
- 削除保護チェック
- 終了時の動作
- ステータスチェック
- ボリュームの詳細情報取得
過去にネットワーク関連のリソース一覧を取得するコマンドをご紹介しておりますので、 ご興味がある方は、こちらもぜひ読んでみてください。
実行環境
今回、コマンドを実行した環境は、以下の通りとなります。
(本記事でご紹介しているコマンドの中には、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 #
EC2の情報取得
※注意点
- ENI:プライマリ、セカンダリのみ表示
- EBS:3つのボリュームのみ表示
- SG:5つのSGのみ表示
- TAG:10個まで表示
コマンド
※すごい長いですが、全部一気に実行してください。
echo "InstanceId,NameTag,ImageId,InstanceType,CoreCount,ThreadsPerCore,\ KeyName,AvailabilityZone,State,VpcId,SubnetId,\ PublicIP,PrimaryENI,\ PrimaryPrivateIpAddress,SeconderyENI,SseconderyPrivateIpAddress,\ DeviceName[1],VolumeId[1],DeleteOnTermination[1],\ DeviceName[2],VolumeId[2],DeleteOnTermination[2],\ DeviceName[3],VolumeId[3],DeleteOnTermination[3],\ GroupName[1],GroupId[1],\ GroupName[2],GroupId[2],\ GroupName[3],GroupId[3],\ GroupName[4],GroupId[4],\ GroupName[5],GroupId[5],\ 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],\ Architecture,EbsOptimized,EnaSupport,Hypervisor,SourceDestCheck,VirtualizationType,DetailMonitoring,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,\ NetworkInterfaces[0].Association.PublicIp,\ NetworkInterfaces[0].NetworkInterfaceId,NetworkInterfaces[0].PrivateIpAddresses[0].PrivateIpAddress,\ NetworkInterfaces[1].NetworkInterfaceId,NetworkInterfaces[1].PrivateIpAddresses[0].PrivateIpAddress,\ 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,\ 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,\ 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,\ 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' >> /tmp/awscli.tmp ;\ cat /tmp/awscli.tmp | tr " " "," > awscli-ec2.csv;\ rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp
- 実行後
カレントディレクトリにある「awscli-ec2.csv」を開きます。
イメージ
コマンドの説明
①ヘッダーの作成
echo "InstanceId,NameTag,ImageId,InstanceType,CoreCount,ThreadsPerCore,\ KeyName,AvailabilityZone,State,VpcId,SubnetId,\ PublicIP,PrimaryENI,\ PrimaryPrivateIpAddress,SeconderyENI,SseconderyPrivateIpAddress,\ DeviceName[1],VolumeId[1],DeleteOnTermination[1],\ DeviceName[2],VolumeId[2],DeleteOnTermination[2],\ DeviceName[3],VolumeId[3],DeleteOnTermination[3],\ GroupName[1],GroupId[1],\ GroupName[2],GroupId[2],\ GroupName[3],GroupId[3],\ GroupName[4],GroupId[4],\ GroupName[5],GroupId[5],\ 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],\ Architecture,EbsOptimized,EnaSupport,Hypervisor,SourceDestCheck,VirtualizationType,DetailMonitoring,Hibernation" > /tmp/awscli.tmp;\
②EC2の情報取得
aws ec2 describe-instances --query "Reservations[].Instances[].\ [InstanceId,ImageId,InstanceType,CpuOptions.CoreCount,CpuOptions.ThreadsPerCore,\ KeyName,Placement.AvailabilityZone,State.Name,VpcId,SubnetId,\ NetworkInterfaces[0].Association.PublicIp,\ NetworkInterfaces[0].NetworkInterfaceId,NetworkInterfaces[0].PrivateIpAddresses[0].PrivateIpAddress,\ NetworkInterfaces[1].NetworkInterfaceId,NetworkInterfaces[1].PrivateIpAddresses[0].PrivateIpAddress,\ BlockDeviceMappings[0].DeviceName,BlockDeviceMappings[0].Ebs.VolumeId,BlockDeviceMappings[0].Ebs.DeleteOnTermination,\ BlockDeviceMappings[1].DeviceName,BlockDeviceMappings[1].Ebs.VolumeId,BlockDeviceMappings[1].Ebs.DeleteOnTermination,\ BlockDeviceMappings[2].DeviceName,BlockDeviceMappings[2].Ebs.VolumeId,BlockDeviceMappings[2].Ebs.DeleteOnTermination,\ 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,\ 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,\ Architecture,EbsOptimized,EnaSupport,Hypervisor,SourceDestCheck,VirtualizationType,Monitoring.State,HibernationOptions.Configured]" --output text | sort > /tmp/awscli-tag-jonn1.tmp;\
③インスタンスIDとNameタグの一覧を取得
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;\
④②と③で生成したファイルをインスタンスIDを基にjoin
join /tmp/awscli-tag-jonn2.tmp /tmp/awscli-tag-jonn1.tmp >> /tmp/awscli.tmp ;\
⑤joinできなかったインスタンスの出力(Nameタグがないインスタンスはjoinができない)
join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/ami-/None ami-/g' >> /tmp/awscli.tmp ;\
⑥スペースをカンマ区切りにし、CSVファイルの作成
cat /tmp/awscli.tmp | tr " " "," > awscli-ec2.csv;\
⑦tmpファイルの削除
rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp
おまけ①
上記は、タグなどにスペースが入ると列がずれてしまいます。 スペースが入っていても列をずらさないコマンドは、以下の通りです。
echo "InstanceId,NameTag,ImageId,InstanceType,CoreCount,ThreadsPerCore,\ KeyName,AvailabilityZone,State,VpcId,SubnetId,\ PublicIP,PrimaryENI,\ PrimaryPrivateIpAddress,SeconderyENI,SseconderyPrivateIpAddress,\ DeviceName[1],VolumeId[1],DeleteOnTermination[1],\ DeviceName[2],VolumeId[2],DeleteOnTermination[2],\ DeviceName[3],VolumeId[3],DeleteOnTermination[3],\ GroupName[1],GroupId[1],\ GroupName[2],GroupId[2],\ GroupName[3],GroupId[3],\ GroupName[4],GroupId[4],\ GroupName[5],GroupId[5],\ 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],\ Architecture,EbsOptimized,EnaSupport,Hypervisor,SourceDestCheck,VirtualizationType,DetailMonitoring,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,\ NetworkInterfaces[0].Association.PublicIp,\ NetworkInterfaces[0].NetworkInterfaceId,NetworkInterfaces[0].PrivateIpAddresses[0].PrivateIpAddress,\ NetworkInterfaces[1].NetworkInterfaceId,NetworkInterfaces[1].PrivateIpAddresses[0].PrivateIpAddress,\ BlockDeviceMappings[0].DeviceName,BlockDeviceMappings[0].Ebs.VolumeId,BlockDeviceMappings[0].Ebs.DeleteOnTermination,\ BlockDeviceMappings[1].DeviceName,BlockDeviceMappings[1].Ebs.VolumeId,BlockDeviceMappings[1].Ebs.DeleteOnTermination,\ BlockDeviceMappings[2].DeviceName,BlockDeviceMappings[2].Ebs.VolumeId,BlockDeviceMappings[2].Ebs.DeleteOnTermination,\ 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,\ 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,\ Architecture,EbsOptimized,EnaSupport,Hypervisor,SourceDestCheck,VirtualizationType,Monitoring.State,HibernationOptions.Configured]" --output table | grep -Ev "\-\-\-\-\-\-\-\-|DescribeInstances" | sed -e 's/^|//g' -e 's/|/ |/g'| 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 table | sed 's/|$//g' | grep -Ev "\-\-\-\-\-\-\-\-|DescribeTags" | sed 's/^|//g' |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' >> /tmp/awscli.tmp ;\ cat /tmp/awscli.tmp | tr "\|" "," | sed 's/^|//g'> awscli-ec2.csv;\ rm /tmp/awscli.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp
おまけ②
wiki形式で出力。 (ボリュームの詳細情報およびインスタンスプロファイル情報込み。)
echo "|InstanceId|NameTag|ImageId|InstanceType|CoreCount|ThreadsPerCore|\ KeyName|AvailabilityZone|State|VpcId|SubnetId|\ PublicIP|PrimaryENI|\ PrimaryPrivateIpAddress|SeconderyENI|SseconderyPrivateIpAddress|\ DeviceName[1]|DeleteOnTermination[1]|VolumeId[1]|\ DeviceName[2]|DeleteOnTermination[2]|VolumeId[2]|\ DeviceName[3]|DeleteOnTermination[3]|VolumeId[3]|\ GroupName[1]|GroupId[1]|\ GroupName[2]|GroupId[2]|\ GroupName[3]|GroupId[3]|\ GroupName[4]|GroupId[4]|\ GroupName[5]|GroupId[5]|\ 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]|\ Architecture|EbsOptimized|EnaSupport|Hypervisor|SourceDestCheck|VirtualizationType|DetailMonitoring|Hibernation|" > /tmp/awscli.tmp;\ echo "|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|" >> /tmp/awscli.tmp;\ aws ec2 describe-instances --query "Reservations[].Instances[].\ [InstanceId,ImageId,InstanceType,CpuOptions.CoreCount,CpuOptions.ThreadsPerCore,\ KeyName,Placement.AvailabilityZone,State.Name,VpcId,SubnetId,\ NetworkInterfaces[0].Association.PublicIp,\ NetworkInterfaces[0].NetworkInterfaceId,NetworkInterfaces[0].PrivateIpAddresses[0].PrivateIpAddress,\ NetworkInterfaces[1].NetworkInterfaceId,NetworkInterfaces[1].PrivateIpAddresses[0].PrivateIpAddress,\ 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,\ 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,\ 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,\ 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 | tr " " "|" | sed -e 's/^/\|/g' -e 's/$/\|/g' | sort -t\| -k3 >> /tmp/awscli.tmp;\ join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/ ami-/ None ami-/g'| tr " " "|" | sed -e 's/^/\|/g' -e 's/$/\|/g' | sort -t\| -k3 >> /tmp/awscli.tmp;\ echo "## 基本情報";\ cat /tmp/awscli.tmp | cut -d\| -f1-10 | sed 's/$/\|/g';\ echo "## ネットワーク";\ cat /tmp/awscli.tmp | cut -d\| -f1-3,11-17 | sed 's/$/\|/g';\ echo "## ボリューム情報";\ cat /tmp/awscli.tmp | cut -d\| -f1-3,18-26 | sed 's/$/\|/g';\ echo "## ボリュームの詳細情報";\ echo "|InstanceId|NameTag|VolumeId|AvailabilityZone|Encrypted|State|Size|Iops|VolumeType|" > /tmp/awscli-vol.tmp;\ echo "|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|" >> /tmp/awscli-vol.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 | tr ' ' '|' | sed -e 's/^/\|/g' -e 's/$/\|/g' | sort>> /tmp/awscli-vol.tmp ;\ join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/ vol-/ None vol-/g' | tr ' ' '|' | sed -e 's/^/\|/g' -e 's/$/\|/g' | sort >> /tmp/awscli-vol.tmp;\ cat /tmp/awscli-vol.tmp;\ echo "## SG情報";\ cat /tmp/awscli.tmp | cut -d\| -f1-3,27-36 | sed 's/$/\|/g';\ echo "## インスタンスプロファイル情報";\ echo "|InstanceId|NameTag|IamInstanceProfile|" > /tmp/awscli-profile.tmp;\ echo "|---|---|---|" >> /tmp/awscli-profile.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 | tr ' ' '|' | sed -e 's/^/\|/g' -e 's/$/\|/g' | sort >> /tmp/awscli-profile.tmp ;\ join -v 1 /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp | sed 's/arn/ None arn/g' | tr ' ' '|' | sed -e 's/^/\|/g' -e 's/$/\|/g' | sort >> /tmp/awscli-profile.tmp;\ cat /tmp/awscli-profile.tmp;\ echo "## タグ情報";\ cat /tmp/awscli.tmp | cut -d\| -f1-3,37-56 | sed 's/$/\|/g';\ echo "## その他情報";\ cat /tmp/awscli.tmp | cut -d\| -f1-3,57-63 | sed 's/$/\|/g';\ rm /tmp/awscli.tmp /tmp/awscli-vol.tmp /tmp/awscli-profile.tmp /tmp/awscli-tag-jonn1.tmp /tmp/awscli-tag-jonn2.tmp
おわりに
今回は、以下のブログで紹介したコマンドを全部一撃で取得するコマンドをご紹介いたしました。
次回は、IAM関連のコマンドをご紹介したいと思います。