Skip to content

Instantly share code, notes, and snippets.

@oh-sky
Created May 14, 2014 14:13
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 oh-sky/f115e0a640a7762564f9 to your computer and use it in GitHub Desktop.
Save oh-sky/f115e0a640a7762564f9 to your computer and use it in GitHub Desktop.
EC2のPublic IPをRDS Security GroupのCIDR/IPに、EC2のinstance-idをELBに自動登録
#!/bin/sh
do_start()
{
#このインスタンスのPublicIPアドレスをRDSのセキュリティグループに追加
PUBLICADDR=`curl http://169.254.169.254/latest/meta-data/public-ipv4 2> /dev/null`
aws rds authorize-db-security-group-ingress --db-security-group-name SECURITY-GROUP-NAME --cidrip "${PUBLICADDR}/32"
#このインスタンスをELB配下に置く
INSTANCEID=`curl http://169.254.169.254/latest/meta-data/instance-id 2> /dev/null`
aws elb register-instances-with-load-balancer --load-balancer-name LB-NAME --instances "${INSTANCEID}"
}
do_stop()
{
#このインスタンスのPublicIPアドレスをRDSのセキュリティグループから外す
PUBLICADDR=`curl http://169.254.169.254/latest/meta-data/public-ipv4 2> /dev/null`
aws rds revoke-db-security-group-ingress --db-security-group-name SECURITY-GROUP-NAME --cidrip "${PUBLICADDR}/32"
#このインスタンスをELBから外す
INSTANCEID=`curl http://169.254.169.254/latest/meta-data/instance-id 2> /dev/null`
aws elb deregister-instances-from-load-balancer --load-balancer-name LB-NAME --instances "${INSTANCEID}"
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_start
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
exit 3
;;
esac
:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment