Search code examples
amazon-web-servicesamazon-ec2aws-sdkaws-sdk-jsaws-sdk-nodejs

Lookup EC2 resource ID to which a known public IP address is associated, in Node


Given a known public IP address (e.g. elastic IP) how can I look up the resource ID of the EC2 instance associated with it, in Node?

My ultimate intention is to start/stop the EC2 instance associated with a given public IP address from Node running in another EC2 instance in the same AWS account. I presume that I first have to find the resource ID of the instance I want to start/stop so that I can then call ec2.startInstances() with the resource ID, but at runtime I know only the public IP address of the instance to start/stop.


Solution

  • Use the describeAddresses Node API. It will list your ElasticIPs and which InstanceID each is assigned to. You can call the startInstances() from this.

    https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/EC2.html#describeAddresses-property

    For a non-elasticIP, you could perhaps add a startup script that causes the Instance to send its environment details somewhere you can store and reference instead?