Automate logging and tunneling to your EC2 instance
More often then not to work on my projects, I have to resort to AWS for my computational needs. Sometimes, it’s the lack of storage in my 128 GB local machine and sometimes it’s the need for GPUs to run deep learning models. AWS EC2 instances are not cheap especially if you are using a GPU enabled one. I use a p2xlarge which costs .9$ per hour and after I’m done with my work I need to stop (not terminate) it so that I don’t get charged unnecessarily. Unless you’re using a reserved instance, you’ll be assigned a new IP every time you restart your instance. Then you have to go through the boring process of copying the IP to the ssh command to log in.
ssh -o ServerAliveInterval=60 -i yourkey.pem email@example.com
Then if you want to tunnel into your instance you have to repeat this process one more time for your tunneling command.
ssh -o ServerAliveInterval=60 -i yourkey.pem firstname.lastname@example.org -L9000:127.0.0.1:8888
For someone like me who has to stop and restart my instance multiple times a day, this can get irritating very quickly. In this post I’ll talk about a neat trick to avoid this process to some extent.
The first piece of the puzzle is the elastic IP feature on AWS.
An Elastic IP address is a static IPv4 address which is associated with your AWS account
You can assign this IP to an instance and then you can use this IP to log in to your instance every time. This will fix the changing IP problem. And the cost for this is negligible. If the IP is attached to a running instance then there will be no cost. But if the IP is attached to a stopped instance which is what you’ll require, it’ll cost you .005$ per hour. If we do a rough calculation, 20 hours of stopped instance per day for 50 days will cost you 5$. Not bad at all!!
Here is how you reserve an elastic IP:
Step1: EC2 dashboard → Network & Security → Elastic IP
Step2: Click on Allocate new address and follow the steps
Step3: Select the new IP → Actions →Associate address
Step4: Select the instance that you want to attach this IP to
For the selected instance, public IP is permanently changed to the elastic IP, unless you release it. Now you don’t need to change the IP portion of logging and tunneling command again and again.
Just using elastic IP will save you time. But there is still scope for making it more efficient so follow along.
SSH script for logging and tunneling
Here I’ll talk about a series of steps to create a ssh script which you can run from your terminal and it’ll not only log in to your instance but also create a tunnel from your localhost to your instance.
Step1: Enter your .ssh directory.
Step2: Remember that you need pem file for logging to your instance. Copy this pem file to the ssh directory.
Step3: Now create a ssh config file in the same directory using any text editor. I’m using nano for this. And in your config file, define your instance details.
LocalForward 9000 localhost:8888
- In the above config file, user will be ubuntu if you’re using an ubuntu instance
- LocalForward 9000 localhost:8888 will forward port 9000 on your localhost to the localhost of your EC2 instance. You can use any other port instead of 9000 also
Step4: Once you’re done it’s as easy as calling this command every time you want to restart your instance
Gurupriyan is a Software Engineer and a technology enthusiast, he’s been working on the field for the last 6 years. Currently focusing on mobile app development and IoT.