You can use the SSH command to connect to another AWS EC2 instance from the command line. Here are the general steps:
Launch two EC2 instances in the same Virtual Private Cloud (VPC) and with the same key pair. Make sure that the instances are in running state.
Find the IP address of the first EC2 instance. You can find this information in the EC2 Management Console under the “Instances” section.
Connect to the first EC2 instance using SSH. For example, if the IP address of the first instance is 18.104.22.168 and the key pair is mykey.pem, you would use the following command:
ssh -i mykey.pem firstname.lastname@example.org
Once connected to the first instance, you can then use the ssh command to connect to the second instance using its private IP address:
Alternatively, if you want to directly connect to the second instance without connecting to first instance, you can use -A option for ssh.
ssh -i mykey.pem -A email@example.com ssh firstname.lastname@example.org
Note that the exact details of how to connect to your instances may differ depending on your specific setup, such as the user name and the location of your key pair file.
Also to avoid ssh connection issues, you can configure your security group for the instances to allow inbound SSH traffic and make sure that it is associated with your instances.