I have internal ALB, with a TargetGroup, and ASG and a set of EC2 behind, basic setup (like internal web server).
On y ALB I have a SecurityGroup SGa which allow connection on port 80 from the vpc cidr range, and allow egress to port 8080 on SecurityGroup SGb which is on each EC2 instances behind the ALB.
The SecurityGroup SGb on each EC2 instances allow connection from the vpc cidr range on port 8080.
That setup work.
I want to limit access on the EC2 instances only to connection coming from the ALB.
My guess would be to configure SGb to only accept connection on port 8080 from SGa ?
but that would create some sort of loop dependency on both SG ?
I use terraform with aws_security_group egress/ingress, that would be a problem, I would have to switch to aws_security_group_rules to break the loop dependency ?
So SGa allow egress to port 8080 to SGb, and SGb allow ingress on port 8080 from SGa ?
Or should I need to add extra ‘place holder’ Security Group with no rules on the ALB and the EC2 instances so they can be referenced in SGa and SGb without creating loop ?
You can add source from the security group attached to the ALB and thats about it.
It should not create a loop in terraform, define both security groups first, and then you define the rules ‘’aws_security_group_rule, don’t put them inline. You allow ALB to access to EC2, so the rule is applied to the EC2 security group (source ALB).
Likely the ALB as an outbound of 0000/0 so it can access to EC2 if allowed no problem. If not, then you can add the outbound rule as well, but again, not inline… so you don’t get a circular reference.
If you have heal check, you need to add the healthcheck ips aswell