18

I have list of IP addressed, I want to find if instances associated with the IP address are still running or terminated. I am launching and terminating lot of instances on daily basis, just want to remove their certificates from puppetmaster.

If there is any alternative method, I can achieve my goal, I can do that.

5 Answers 5

27

aws ec2 describe-instances --filter Name=ip-address,Values=IP_1,..IP_N

Should do what you need.

use the filter name of private-ip-address to select using private address in your VPC.

Pipe through something like

jq -r '.Reservations[].Instances[] | .InstanceId, .PublicIpAddress'

if you want the corresponding InstanceID

1
  • 5
    You don't need to use jq, you can pass a --query option to extract parts of the output: aws ec2 describe-instances --filter Name=ip-address,Values=IP_1,..IP_N --query 'Reservations[].Instances[].[InstanceId,PublicIpAddress]See Controlling Command Output from the AWS Command Line Interface for more details. Note that --query uses JMESPath, which is slightly different from jq.
    – markusk
    Sep 13, 2018 at 8:52
12

You can use --query and --output formats if you want to use this in a bash script.

aws ec2 describe-instances --filter Name=private-ip-address,Values=x.x.x.x --query 'Reservations[].Instances[].InstanceId' --output text

This will give you text response without json formatting

i-03c1ad0d6abe32323
1

An alternative approach would be to use CloudWatch Events to listen for EC2 instances being terminated, and have the listener (which could be a Lambda function, or some custom service listening to SQS) remove the corresponding certificates from Puppet.

References:

0

You can use grep

 aws ec2 describe-instances --filter Name=private-ip-address,Values=x.x.x.x | grep 'InstanceId'

Note: dont use InstanceId keyword in Tag or Name

0
aws ec2 describe-instances --filter Name=private-ip-address,Values=10.xxx.xx.xx,10.yyy.yy.yy --query 'Reservations[].Instances[].InstanceId' --output text
1
  • You can apply multiple instance id's or single ip as well. Redirect above command to a file to store instance id's. aws ec2 describe-instances --filter Name=private-ip-address,Values=10.xxx.xx.xx,10.yyy.yy.yy --query 'Reservations[].Instances[].InstanceId' --output text >> InstanceId.txt Jan 16, 2023 at 9:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .