How to Transfer Files/Folders from AWS EC2 to Local Machine Using SCP

Introduction

Transferring files between an AWS EC2 instance and your local machine is a common task, particularly for developers and system administrators working on cloud-based applications. AWS provides several secure and efficient methods to accomplish this, leveraging tools like SSH, SCP, SFTP, or AWS CLI. This article walks you through the process step-by-step using SCP.

Prerequisites

  1. AWS EC2 Instance: Ensure your EC2 instance is up and running.
  2. Private Key File: You must have access to the private key (.pem file) used for SSH connections to your EC2 instance.
  3. Public IP/DNS: Obtain the public IP address or the public DNS name of the EC2 instance.

Transfering Files/Folders using SCP

Step 1. Transferring Files.

To transfer a file from your EC2 instance to your local machine, open your terminal (Linux/macOS) or Command Prompt (Windows) and use the SCP command as follows.

 scp -i “C:\Users\poonam\Downloads\myinstancekey.pem” [email protected]:/home/ubuntu/TestFile C:\Users\poonam\Downloads

File

Verify the file is transferred.

After running the command, check the folder at the path you provided in the command.

Local machine

Step 2. Transferring Directories.

To copy entire directories from the EC2 instance to your local machine, you’ll need to add the -r (recursive) flag to the SCP command.

scp -i “C:\Users\poonam\Downloads\myinstancekey.pem”  -r [email protected]:/home/ubuntu/TestDir C:\Users\poonam

Directory

Verify the folder is transferred.

After the transfer is complete, check the destination folder to ensure the directory has been successfully transferred.

Folder in Local

Conclusion

Using SCP to transfer files from an EC2 instance is quick and secure. This method ensures encrypted data transfer and works seamlessly for single files, multiple files, or entire directories. By following the steps, you can efficiently manage your file transfers.


Similar Articles