I mentioned earlier that I recently moved my sites over to Amazon Web Services. It's incredibly easy to deploy applications with the EB CLI, using the command eb deploy
. And accessing my instance through SSH is as easy as eb ssh
.
Sometimes, though, I want a GUI to display my filesystem. I wanted a way to access my EC2 instance through Finder, and I wanted it to be as seamless as possible.
Prerequisites
First, I'm assuming you're using AWS Elastic Beanstalk. You need to have the Elastic Beanstalk Command Line Interface installed on your local machine.
Getting started: Installing sshfs
We're going to use the SSH protocol to access the filesystem.
First, you need to install FUSE, and then install SSHFS.
Next, you need to make a folder on your local machine that your computer will use as a mount point. I made a folder right next to my local copy of the filesystem. MAMP
is my local copy that I usually push to AWS, while AWS
is the folder that I just created.
Automating the process: Editing our .bashrc
In my .bash_profile
file, I added the line:
source $HOME/.bashrcThen, in
.bashrc
, I added the following:
ebmount() { sshfs [YOUR SSH USERNAME]@ec2-${1//./-}.compute-1.amazonaws.com:/var/app/current ~/Sites/AWS -ovolname=AWS; } alias eb-mount=ebmount alias ebunmount="umount -f ~/Sites/AWS;current=`pwd`;cd ~/Sites/MAMP;eb ssh;cd $current;unset current;" alias eb-unmount=ebunmount alias eb-ssh="current=`pwd`;cd ~/Sites/MAMP; eb ssh --force -o;cd $current;unset current;"
You're going to have to modify this a little bit so it fits your needs. First of all, [YOUR SSH USERNAME]
should be changed to your SSH username, usually ec2-user
or ubuntu
.
Second, ~/Sites/MAMP
is the directory where I initialized EB CLI. You should change this path to your directory's location. Similarly, ~/Sites/AWS
is the location of my AWS
mount folder. You need to change this as well.
/var/app/current
is the actual location that the alias /var/www/html
points to. You can change this to another directory if you'd like, or if your configuration is different from mine.
That's likely all that you need to change. ebmount
(which the alias eb-mount
points to) is a function that takes the current IP address of your EC2 instance and connects via sshfs. The alias eb-ssh
is eb ssh
with specific options -- --force
forces port 22 open and -o
keeps it open afterwards, so you can connect via sshfs.
The eb-unmount
function uses eb ssh, because when you exit
, port 22 will close again. I usually like to leave this port closed for security.
These aliases do pollute the global namespace. If you had a variable named $current
, it will no longer be set. If you're really concerned about this, you can make these aliases into functions instead, and use the local
keyword when initializing current
.
Now you just need to enter . ~/.bashrc
into the shell (and potentially . ~/.bash_profile
if you edited that too).
Using the commands
When you want to connect, you'll need to get the current IP address of your EC2 instance. This can change! That's how Elastic Beanstalk works. This is how you'll connect.
First, run eb-ssh
in your shell. This accomplishes two things: 1) port 22 is opened and 2) you get the current IP address of your instance. Go ahead and exit SSH immediately.
I've highlighted the relevant part -- the public IP address of your EC2 instance.
Next, run eb-mount [YOUR IP ADDRESS]
. For example, I would run eb-mount 52.90.130.111
.
Now, check the your mount folder. Your filesystem should be there.
If you're getting the error Socket is not connected
, it's because you're not successfully connecting to the host via SSH. Make sure that you're typing in the right address, and that your private key is correct. It's also possible that port 22 on your server is closed, but if you run our eb-ssh
command, this won't be a problem. If you need to specify a location for the private key, you can add the option -oIdentityFile=/path/to/key
to the sshfs command in your .bashrc
file.
Conclusion
Finder has some benefits over plain SSH. It's easy to transfer files. You get visual feedback. But a major disadvantage is the speed -- sshfs is considerably slower than ssh in my experience.
You might want to also consider closing port 22 after you're done using sshfs. You can do it by just running the eb ssh
command (notice, no hyphen) in the directory where you initialized EB CLI. Note that any changes you make via SSH could be replaced at any moment. That's the way that Elastic Beanstalk works. SSH should really be used for testing and debugging purposes.
Let me know if you have any issues with this configuration. I'd be happy to help.