How to get the secondary private IP address of an Amazon EC2 instance
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.
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
Because of antrost answer (his curl request returns all ip addresses) I was able to write the following bash script wich returns the secondary private IP only and configures Ubuntu to recognize it:
#!/bin/bash
MAC=`curl -s http://169.254.169.254/latest/meta-data/mac`
ADDRESSES=`curl -s http://169.254.169.254/latest/meta-data/network/interfaces/macs/${MAC}/local-ipv4s`
arr=($ADDRESSES)
if [ ${#arr[@]} -ge 2 ];
then
ip addr add ${arr[1]}/24 dev eth0
fi