When I first started to use AWS CLI, I felt overwhelmed, and I wondered how long it would take me to be familiar with the tool. Fast forward to many months later, I find the AWS CLI very pleasant to use. For example, there are same patterns and the CLI reference page online is quite useful.

Anyway, like many thing I’ve learned about Linux, I learned and could retain the information better with practice and document. So, I’m gonna just dump the commands that I need and learn here to help my future self should I need to find the same commands.

Here is the first one. Let’s say I have a known private IP of an EC2 instance on AWS as 172.31.26.43. How could I find its instance ID using AWS CLI?

We can use ‘describe-instances’ API by filtering for instance that match the desired private IP. Optionally, we can use --query, the client side query, to display just the instance of the instance. Here is an example:

➜ aws ec2 describe-instances --filters "Name=private-ip-address,Values=172.31.26.43" \
    --query 'Reservations[].Instances[].InstanceId'
[
    "i-0a4cafb4d8095ef91"
]

To learn more about this API, check at the references below.

References: