How to connect two EC2 instances to each other on AWS.

We will create EC2 instances like Linux instances on AWS. Sometimes we need to make two EC2 instances and connect together so that they can exchange files, etc. However, this link is not available by default.

We need to make some configuration changes to connect the two EC2 instances to each other. Here’s a quick way to do it.

Prerequisite:

We have two Linux instances in EC2 as shown below:

Make a note of the IP details and security group IDs of the two instances:

Instance ID: i-001bf8faf34df386f
Private IP 172.31.23.137
Sec grp ID sg-0ce7d465
------
Instance ID: i-0b6a20837dd74ba2c
Private IP 172.31.25.150
Sec grp ID sg-d0e7d4b9

Initially, when we try to ping these servers from each end of the test, it fails as shown below:

test1:

	
[[email protected] ~]# ping 172.31.25.150
PING 172.31.25.150 (172.31.25.150) 56(84) bytes of data.
^C
--- 172.31.25.150 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 999ms

Test 2:

	
[[email protected] ~]# ping 172.31.23.137
PING 172.31.23.137 (172.31.23.137) 56(84) bytes of data.
^C
--- 172.31.23.137 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 999ms

Now, to connect two EC2 instances to each other follow the steps below:

Step 1: Go to the security options group in the left pane under NETWORK & SECURITY options, as below:

make two instances of EC2 connection

Step 2: On the right side of the Security group, select “sg-0ce7d465”, which is the security group for the first instance. Click on the Edit option:

make two instances of EC2 connection

Step 3: In the next pop-up window, click on the “Add Rule” button:

  make two instances of EC2 connection

Step 4: Now select ICMP-ipv4 on the drop-down tab of the Source column select custom and in the next window enter the group SID of the other server, which is “sg-d0e7d4b9” and click on the Save button.

make two instances of EC2 connection

With the above configuration, we can ping server “172.31.23.137” from server 172.31.25.150 as shown below:

[[email protected] ~]# ping 172.31.23.137
PING 172.31.23.137 (172.31.23.137) 56(84) bytes of data.
64 bytes from 172.31.23.137: icmp_seq=1 ttl=64 time=0.632 ms
64 bytes from 172.31.23.137: icmp_seq=2 ttl=64 time=0.698 ms
64 bytes from 172.31.23.137: icmp_seq=3 ttl=64 time=0.646 ms
64 bytes from 172.31.23.137: icmp_seq=4 ttl=64 time=0.638 ms
^C
--- 172.31.23.137 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.632/0.653/0.698/0.036 ms
[[email protected] ~]#

For communication to work with a different server, follow steps 1 through 3 for the other security group “sg-d0e7d4b9” and in step 4 enter the details and click Save.

make two instances of EC2 connection

After the above configuration, we can ping the other way around as shown below:

[[email protected] ~]# ping 172.31.25.150
PING 172.31.25.150 (172.31.25.150) 56(84) bytes of data.
64 bytes from 172.31.25.150: icmp_seq=1 ttl=64 time=0.538 ms
64 bytes from 172.31.25.150: icmp_seq=2 ttl=64 time=0.666 ms
64 bytes from 172.31.25.150: icmp_seq=3 ttl=64 time=0.562 ms
64 bytes from 172.31.25.150: icmp_seq=4 ttl=64 time=0.618 ms
^C
--- 172.31.25.150 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 0.538/0.596/0.666/0.049 ms
[[email protected] ~]#

So this is how we can connect two EC2 instances to each other. For other ports or services, please select a specific service like SSH, HTTP or even NFS. Make sure you open source with another server’s custom SID group.

Sidebar