I have a circular dependency problem.
I’m trying to create security group for an autoscaling group that allows traffic to an RDS MySQL DB instance. Similarly I want to create a security group for the RDS instance that allows traffic from the autoscaling group but they both depend on each other. What might be the best way to solve it?
AutoscalingSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: Security group for autoscaling
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 3306
ToPort: 3306
SourceSecurityGroupId: !Ref RDSSecurityGroup
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 3306
ToPort: 3306
SourceSecurityGroupId: !Ref RDSSecurityGroup
RDSSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: Security group for RDS instance
VpcId: !Ref VPC
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 3306
ToPort: 3306
SourceSecurityGroupId: !Ref AutoscalingSecurityGroup
SecurityGroupEgress:
- IpProtocol: tcp
FromPort: 3306
ToPort: 3306
SourceSecurityGroupId: !Ref AutoscalingSecurityGroup
Any ideas of how I’d solve it?
TIA
2
Answers
One option is to create two security groups without configuring SecurityGroupIngress and SecurityGroupEgress properties.
Then add
AWS::EC2::SecurityGroupIngress
andAWS::EC2::SecurityGroupEgress
resources to configure Ingress and Egress rules in the same template/stack.For example:
The previous answer has GroupId & Source/DestinationSecurityGroupId mixed up. I also think the security group rules can be simplified. Security groups are stateful and thus the return traffic is allowed by default. So, the above rules can be simplified as per below.
Alternatively, it can be further simplified as following.