Transferring files between local machine and AWS instance

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:

  1. from local to remote machine
  2. from remote to local machine
  3. 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

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 

Let’s segment command for easier understanding:

  1. -i ~/ .shh/key specifying path to the identity_file, key which you also use for ssh connection with your AWS instance
  2. ~/Downloads/image.png specifying path to the file which we want to transfer from local machine
  3. ec2-user@192.168.1.1 specifying AWS user and IP address of the remote instance
  4. ~/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

Let’s segment command for easier understanding:

  1. -i ~/.ssh/key specifying path to the identity_file, key which you also use for ssh connection with your AWS instance
  2. -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
  3. ~/deployment/current/src specifying path to the directory which we want to transfer to from AWS server
  4. ~/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.