Is it possible to get aws ec2 instance id based on its IP address

Solution 1:

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

Solution 2:

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

Solution 3:

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:

  • Automating Amazon EC2 with CloudWatch Events
  • New CloudWatch Events – Track and Respond to Changes to Your AWS Resources