Transferring files between local machine and AWS instance

wnbsmart

Maroje Macola

Posted on March 22, 2021

Transferring files between local machine and AWS instance

Hello fellow dev.to readers!

Intro

Recently, I needed to transfer files from local machine to the remote AWS instance. Since I haven't done this before, after a quick googling, I stumbled upon a cool bash command scp (secure copy). After seeing secure keyword in its name, I didn't waste time on looking for other solutions, although I believe there are many. Therefore, let's take a look at how can we use it.

scp command

Generally, with this command, we are able to do 3 types of file transfers:

  • from local to remote machine
  • from remote to local machine
  • between two remote machines, by using local machine

In this post, we will cover first 2 cases.

To get a list of available options for scp command, we can run scp help:

$ scp help
usage: scp [-346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P port] [-S program] source ... target
Enter fullscreen mode Exit fullscreen mode

A comprehensive list with detailed explanations can be found here.

[-i identity_file] parameter will be of use for our case of communicating with AWS instances, since we need to use private key which is also used for ssh connection with the server.

[-r Recursively copy entire directories] parameter will be of help to transfer whole directory.

Local -> remote file transfer

Scenario - transferring an image from the local machine's Downloads folder to AWS server, which has ec2-user and IP address 192.168.1.1:

scp -i ~/.ssh/key ~/Downloads/image.png ec2-user@192.168.1.1:~/deployment/current/public 
Enter fullscreen mode Exit fullscreen mode

Let's segment command for easier understanding:

  • -i ~/.ssh/key specifying path to the identity_file, key which you also use for ssh connection with your AWS instance
  • ~/Downloads/image.png specifying path to the file which we want to transfer from local machine
  • ec2-user@192.168.1.1 specifying AWS user and IP address of the remote instance
  • ~/deployment/current/public specifying path to the destination directory on the remote instance (where transferred file will exist after execution)

Remote -> local file transfer

Scenario - transferring src directory from AWS server, to the local Downloads folder:

scp -i ~/.ssh/key -r ec2-user@192.168.1.1:~/deployment/current/src ~/Downloads
Enter fullscreen mode Exit fullscreen mode

Let's segment command for easier understanding:

  • -i ~/.ssh/key specifying path to the identity_file, key which you also use for ssh connection with your AWS instance
  • -r ec2-user@192.168.1.1 specifying AWS user and IP address of the remote instance, this time with -r option we say that we want to transfer directory recursively
  • ~/deployment/current/src specifying path to the directory which we want to transfer to from AWS server
  • ~/Downloads specifying path to the destination directory on the local machine (where transferred file will exist after execution)

Conclusion

scp is a very powerful command which allows us to easily transfer files between local and remote AWS server. As mentioned in one of the paragraphs, it has many additional options to be used, but we only need a few of them to be able to do a basic data transfer.

How do you transfer files between your servers? 🙃

Cover image source

💖 💪 🙅 🚩
wnbsmart
Maroje Macola

Posted on March 22, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related