DEV Community

Cover image for πŸ” How I Established SSH Connection Between Two AWS EC2 Instances
Abhishek Jha
Abhishek Jha

Posted on

πŸ” How I Established SSH Connection Between Two AWS EC2 Instances

Hey everyone! πŸ‘‹

Today, I want to share a new concept I learned while working with AWS β€” establishing an SSH connection between two EC2 instances. It might sound simple, but it’s a foundational skill if you're diving deeper into networking, automation, or setting up multi-node systems like clusters, load balancers, or databases.

Let me walk you through the process, the purpose, and the lessons I picked up along the way!

🧾 Why I Did It

In real-world cloud infrastructure setups, different EC2 instances often need to communicate with each other β€” for example:

  • One EC2 acts as a controller or bastion host.

  • You need to run scripts remotely on another EC2 instance.

  • You're building a distributed application and want to pass data internally between nodes.

To do this securely, you set up an SSH connection from one EC2 to another using a private key.

βš™οΈ What I Did

Here’s the exact process I followed to set up SSH between two Ubuntu-based EC2 instances:

βœ… Step 1: Create 2 EC2 Instances
I launched two EC2 instances in the same VPC and availability zone. Let's call them:

  • EC2-A (Source machine)

  • EC2-B (Target machine)

βœ… Step 2: Copy EC2-B's Private Key to EC2-A
I securely copied the private key (PEM file) used to launch EC2-B into EC2-A using scp.

βœ… Step 3: Adjust EC2-B's Security Group
In EC2-B's security group, I added an inbound rule to allow SSH (port 22) from EC2-A's private IP.

This ensures only EC2-A can initiate SSH connections to EC2-B β€” a best practice in cloud security.

βœ… Step 4: SSH from EC2-A to EC2-B
Once everything was set, I SSHed into EC2-A and ran:

ssh -i "ec2-b-key.pem" ubuntu@<private-ip-of-ec2-b>

And boom πŸ’₯ β€” I was inside EC2-B from EC2-A!

🧩 Something I Faced

One issue I initially ran into:
Even after copying the PEM file and setting the correct permissions (chmod 400), I was getting "Permission denied (publickey)".

Turns out I hadn’t allowed EC2-A’s IP in EC2-B’s security group. Once I fixed that β€” the SSH worked like a charm.

πŸ“š What I Learned

βœ… How EC2 instances communicate within a private VPC
βœ… Setting up secure SSH connections between two cloud servers
βœ… Importance of security group configurations and IP restrictions
βœ… Handling PEM keys and access control responsibly

This might seem like a small step, but it opens the door to bigger possibilities like:

  • Automating deployments from one EC2 to another

  • Setting up internal-only communication for microservices

  • Managing remote scripts or server orchestration

🧠 Final Thoughts

Cloud networking and EC2 configurations may look scary at first, but once you start doing it hands-on β€” it becomes clear, logical, and even fun.

If you're starting with AWS or EC2, I highly recommend practicing internal SSH setups β€” it’ll give you confidence in managing infrastructure securely and efficiently.

Thanks for reading! If this helped you or if you’ve done something similar, feel free to:

πŸ’¬ Drop a comment
🧑 React to this post
πŸ“Œ Save it for later

Let’s keep exploring, experimenting, and learning! πŸš€

Top comments (0)