@@ -25,11 +25,6 @@ export interface PgBouncerConfigProps {
2525}
2626
2727export interface PgBouncerProps {
28- /**
29- * Name for the pgbouncer instance
30- */
31- instanceName : string ;
32-
3328 /**
3429 * VPC to deploy PgBouncer into
3530 */
@@ -56,15 +51,14 @@ export interface PgBouncerProps {
5651 usePublicSubnet ?: boolean ;
5752
5853 /**
59- * Instance type for PgBouncer
60- * @default t3.micro
54+ * PgBouncer configuration options
6155 */
62- instanceType ?: ec2 . InstanceType ;
56+ pgBouncerConfig ?: PgBouncerConfigProps ;
6357
6458 /**
65- * PgBouncer configuration options
59+ * EC2 instance options
6660 */
67- pgBouncerConfig ?: PgBouncerConfigProps ;
61+ instanceProps ?: Partial < ec2 . InstanceProps > ;
6862}
6963
7064export class PgBouncer extends Construct {
@@ -78,7 +72,7 @@ export class PgBouncer extends Construct {
7872 // be slightly smaller than the actual max_connections value on the RDS instance
7973 // so we perform this calculation.
8074
81- private getDefaultConfig (
75+ private getDefaultPgbouncerConfig (
8276 dbMaxConnections : number
8377 ) : Required < PgBouncerConfigProps > {
8478 // maxDbConnections (and maxUserConnections) are the only settings that need
@@ -99,17 +93,14 @@ export class PgBouncer extends Construct {
9993 super ( scope , id ) ;
10094
10195 // Set defaults for optional props
102- const defaultInstanceType = ec2 . InstanceType . of (
103- ec2 . InstanceClass . T3 ,
104- ec2 . InstanceSize . MICRO
105- ) ;
10696
107- const instanceType = props . instanceType ?? defaultInstanceType ;
108- const defaultConfig = this . getDefaultConfig ( props . dbMaxConnections ) ;
97+ const defaultPgbouncerConfig = this . getDefaultPgbouncerConfig (
98+ props . dbMaxConnections
99+ ) ;
109100
110101 // Merge provided config with defaults
111102 const pgBouncerConfig : Required < PgBouncerConfigProps > = {
112- ...defaultConfig ,
103+ ...defaultPgbouncerConfig ,
113104 ...props . pgBouncerConfig ,
114105 } ;
115106
@@ -144,21 +135,21 @@ export class PgBouncer extends Construct {
144135 } ) ;
145136
146137 // Create PgBouncer instance
147- this . instance = new ec2 . Instance ( this , "Instance" , {
148- vpc : props . vpc ,
138+ const defaultInstanceConfig : Omit < ec2 . InstanceProps , "vpc" > = {
139+ instanceName : "pgbouncer" ,
140+ instanceType : ec2 . InstanceType . of (
141+ ec2 . InstanceClass . T3 ,
142+ ec2 . InstanceSize . MICRO
143+ ) ,
149144 vpcSubnets : {
150145 subnetType : props . usePublicSubnet
151146 ? ec2 . SubnetType . PUBLIC
152147 : ec2 . SubnetType . PRIVATE_WITH_EGRESS ,
153148 } ,
154- securityGroup : this . securityGroup ,
155- instanceType,
156- instanceName : props . instanceName ,
157149 machineImage : ec2 . MachineImage . fromSsmParameter (
158150 "/aws/service/canonical/ubuntu/server/jammy/stable/current/amd64/hvm/ebs-gp2/ami-id" ,
159151 { os : ec2 . OperatingSystemType . LINUX }
160152 ) ,
161- role,
162153 blockDevices : [
163154 {
164155 deviceName : "/dev/xvda" ,
@@ -169,9 +160,17 @@ export class PgBouncer extends Construct {
169160 } ) ,
170161 } ,
171162 ] ,
163+ role,
164+ securityGroup : this . securityGroup ,
172165 userData : this . loadUserDataScript ( pgBouncerConfig , props . database ) ,
173166 userDataCausesReplacement : true ,
174167 associatePublicIpAddress : props . usePublicSubnet ,
168+ } ;
169+
170+ this . instance = new ec2 . Instance ( this , "Instance" , {
171+ ...defaultInstanceConfig ,
172+ ...props . instanceProps ,
173+ vpc : props . vpc ,
175174 } ) ;
176175
177176 // Allow PgBouncer to connect to RDS
@@ -183,7 +182,7 @@ export class PgBouncer extends Construct {
183182
184183 // Create a new secret for pgbouncer connection credentials
185184 this . pgbouncerSecret = new secretsmanager . Secret ( this , "PgBouncerSecret" , {
186- description : ` Connection information for PgBouncer instance ${ props . instanceName } ` ,
185+ description : " Connection information for PgBouncer instance" ,
187186 generateSecretString : {
188187 generateStringKey : "dummy" ,
189188 secretStringTemplate : "{}" ,
0 commit comments