Transferring files to EC2 Instance

Jun 2017

Introduction

There are a few ways to transfer files to and from an Amazon EC2 instance. If you are using a desktop environment, then you can use programs like Putty, WinSCP or MobaXterm. These graphically driven programs make transferring files very simple.
However, from the command line it can be a little trickier. In this example we will look at transferring files to and from an Amazon EC2 instance from the Linux command line using SCP.

Get your Key File

To be able to connect to your Amazon EC2 instance you will require your EC2 Instance key pair. Copy your key file to a directory on your source Linux server.
Before we can use the key we have to change its permissions, if you don't do this Amazon will reject the key on security grounds.
Use chmod to change your key.pem file permissions.

chmod 0400 keyfile.pem

The keyfile.pem should now have the following permissions: -r——–. 1 root root 1692 Jul 26 07:47 keyfile.pem

File Transfer

We are now ready to transfer files to and from our EC2 Instance. To connect to the EC2 instance you can use either the currently assigned IP Address, your Elastic IP Address if you have one, or the Public DNS (IPv4).
My test AWS EC2 Intance has the following details.
Elastic IP Address: 35.177.4.81
Public DNS (IPv4): ec2-35-177-4-81.eu-west-2.compute.amazonaws.com
The syntax of SCP is as follows:

scp -i <keypair> /source/myfile.txt login@address:/destination

I am going to transfer a file called TestFile.txt to the home directory on my EC2 Instance, for this I can use the following syntax:

scp -i keyfile.pem /home/root/TestFile.txt ec2-user@35.177.4.81:/home/ec2-user

If this is the first time connecting to an instance, you will see the following message:
The authenticity of host '35.177.4.81 (35.177.4.81)' can't be established. ECDSA key fingerprint is 02:8c:04:6c:e9:d5:aa:97:e3:40:35:a6:d9:f6:24:ed. Are you sure you want to continue connecting (yes/no)?
Type yes and press enter.
You should see a transfer confirmation similar to this:

TestFile.txt 100% 0 0.0KB/s 00:00