AWS EC2 instances are more like a server which provide the hosting platform
As your concern is how to connect two EC2 instances then try this way:
1) Go to EC2 console and note down IP and security ID of both the linux instances
2) Select any one instance and open network and security option
3) Select your instance's security group and click on edit
4) In the next popup window click on “Add Rule” button
5) Now Select All ICMP-ipv4 under Type drop-down, set source tab to custom and in the next box enter the security group id of the other instance (which is “sg-d0e7d4b9” )click on save button.
6) Now open CLI connect to instance and try to ping to other instance
- [root@ip-172-31-25-150 ~]# ping 172.31.23.137
7)To make the communication open from other instance as well, follow the steps 1 to 3 for other security group “sg-d0e7d4b9” and in the step 4 enter below details and click save button.
8) After above configuration we are now able to ping the first instance via second instance
- [root@ip-172-31-23-137 ~]# ping 172.31.25.150
- PING 172.31.25.150 (172.31.25.150) 56(84) bytes of data.
This way two Ec2 instances connect to each other. In case of other ports or service please select particular service such as ssh, http or even NFS make sure you open the source with custom mentioning the security group id of the other server.