As an example of referencing security groups, imagine a 3-tier architecture:
- A Load Balancer receiving traffic from the Internet and sending requests to an Amazon EC2 instance
- An Amazon EC2 instance receiving requests from the Load Balancer and sending queries to an Amazon RDS database
- An Amazon RDS database receiving requests from the EC2 instance
This would involve 3 security groups:
- A security group on the Load Balancer (
LB-SG
) that allows incoming traffic on port 80/443.
- A security group on the EC2 instance (
App-SG
) that allows incoming traffic on port 80 only from the load balancer. It does this by setting the source to LB-SG
.
- A security group on the RDS database (
DB-SG
) that allows incoming traffic on the relevant port (eg 3306) only from the EC2 instance. It does this by setting the source to App-SG
.
- All security groups allow All Outbound Traffic.
By referencing other security groups, resources can be added/removed without having to change the security groups. For example, another EC2 instance could be launched and assigned the App-SG
security group. This new instance would then be able to communicate with the database since DB-SG
allows incoming traffic from App-SG
, without being tied to any specific IP addresses.
If a resource is associated with multiple security groups, then all rules apply to the resource. Security Groups only say what is 'Allowed'. They do not include 'Deny' rules.
In your SG-10/SG-20 example, you do not mention the source of the traffic, so it is not possible to answer your question. If you want EC2-20 to accept connections from EC2-10, then the SG-20 security group should allow connections with the Source set to SG-10.
As an aside, I should mention that Network ACLs should normally be left at their default "Allow All" settings unless there is a specific networking requirement (eg creating a DMZ).