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

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

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

その他のAWS CLI関連の記事

私はよくqueryを使うため、queryの使い方が分からない方は、こちらを参照していただけますと幸いです。

実行環境

今回、コマンドを実行した環境は、以下の通りとなります。
(本記事でご紹介しているコマンドの中には、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 CLIの構造は以下の通りです。

aws <command> <subcommand> [options and parameters]

上記を前提に今回使う <command>,<subcommand>は、以下の通りです。

<command>

  • ec2

<subcommand>

  • â‘ describe-instances
    →EC2の情報を取得できます。
  • â‘¡describe-iam-instance-profile-associations
    → EC2のインスタンスプロファイルの情報を取得できます。
  • â‘¢describe-tags
    →Tagの情報を取得できます。
  • â‘£describe-instance-attribute
    →EC2の詳細設定の情報を取得できます。
  • ⑤describe-instance-status
    →EC2のステータスを取得できます。
  • â‘¥describe-volumes
    →EBSの情報を取得できます。

基本情報の取得

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

aws ec2 describe-instances --query "Reservations[].Instances[].[Tags[?Key=='Name'] | [0].Value,InstanceId,InstanceType,ImageId,KeyName,Placement.AvailabilityZone,PrivateIpAddress,State.Name,VpcId,SubnetId]" --output text
  • 実行結果
fk-linux-jira   i-0f6126b7aeedfabd6     t2.medium       ami-041581098aa702a3b   fk-test-key     ap-northeast-1c 10.88.1.83      stopped vpc-0fee138d3e0deef81   subnet-03fbedc39e6211234
fk-test-vyos    i-050536efdd9dc1126     t3.large        ami-0bcd9399d3bfa0a0e   fk-test-key     ap-northeast-1c 10.88.1.249     stopped vpc-0fee138d3e0deef81   subnet-03fbedc39e6211234
fk-test-smtp    i-0807173c03bac7939     t4g.micro       ami-0ff2715db3ddfcaf5   fk-test-key     ap-northeast-1c 10.88.1.46      stopped vpc-0fee138d3e0deef81   subnet-03fbedc39e6211234

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

echo "NameTag InstanceId ImageId InstanceType  AvailabilityZone KeyName PrivateIpAddress State VpcId SubnetId" > /tmp/awscli.tmp;\
aws ec2 describe-instances --query "Reservations[].Instances[].[Tags[?Key=='Name'] | [0].Value,InstanceId,ImageId,InstanceType,Placement.AvailabilityZone,KeyName,PrivateIpAddress,State.Name,VpcId,SubnetId]" --output text >> /tmp/awscli.tmp;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
NameTag               InstanceId           ImageId                InstanceType  AvailabilityZone  KeyName      PrivateIpAddress  State    VpcId                  SubnetId
fk-test-asav      i-xxxxxxxxxxxxxxxxx          ami-xxxxxxxxxxxxxxxxx  m4.large ap-northeast-1c   fk-test-key  10.88.1.127       stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx
fk-win-ad     i-xxxxxxxxxxxxxxxxx              ami-xxxxxxxxxxxxxxxxx  t2.medium ap-northeast-1a   fk-test-key  10.88.0.240       stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx
fk-test-jwin2016      i-xxxxxxxxxxxxxxxxx            ami-xxxxxxxxxxxxxxxxx t2.medium ap-northeast-1a   fk-test-key  10.88.0.241       stopped  vpc-xxxxxxxxxxxxxxxxx  subnet-xxxxxxxxxxxxxxxxx

詳細情報の取得

コマンド ヘッダー無し

aws ec2 describe-instances --query "Reservations[].Instances[].[Tags[?Key=='Name'] | [0].Value,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
  • 実行結果
fk-linux-jira   i-0f6126b7aeedfabd6     ami-041581098aa702a3b   t2.medium       2       1       fk-test-key     ap-northeast-1c stopped vpc-0fee138d3e0deef81   subnet-03fbedc39e6211234        x86_64  False   True    xen     True    hvm     disabled        False
fk-test-vyos    i-050536efdd9dc1126     ami-0bcd9399d3bfa0a0e   t3.large        1       2       fk-test-key     ap-northeast-1c stopped vpc-0fee138d3e0deef81   subnet-03fbedc39e6211234        x86_64  True    True    xen     False   hvm     disabled        False
fk-test-smtp    i-0807173c03bac7939     ami-0ff2715db3ddfcaf5   t4g.micro       2       1       fk-test-key     ap-northeast-1c stopped vpc-0fee138d3e0deef81   subnet-03fbedc39e6211234        arm64   True    True    xen     True    hvm     disabled        False

コマンド ヘッダー有り

echo "NameTag InstanceId 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[].[Tags[?Key=='Name'] | [0].Value,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.tmp;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.tmp
  • 実行結果
NameTag                                                        InstanceId           ImageId                InstanceType  CoreCount  ThreadsPerCore  KeyName      AvailabilityZone  Name     VpcId                  SubnetId                  Architecture  EbsOptimized  EnaSupport  Hypervisor  SourceDestCheck  VirtualizationType  Monitoring.State  Hibernation
aws-cloud9-aws-code-hands-on-0f7eeec411fd49be880ebf5a4791df17  i-03767ce5637a45a1a  ami-0ea42ac15b5216c72  t2.micro      1          1               None         ap-northeast-1a   stopped  vpc-0fee138d3e0deef81  subnet-0093d7475de79313b  x86_64        False         True        xen         True             hvm                 disabled          False
fk-linux                                                       i-0808672558492fde8  ami-052652af12b58691f  t3.micro      1          2               fk-test-key  ap-northeast-1a   stopped  vpc-0fee138d3e0deef81  subnet-0093d7475de79313b  x86_64        True          True        xen         True             hvm                 disabled          False
fk-linux-jira                                                  i-0f6126b7aeedfabd6  ami-041581098aa702a3b  t2.medium     2          1               fk-test-key  ap-northeast-1c   stopped  vpc-0fee138d3e0deef81  subnet-03fbedc39e6211234  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[].[Tags[?Key=='Name'] | [0].Value,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 "NameTag InstanceId VpcId SubnetId PublicIP PrimaryENI PrimaryPrivateIpAddress SeconderyENI SseconderyPrivateIpAddress" > /tmp/awscli.tmp;\
aws ec2 describe-instances --query "Reservations[].Instances[].[Tags[?Key=='Name'] | [0].Value,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.tmp;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.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 "NameTag InstanceId 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[].[Tags[?Key=='Name'] | [0].Value,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.tmp;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.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[].[Tags[?Key=='Name'] | [0].Value,Attachments[0].InstanceId,VolumeId,AvailabilityZone,Encrypted,Size,State,Iops,VolumeType]" --output text | sort >> /tmp/awscli.tmp;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.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 "NameTag InstanceId 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[].[Tags[?Key=='Name'] | [0].Value,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.tmp;\
column -t /tmp/awscli.tmp;\
rm /tmp/awscli.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形式で出力する方法をご紹介したいと思います。

福島 和弥 (記事一覧)

2019/10 入社

AWS CLIが好きです。

"; doc.innerHTML = entry_notice + doc.innerHTML; }
' } }) e.innerHTML = codeBlock; });