@@ -70,6 +70,7 @@ export interface PgBouncerProps {
70
70
export class PgBouncer extends Construct {
71
71
public readonly instance : ec2 . Instance ;
72
72
public readonly pgbouncerSecret : secretsmanager . Secret ;
73
+ public readonly securityGroup : ec2 . SecurityGroup ;
73
74
74
75
// The max_connections parameter in PgBouncer determines the maximum number of
75
76
// connections to open on the actual database instance. We want that number to
@@ -134,6 +135,13 @@ export class PgBouncer extends Construct {
134
135
} )
135
136
) ;
136
137
138
+ // Create a security group and allow connections from the Lambda IP ranges for this region
139
+ this . securityGroup = new ec2 . SecurityGroup ( this , "PgBouncerSecurityGroup" , {
140
+ vpc : props . vpc ,
141
+ description : "Security group for PgBouncer instance" ,
142
+ allowAllOutbound : true ,
143
+ } ) ;
144
+
137
145
// Create PgBouncer instance
138
146
this . instance = new ec2 . Instance ( this , "Instance" , {
139
147
vpc : props . vpc ,
@@ -142,6 +150,7 @@ export class PgBouncer extends Construct {
142
150
? ec2 . SubnetType . PUBLIC
143
151
: ec2 . SubnetType . PRIVATE_WITH_EGRESS ,
144
152
} ,
153
+ securityGroup : this . securityGroup ,
145
154
instanceType,
146
155
instanceName : props . instanceName ,
147
156
machineImage : ec2 . MachineImage . fromSsmParameter (
@@ -161,6 +170,7 @@ export class PgBouncer extends Construct {
161
170
] ,
162
171
userData : this . loadUserDataScript ( pgBouncerConfig , props . database ) ,
163
172
userDataCausesReplacement : true ,
173
+ associatePublicIpAddress : props . usePublicSubnet ,
164
174
} ) ;
165
175
166
176
// Allow PgBouncer to connect to RDS
@@ -201,7 +211,9 @@ export class PgBouncer extends Construct {
201
211
new CustomResource ( this , "pgbouncerSecretBootstrapper" , {
202
212
serviceToken : secretUpdaterFn . functionArn ,
203
213
properties : {
204
- instanceIp : this . instance . instancePrivateIp ,
214
+ instanceIp : props . usePublicSubnet
215
+ ? this . instance . instancePublicIp
216
+ : this . instance . instancePrivateIp ,
205
217
} ,
206
218
} ) ;
207
219
}
0 commit comments