Skip to content

Instantly share code, notes, and snippets.

@newtonlabs
Created September 15, 2015 15:03
Show Gist options
  • Save newtonlabs/512030e920542f8eca5b to your computer and use it in GitHub Desktop.
Save newtonlabs/512030e920542f8eca5b to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
set -x
read -r -d '' attach_eth1_eni <<'EOF'
#!/bin/bash
function log () {
echo "$(date +"%b %e %T") $@"
logger -- $(basename $0) " - $@"
}
instance_id=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
eni_id=$(cat /var/tmp/eni-id.txt)
/usr/bin/aws ec2 attach-network-interface --network-interface-id $${eni_id} --instance-id $${instance_id} --device-index 1
retcode=$?
[ "$retcode" -eq 0 ] && { log "eni attachment successful" ; exit 0 ; } || { log "eni attachment failed" ; exit 1 ; }
EOF
echo "Attaching elastic interface"
echo "${attach_eth1_eni}" > /usr/local/bin/attach_eth1_eni.sh
chmod 755 /usr/local/bin/attach_eth1_eni.sh
echo ${eni} > /var/tmp/eni-id.txt
export AWS_DEFAULT_REGION="${aws_region}"
/usr/local/bin/attach_eth1_eni.sh
sleep 60
echo "Dropping eth0"
# This is necessary otherwise there are identical routes to the public subnet via two separate interfaces!
ifdown eth0
echo "Configuring NAT forwarding"
iptables -t nat -A POSTROUTING -j MASQUERADE
iptables -A FORWARD -j ACCEPT
echo 1 > /proc/sys/net/ipv4/ip_forward
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment