By default, resources including Amazon EC2 instances cannot communicate with each other. (Although, the management console will offer some standard security groups that do permit this communication, but it is your choice to accept it.)
If you want to permit inbound communication to/from resources, you must configure Security Groups.
Security Groups have two sets of rules:
- Inbound rules, where communications are initiated from outside of the resource, and
- Outbound rules, where communication is initiated from the resource to something outside of the resource
Note that these rules apply to any traffic in/out of the resource, not just traffic to/from the Internet.
If two Amazon EC2 instances are in the same VPC, they can only communicate with each other if there are:
- Outbound rules that permit the traffic, and
- Inbound rules that permit the traffic
For example, if Instance-A wants to communicate with Instance-B, then:
- Instance-A requires an Outbound rule that permits traffic to go to Instance-A, and
- Instance-B requires an Inbound rule that permits traffic from Instance-A to be received by Instance-B
Some people think that if multiple instances are "in the same Security Group" then they can communicate with each other. However, this is not true. Resources are not "in" security groups. Rather, each Security Group applies to each instance individually. If two instances have been assigned the same security group, then they can only communicate if the security group permits outbound connections to itself and also permits inbound connections to itself.
To answer your question "Can they protect EC2 from being attacked by other instances inside AWS?", the answer is YES. That is exactly what they do. They go further than preventing "being attacked". They actually block all traffic unless it is specifically allowed, whether or not that traffic is an 'attack'.