Connect to EC2 from Visual Studio Code

VerticalServe Blogs
3 min readAug 12, 2024

--

Connecting to an Amazon EC2 instance using Visual Studio Code (VS Code) can significantly streamline your development workflow by allowing you to edit files directly on the server using a familiar interface. This blog post will guide you through the process of setting up an SSH connection to your EC2 instance using VS Code’s Remote — SSH extension. We’ll cover setting up the SSH configuration file with your PEM key, hostname, and user details.

Prerequisites

  1. Amazon EC2 Instance: Ensure you have an EC2 instance running. You should have the PEM file downloaded when you created the instance.
  2. Visual Studio Code: Install VS Code on your local machine.
  3. Remote — SSH Extension: Install the Remote — SSH extension in VS Code.

Step-by-Step Guide

1. Install the Remote — SSH Extension

  • Open VS Code.
  • Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or by pressing Ctrl+Shift+X.
  • Search for “Remote — SSH” and install the extension provided by Microsoft.

2. Configure SSH on Your Local Machine

  • Locate the PEM file: Ensure your PEM file is accessible on your local machine. This file is used to authenticate your SSH connection.
  • Set Permissions: The PEM file should have the correct permissions. Run the following command in your terminal to set the permissions:
chmod 400 path/to/your-key.pem

3. Set Up the SSH Configuration File

  • Open or create the SSH configuration file located at ~/.ssh/config on your local machine.
  • Add the following configuration to the file, replacing placeholders with your actual details:
Host my-ec2-instance
HostName your-ec2-public-dns
User ec2-user # or ubuntu, depending on your instance's AMI
IdentityFile path/to/your-key.pem
  • Host: A nickname for your connection.
  • HostName: The public DNS or IP address of your EC2 instance.
  • User: Typically ec2-user for Amazon Linux or ubuntu for Ubuntu instances.
  • IdentityFile: The path to your PEM file.

4. Connect to the EC2 Instance Using VS Code

  • Open VS Code.
  • Press Ctrl+Shift+P to open the Command Palette.
  • Type and select “Remote-SSH: Connect to Host…”.
  • Choose the host you configured (my-ec2-instance in this example) from the list.
  • VS Code will open a new window connected to your EC2 instance. You can now open folders and edit files directly on the server.

5. Troubleshooting

  • Permission Denied: Ensure your PEM file has the correct permissions and that the IdentityFile path is correct.
  • Network Issues: Make sure your EC2 instance’s security group allows SSH access on port 22 from your IP address.

Conclusion

By following these steps, you can easily connect to your Amazon EC2 instance using Visual Studio Code. This setup allows you to leverage the power of cloud computing while maintaining a seamless development environment. Whether you’re working on web applications, data processing scripts, or other projects, this method enhances productivity by integrating your local development tools with remote server capabilities.

About:

VerticalServe Inc — Niche Cloud, Data & AI/ML Premier Consulting Company, Partnered with Google Cloud, Confluent, AWS, Azure…60+ Customers and many success stories..

Website: http://www.VerticalServe.com

Contact: contact@verticalserve.com

Successful Case Studies: http://verticalserve.com/success-stories.html

InsightLake Solutions: Our pre built solutions — http://www.InsightLake.com

--

--