Skip to content

Commit add27ec

Browse files
authored
feat: add pgbouncerInstanceProps parameter to PgstacDatabase (#139)
1 parent 35aa67e commit add27ec

File tree

3 files changed

+45
-30
lines changed

3 files changed

+45
-30
lines changed

integration_tests/cdk/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def __init__(
8383
allocated_storage=app_config.db_allocated_storage,
8484
instance_type=aws_ec2.InstanceType(app_config.db_instance_type),
8585
add_pgbouncer=True,
86+
pgbouncer_instance_props={
87+
"instanceName": "test-name",
88+
},
8689
removal_policy=RemovalPolicy.DESTROY,
8790
pgstac_version=PGSTAC_VERSION,
8891
)

lib/database/PgBouncer.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ export interface PgBouncerConfigProps {
2525
}
2626

2727
export 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

7064
export 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: "{}",

lib/database/index.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,20 @@ export class PgStacDatabase extends Construct {
144144
});
145145

146146
// PgBouncer: connection poolercustomresource trigger on redeploy
147+
const defaultPgbouncerInstanceProps: Partial<ec2.InstanceProps> = {
148+
instanceName: `${Stack.of(this).stackName}-pgbouncer`,
149+
instanceType: ec2.InstanceType.of(
150+
ec2.InstanceClass.T3,
151+
ec2.InstanceSize.MICRO
152+
),
153+
};
147154
const addPgbouncer = props.addPgbouncer ?? true;
148155
if (addPgbouncer) {
149156
this._pgBouncerServer = new PgBouncer(this, "pgbouncer", {
150-
instanceName: `${Stack.of(this).stackName}-pgbouncer`,
151-
instanceType: ec2.InstanceType.of(
152-
ec2.InstanceClass.T3,
153-
ec2.InstanceSize.MICRO
154-
),
157+
instanceProps: {
158+
...defaultPgbouncerInstanceProps,
159+
...props.pgbouncerInstanceProps,
160+
},
155161
vpc: props.vpc,
156162
database: {
157163
connections: this.db.connections,
@@ -258,6 +264,13 @@ export interface PgStacDatabaseProps extends rds.DatabaseInstanceProps {
258264
*/
259265
readonly addPgbouncer?: boolean;
260266

267+
/**
268+
* Properties for the pgbouncer ec2 instance
269+
*
270+
* @default - defined in the construct
271+
*/
272+
readonly pgbouncerInstanceProps?: ec2.InstanceProps | any;
273+
261274
/**
262275
* Lambda function Custom Resource properties. A custom resource property is going to be created
263276
* to trigger the boostrapping lambda function. This parameter allows the user to specify additional properties

0 commit comments

Comments
 (0)