Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbrannigan/50c717d72b52293563e4777ed788b140 to your computer and use it in GitHub Desktop.
Save mbrannigan/50c717d72b52293563e4777ed788b140 to your computer and use it in GitHub Desktop.
############################################
# Instance info
############################################
INSTANCE_ID=$(/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id)
EC2_AVAIL_ZONE=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone`
REGION="`echo \"$EC2_AVAIL_ZONE\" | /bin/sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
ENI_ID="${ENI_ID}" # have terraform or whatever you're using provide this
ENI_IP=$(/usr/bin/aws ec2 describe-network-interfaces --region=${REGION} | \
/usr/bin/jq -cr --arg i "${ENI_ID}" '.NetworkInterfaces[]|select(.NetworkInterfaceId==$i).PrivateIpAddress')
NETCIDR="${ENI_IP%.*}.0/24" # yeah, it's defaults for my network, put in whatever it is for you
GW="${ENI_IP%.*}.1"
############################################
# Attach ENI
############################################
/usr/bin/aws ec2 attach-network-interface --network-interface-id ${ENI_ID} \
--instance-id ${INSTANCE_ID} --device-index 1 --region ${REGION}
############################################
# Fix networking for CentOS 7
############################################
/bin/echo "GATEWAYDEV=eth0" >> /etc/sysconfig/network
/bin/cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE="eth1"
BOOTPROTO="dhcp"
ONBOOT="yes"
TYPE="Ethernet"
USERCTL="yes"
PEERDNS="yes"
IPV6INIT="no"
PERSISTENT_DHCLIENT="1"
EOF
/bin/cat <<EOF > /etc/sysconfig/network-scripts/route-eth1
default via ${GW} dev eth1 table 2
${NETCIDR} dev eth1 src ${ENI_IP} table 2
EOF
/bin/cat <<EOF > /etc/sysconfig/network-scripts/rule-eth1
from ${ENI_IP}/32 table 2
EOF
sleep 30 # yeah, it takes a little while for the interface to register
ifup eth1 # bring it online
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment