In the AWS VPC, I added a security group for the database access that allows any request from a specific CIDR IP on port 3306. This CIDR IP includes private subnets as well as public subnets. A public subnet is allowed so that database can explicitly be connected to developers machines using bastion host (EC2 instance configured on VPC's public subnet and assigned an IP from Amazon's pool of public IPs).
Ideally, services on a private subnet should able to connect to a database.
Tried something along this line:
DBConnectableSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: ...
GroupDescription: Allows for connection to the DB cluster.
ServerlessDBSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: ...
GroupDescription: Defines rules for connecting to the DB cluster.
OutboundRule:
Type: AWS::EC2::SecurityGroupEgress
Properties:
IpProtocol: tcp
FromPort: 0
ToPort: 65535
DestinationSecurityGroupId: !GetAtt ServerlessDBSecurityGroup.GroupId
GroupId: !GetAtt DBConnectableSecurityGroup.GroupId
InboundRule:
Type: AWS::EC2::SecurityGroupIngress
Properties:
IpProtocol: tcp
FromPort: 3306
ToPort: 3306
SourceSecurityGroupId: !GetAtt DBConnectableSecurityGroup.GroupId
GroupId: !GetAtt ServerlessDBSecurityGroup.GroupId
App-SG -- Inbound Rule
DB-SG -- Inbound Rule (source is pointing to App-SG)
App-SG and DB-SG -- Outbound Rule
Now I associate App-SG
with an application. This application can successfully connect to the database on port 3306
(same as configured in Inbound Rule of DB-SG
).
I associate App-SG
with another application. This application uses a different port to connect to the database, port 3310
. As App-SG
allows all ports, I expect this to connect to the database but this does not work and the connection is refused.
The preferred configuration is:
App-SG
) with appropriate inbound permissions to use the application, and the default All Outbound permissionsDB-SG
) that permits inbound connections on the database port from App-SG
and All OutboundThat is, DB-SG
specifically references App-SG
in its inbound rules. This way, any resource that is associated with App-SG
will be allowed to communicate with the database. This method avoids having to specify IP address and CIDR ranges and any new resources that use App-SG
will automatically gain access to the database.