How to get the secondary private IP address of an Amazon EC2 instance

amazon ec2amazon-vpcamazon-web-services

I'm assigning automatically a secondary private IPv4 address during launch of my instances. To configure Ubuntu to recognize this secondary IP I need to run

ip addr add 10.0.1.15/24 dev eth0

To access the primary private IP address I have found this command (described in the AWS Docs and in figaros question):

curl http://169.254.169.254/latest/meta-data/local-ipv4

But how can I get the secondary IP address to run the ip command automatically on startup? For Amazon Linux Systems there's the ec2-net-utils package wich can handle this automatically.

Best Answer

You can get secondary address with 2 commands.

Get mac address of your network interface

MAC=`curl http://169.254.169.254/latest/meta-data/mac` 

Get all IP addresses for this mac address

curl http://169.254.169.254/latest/meta-data/network/interfaces/macs/${MAC}/local-ipv4s