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)