Check if elastic IP is associated
The sub command ec2 describe-instances
doesn't show whether it has Elastic IPs or not.
Another sub command ec2 describe-addresses
shows InstanceId and PublicIp.
We can join them with join
command representing Name
.
$ aws ec2 describe-instances \
| jq -r '.Reservations[].Instances[]|[
.InstanceId,
(select(.Tags!=null).Tags[]|select(.Key=="Name").Value)
]|@tsv' | sort | tee ec2.tsv
i-00d7xxxxxxxxxxxxx app-01
i-01bdxxxxxxxxxxxxx db-03
i-04a7xxxxxxxxxxxxx app-02
i-0636xxxxxxxxxxxxx db-01
i-0808xxxxxxxxxxxxx db-02
$ aws ec2 describe-addresses \
| jq -r '.[][]|[.InstanceId, .PublicIp]|@tsv' \
| sort | tee ip.tsv
i-01bdxxxxxxxxxxxxx 1x.1x3.xxx.xxx
i-0636xxxxxxxxxxxxx 5x.1x8.xxx.xxx
i-0808xxxxxxxxxxxxx 5x.1x7.xxx.xxx
$ join -a1 ec2.tsv ip.tsv
i-00d7xxxxxxxxxxxxx app-01
i-01bdxxxxxxxxxxxxx db-03 1x.1x3.xxx.xxx
i-04a7xxxxxxxxxxxxx app-02
i-0636xxxxxxxxxxxxx db-01 5x.1x8.xxx.xxx
i-0808xxxxxxxxxxxxx db-02 5x.1x7.xxx.xxx
One-liner works.
join -a1 <(aws ec2 describe-instances \
| jq -r '.Reservations[].Instances[]|
[
.InstanceId,
(select(.Tags!=null).Tags[]|select(.Key=="Name").Value)
]
|@tsv' | sort) \
<(aws ec2 describe-addresses \
| jq -r '.[][]|[.InstanceId, .PublicIp]|@tsv' | sort)