2

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][0][0],PrivateIpAddress,State.Name ]' --output json

4

It should just work if you add the Alias tag query as you did the Name query:

aws ec2 describe-instances --filters  "Name=instance-state-name,Values=running" --query 'Reservations[].Instances[].[ [Tags[?Key==`Name`].Value][0][0],[Tags[?Key==`Alias`].Value][0][0],PrivateIpAddress,State.Name ]' --output json
| improve this answer | |
  • Think I tried every possible way but that. Must have botched it up. – geniestacks May 24 '16 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.