VSCode SFTP AWS EC2
December 1, 2024 · AI Conversations
1. **Install the SFTP Extension**:
– Open VSCode.
– Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side or pressing `Ctrl + Shift + X`.
– Search for "SFTP" and install the **SFTP** extension by liximomo (or any preferred SFTP extension).
2. **Create an SFTP Configuration**:
– In your VSCode workspace, create a new folder (if you don't have one) to hold your SFTP configuration file.
– Inside this folder, create a file named `sftp.json`.
3. **Edit the `sftp.json` File**:
– Open `sftp.json` and add the following configuration, replacing the placeholders with your actual information:
{
"host": "ec2-16-170-254-93.eu-north-1.compute.amazonaws.com",
"username": "ubuntu",
"privateKeyPath": "/path/to/your/key.pem", // Replace with the path to your PEM file
"remotePath": "/var/www/share", // The path on your EC2 instance
"protocol": "sftp",
"port": 22,
"passphrase": "", // If your PEM file has a passphrase, add it here
"uploadOnSave": true, // Optional: Set to true to automatically upload files on save
"syncMode": "full" // Optional: Can be 'full' or 'update' based on your sync preferences
}
Make sure to replace `/path/to/your/key.pem` with the actual path to your PEM file on your local machine.
4. **Set Permissions for the PEM File**:
Ensure that your PEM file has the correct permissions. Run the following command in your terminal:
chmod 400 /path/to/your/key.pem
5. **Connect to the EC2 Instance**:
– After saving the `sftp.json` file, you should be able to use the SFTP extension to connect to your EC2 instance.
– Open the command palette (`Ctrl + Shift + P`), then type and select `SFTP: Connect`.
– Choose the appropriate configuration if prompted.
6. **Uploading and Downloading Files**:
– You can right-click files or folders in your workspace and use options like "Upload," "Download," or "Sync" to manage your files between your local environment and the EC2 instance.
### Troubleshooting
– If you encounter any connection issues, double-check your host, username, and PEM file path.
– Ensure that your EC2 instance's security group allows inbound connections on port 22.
By following these steps, you should be able to set up SFTP access to your AWS EC2 instance in VSCode using a PEM key. If you have any specific issues or errors, feel free to ask!
D:aws/k.pem
{
"host": "ec2-16-170-254-93.eu-north-1.compute.amazonaws.com",
"username": "ubuntu",
"privateKeyPath": "D:\aws\k.pem", // Note the double backslashes
"remotePath": "/var/www/share", // The path on your EC2 instance
"protocol": "sftp",
"port": 22,
"passphrase": "", // If your PEM file has a passphrase, add it here
"uploadOnSave": true, // Optional: Set to true to automatically upload files on save
"syncMode": "full" // Optional: Can be 'full' or 'update' based on your sync preferences
}
### Key Points:
– **Double Backslashes**: In JSON, backslashes need to be escaped, so use double backslashes (`\`) in the path.
– **Save the File**: Make sure to save the `sftp.json` file after making these changes.
### Additional Steps:
1. **Set Permissions (If Needed)**:
If you haven't set permissions for the PEM file in Windows, it’s generally not required, but make sure that the file is not accessible to unauthorized users.
2. **Connect to the EC2 Instance**:
– Use the command palette (`Ctrl + Shift + P`) and type `SFTP: Connect` to initiate the connection using the configuration.
You should now be able to connect to your EC2 instance via SFTP in VSCode. Let me know if you run into any issues!
### Configuration Breakdown
{
"remotePath": "/var/www/shareyourthoughts",
"uploadOnSave": true,
"useTempFile": false,
"openSsh": false,
"downloadOnOpen": false,
"ignore": [],
"concurrency": 4,
"protocol": "sftp",
"connectTimeout": 10000,
"interactiveAuth": false,
"secure": false,
"remoteTimeOffsetInHours": 0,
"remoteExplorer": {
"order": 0
},
"host": "ec2-51-20-60-238.eu-north-1.compute.amazonaws.com",
"username": "******", // Masked for security
"privateKeyPath": "D:\AWS-instance-projects\shareview.pem",
"port": 22,
"passphrase": "******", // Masked for security
"syncMode": "full"
}
### Tips for Troubleshooting
1. **Verify the PEM File Path**:
Ensure the path to your private key is correct. The path you've provided is `D:\AWS-instance-projects\shareview.pem`. Make sure that this file exists at this location.
2. **Check Permissions on the PEM File**:
On Windows, while permissions management is different from Unix/Linux, ensure that your PEM file is only accessible by you to avoid any potential issues.
3. **Host Information**:
Ensure that the `host` value matches your current EC2 instance. The host should be the public DNS or IP address of your EC2 instance.
4. **Security Group Settings**:
Verify that the EC2 instance's security group allows inbound traffic on port 22 (SSH/SFTP) from your IP address. You can check this in the AWS Management Console under the EC2 instance's security group settings.
5. **Passphrase**:
If your PEM file is not encrypted with a passphrase, you can set `"passphrase": ""` (an empty string). If it is encrypted, ensure you're providing the correct passphrase.
6. **Try Connecting from Command Line**:
As a test, you can try connecting to your EC2 instance using the command line (e.g., using PuTTY or OpenSSH) to see if the connection works outside of VSCode. For example:
ssh -i "D:AWS-instance-projectsshareview.pem" ubuntu@ec2-51-20-60-238.eu-north-1.compute.amazonaws.com
7. **Logs and Errors**:
If you encounter any connection errors, check the output logs in VSCode for more details on what might be going wrong.
### Final Configuration
Once you've verified all the above, your `sftp.json` file should look like this:
{
"host": "ec2-51-20-60-238.eu-north-1.compute.amazonaws.com",
"username": "your-username", // Ensure this is correct
"privateKeyPath": "D:\AWS-instance-projects\shareview.pem",
"remotePath": "/var/www/shareyourthoughts",
"protocol": "sftp",
"port": 22,
"passphrase": "", // or your actual passphrase
"uploadOnSave": true,
"syncMode": "full"
}
Let me know if you have any specific errors or issues, and I can help troubleshoot further!