I'm attempting to dynamically create a hosts file mapping with all my ec2 instance private ip's, to use as the /etc/hosts on my ec2 remote ssh server.
I have instances tagged with their normal "Name" eg. prod-database-01 and an "Alias" tag eg. db-01. The goal being to be able to use "ssh db-01" from my ssh box and have the right mapping even if the server was just spun-up or autoscaled.
For any instance with State "Running" ie. active servers, I'm trying to get the cli to output the Name, Alias and private IP address to JSON or something like that, for which I can then create a hosts file.
However, I'm stuck at trying to output the second "Alias" tag.
This is what I have so far which outputs "Name","PrivateIP",State".
Wondering how I can iterate through and output the additional Alias tag as well?
aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query 'Reservations.Instances.[ [Tags[?Key==`Name`].Value],PrivateIpAddress,State.Name ]' --output json