Skip to content

allow security group , which associated with other ec2, to allow it access

0

I have two ec2, let say ec2A and ec2B, and I have created 2 security group, let say sgA and sgB.

sgA is associated with ec2A sgB is associated with ec2B.

now, I edit sgB , added sgA in the sgB inbound rule list, to allow ec2A connect to ec2B. but not working (can't connect from ec2A to ec2B). until, I added ec2A I.P address in the sgB inbound. Why will that? (both ec2/sg is allowed to outgoing to any ip)

asked 2 years ago727 views

4 Answers
1
Accepted Answer

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.

EXPERT

answered 2 years ago

EXPERT

reviewed 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.

0

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:

  1. 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.

  2. Add a rule in sgB that allows inbound traffic from the entire VPC CIDR range or subnet that contains ec2A.

  3. 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

answered 2 years ago

EXPERT

reviewed 2 years ago

0

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

0

Please find attached screen for reference.

ec2A

Enter image description here

sgB

Enter image description here

If I remove the external I.P in the inbound rule, ec2A can not connect ec2B 80/443 port

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.