I want to list the public IP addresses of my EC2 instances using Bash, separated by a delimiter (space or a new-line).
I tried to pipe the output to jq with aws ec2 describe-instances | jq
, but can't seem to isolate just the IP addresses.
Can this be done by aws
alone, specifying arguments to jq
, or something else entirely?
Query for each PublicIPaddress and the Name Tag, handling when Name isn't set
aws ec2 describe-instances --filter "Name=instance-state-name,Values=running" --query "Reservations[].Instances[].[PublicIpAddress, Tags[?Key=='Name'].Value|[0]]" --output text
The below command would list the IP addresses of all your running EC2 instances
Hope that answers your query.
Refer: http://docs.aws.amazon.com/cli/latest/userguide/controlling-output.html
Directly from the aws cli:
You can use instance metadata so you can run the following command from the ec2 instance:
and it will give you the public IP of the instance. If you want the private IP, you will run