My isp recently began to change my ip a bit more frequently than before, and I’ve been spending more and more time logging in to the aws console on Amazon’s website, manually adding my external ip every for port 3306 second day, so that I could connect to the MySQL database from my own computer.
After a few times I felt the need to do this in a better way, and what is better than doing it with the EC2 CLI tools? This short tutorial is on how to do it on OSX (my version is 10.8.2).
If you follow my steps here, you will be able to open up port 3306 for your ip by simply writing
$ ./addip.sh 1.2.3.4
Neat ha?
This is how I did it:
Download and install the EC2 cli tools. They can be found here: http://aws.amazon.com/developertools/351. I put my stuff in ~/ec2
Next up is adding some environmental variables for the tools, so that they run. Open your terminal, write the following:
$ cd ~ $ pwd /Users/username
The “/Users/username” stuff that is outputted is your home path. Remember that. Now open up your .bash_profile file, it’s located in the home folder. Add the following lines:
export JAVA_HOME=`/System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java_home` export EC2_HOME=/Users/username/ec2 export PATH=$PATH:$EC2_HOME/bin export AWS_ACCESS_KEY=bbbbbbbbbbbbbbbbbbbbb export AWS_SECRET_KEY=aaaaaaaaaaaaaaaaaaaaaaaa
You find your AWS_ACCESS_KEY and AWS_SECRET_KEY when you’ve logged in under “credentials” in the menu, or by clicking here.
Now, in order to make it super simple, I assume that you just want to add your ip to one security group in one amazon region.
You need to know the url to the region that you need. Restart your terminal, since this exports all the variables we just added to the .bash_profile file. Now write:
$ ec2-describe-regions REGION eu-west-1 ec2.eu-west-1.amazonaws.com REGION sa-east-1 ec2.sa-east-1.amazonaws.com REGION us-east-1 ec2.us-east-1.amazonaws.com REGION ap-northeast-1 ec2.ap-northeast-1.amazonaws.com REGION us-west-2 ec2.us-west-2.amazonaws.com REGION us-west-1 ec2.us-west-1.amazonaws.com REGION ap-southeast-1 ec2.ap-southeast-1.amazonaws.com REGION ap-southeast-2 ec2.ap-southeast-2.amazonaws.com
Note the url of the region where your hosts are located. Open up the .bash_profile file again, and add the following line:
export EC2_URL=ec2.eu-west-1.amazonaws.com
Of course, use your url.
Now, lets create the shell command script for adding an ip quickly. Open up a file named addip.sh in your home folder. Copy the following lines:
#!/bin/bash function valid_ip() { local ip=$1 local stat=1 if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then OIFS=$IFS IFS='.' ip=($ip) IFS=$OIFS [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]] stat=$? fi return $stat } ARRAY=(${@}) if [ $# -eq 0 ] then echo "No arguments supplied" echo "Call: \$ ./addip.sh 10.20.30.40" exit fi if valid_ip ${ARRAY[0]}; then echo "Valid ip, adding to security group" ec2-authorize sg-dc072fa8 -p 3306 -s ${ARRAY[0]}/32 fi
This is a shell script that does some simple things. 1) Check so that you have a parameter and 2) Check so that the parameter is a valid ip address.
Saving this, and restarting the terminal (since you’ve added environmental variables), you can now use the script.
Go to http://www.whatsmyip.org/, copy your external ip, and from the terminal run:
$ ./addip.sh 213.22.33.44
And voila, the port 3306 will now be open for your ip.