- Newest
- Most votes
- Most comments
Hello.
Is communication from EC2A to EC2B using private IP addresses?
Referencing security groups is a setting that can only be used with private IP addresses (communication within a VPC), so it cannot be used with communication with public IP addresses.
By the way, you can also set security group references when communicating using private IP addresses between VPCs connected via VPC peering or Transit Gateway.
https://docs.aws.amazon.com/vpc/latest/userguide/security-group-rules.html#security-group-referencing
When you specify a security group as the source or destination for a rule, the rule affects all instances that are associated with the security groups. The instances can communicate in the specified direction, using the private IP addresses of the instances, over the specified protocol and port.
The behavior you're experiencing is due to how security groups work in Amazon EC2. Adding a security group (sgA) to another security group's (sgB) inbound rules doesn't automatically allow traffic from instances associated with sgA. Instead, it allows traffic from resources that are associated with sgA.
When you add sgA to sgB's inbound rules, you're essentially saying "allow traffic from any resource that has sgA attached to it." However, this doesn't mean EC2 instances automatically. The security group reference in the inbound rules is looking for the source of the traffic to have that security group attached.
In your case, ec2A isn't directly using sgA as the source of its traffic. It's using its own IP address. That's why adding ec2A's IP address to sgB's inbound rules worked - you're explicitly allowing traffic from that specific IP.
To make the security group reference work as you initially intended, you would need to do one of the following:
-
Use sgA as the source security group for ec2A's network interface. This way, when ec2A sends traffic, it would be recognized as coming from sgA.
-
Add a rule in sgB that allows inbound traffic from the entire VPC CIDR range or subnet that contains ec2A.
-
Keep the current setup with ec2A's IP address in sgB's inbound rules.
Remember, for complete communication, you also need to ensure that sgA allows outbound traffic to sgB (which is likely already the case if you have allowed all outbound traffic).
This approach of using security group references can be more flexible than IP-based rules, especially in dynamic environments where IP addresses might change. However, it requires careful configuration to ensure it works as intended.
Sources
Security group rules for different use cases - Amazon Elastic Compute Cloud
Use AuthorizeSecurityGroupIngress with an AWS SDK or CLI - Amazon Elastic Compute Cloud
I want method #1, you mentioned on above.
I have checked both ec2A/eniA, I can see 4 security groups and already included sgA, why still can't connect from ec2A to ec2B? what is different bewteen , ec2A and eniA use sgA as security group?, since eniA is the network interface on ec2A.
answered 2 years ago
Please find attached screen for reference.
ec2A
sgB
If I remove the external I.P in the inbound rule, ec2A can not connect ec2B 80/443 port
answered 2 years ago

Yes, both ec2 on same vpc, region/zone, and subnet.
Are EC2A and EC2B communicating via private IP addresses? Could you please share with us the specific protocol you are using and the steps you are taking to check it?
TCP/80/443. Please find attached screen capture show as below, thanks.
Oh, yes you are right, allow security rule on inbound only works for private I.P communication. And I use external I.P/domain, thank you.