How to find out an ec2 instance's private and public IP via aws cli?
Solution 1:
Generally you can do it with --query filter.
If you need the private IP address only:
aws --region YOUR-AWS-REGION \
ec2 describe-instances \
--filters \
"Name=instance-state-name,Values=running" \
"Name=instance-id,Values=i-00914683ababcba7eb1" \
--query 'Reservations[*].Instances[*].[PrivateIpAddress]' \
--output text
If you need the public ip address only:
aws --region YOUR-AWS-REGION \
ec2 describe-instances \
--filters \
"Name=instance-state-name,Values=running" \
"Name=instance-id,Values=i-00914683ababcba7eb1" \
--query 'Reservations[*].Instances[*].[PublicIpAddress]' \
--output text
Or you can have both:
aws --region YOUR-AWS-REGION \
ec2 describe-instances \
--filters \
"Name=instance-state-name,Values=running" \
"Name=instance-id,Values=i-00914683ababcba7eb1" \
--query 'Reservations[*].Instances[*].[PrivateIpAddress, PublicIpAddress]' \
--output text
Of course you can have the output in json format too. Just change --output text
to --output json
More information about --query filters.