Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colegatron/79bc915e1669839f47d5 to your computer and use it in GitHub Desktop.
Save colegatron/79bc915e1669839f47d5 to your computer and use it in GitHub Desktop.
AWS EC2 dinamically add 2nd ENI to instance (and only a 2nd
INSTID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
echo "Getting instance info..."
aws ec2 describe-instances --instance-id $INSTID --region eu-west-1 | jq .Reservations > /tmp/descinstance.json
if [ $( cat /tmp/descinstance.json | jq '.[].Instances[0].NetworkInterfaces | length' ) -gt 1 ]; then
echo "[WARNING] Instance already has more than one ENI"
else
echo "Creating ENI..."
aws ec2 create-network-interface --subnet-id subnet-14416563 --description "Nic2 SaltMaster DevTest Network [$INSTID]" --groups sg-28dc214d --region eu-west-1 > /tmp/create-eni.json
echo "Getting ENI id..."
NICID=$(cat /tmp/create-eni.json | jq .NetworkInterface.NetworkInterfaceId | sed "s/\"//g")
# CAUTION!! ENI HAS TO RESIDE IN THE SAME AVAILABILITY ZONE THAN THE INSTANCE (eu-west-1c o el que sea)
echo "Attaching ENI [$NICID]..."
AZ=$( curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone )
aws ec2 attach-network-interface --network-interface-id $NICID --instance-id $INSTID --device-index 1 --region $AZ
aws ec2 attach-network-interface --network-interface-id $NICID --instance-id $INSTID --device-index 1 --region eu-west-1
echo "Bringing up eth1..."
sudo cp /etc/network/interfaces.d/eth0.cfg /etc/network/interfaces.d/eth1.cfg
sudo sed -i "s/eth0/eth1/g" /etc/network/interfaces.d/eth1.cfg
sudo ifup eth1
echo "ETH1 up..."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment