-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.ts
More file actions
176 lines (150 loc) · 5.14 KB
/
index.ts
File metadata and controls
176 lines (150 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import {
Stack,
aws_rds as rds
} from "aws-cdk-lib";
import { Construct } from "constructs";
import { CustomLambdaFunctionProps } from "../utils";
const instanceSizes: Record<string, number> = require("./instance-memory.json");
/**
* An RDS instance with pgSTAC installed. This is a wrapper around the
* `rds.DatabaseInstance` higher-level construct making use
* of the BootstrapPgStac construct.
*/
export class PgStacDatabase extends Construct {
db: rds.DatabaseInstance;
constructor(scope: Construct, id: string, props: PgStacDatabaseProps) {
super(scope, id);
const defaultParameters = this.getParameters(
props.instanceType?.toString() || "m5.large",
props.parameters
);
const parameterGroup = new rds.ParameterGroup(this, "parameterGroup", {
engine: props.engine,
parameters: {
shared_buffers: defaultParameters.sharedBuffers,
effective_cache_size: defaultParameters.effectiveCacheSize,
work_mem: defaultParameters.workMem,
maintenance_work_mem: defaultParameters.maintenanceWorkMem,
max_locks_per_transaction: defaultParameters.maxLocksPerTransaction,
temp_buffers: defaultParameters.tempBuffers,
seq_page_cost: defaultParameters.seqPageCost,
random_page_cost: defaultParameters.randomPageCost,
...props.parameters,
},
});
this.db = new rds.DatabaseInstance(this, "db", {
instanceIdentifier: Stack.of(this).stackName,
parameterGroup,
...props,
});
}
public getParameters(
instanceType: string,
parameters: PgStacDatabaseProps["parameters"]
): DatabaseParameters {
// https://github.com/aws/aws-cli/issues/1279#issuecomment-909318236
const memory_in_kb = instanceSizes[instanceType] * 1024;
// It's only necessary to consider passed in parameters for any value that used to
// derive subsequent values. Values that don't have dependencies will be overriden
// when we unpack the passed-in user parameters
const maxConnections = parameters?.maxConnections
? Number.parseInt(parameters.maxConnections)
: // https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html#RDS_Limits.MaxConnections
Math.min(Math.round((memory_in_kb * 1024) / 9531392), 5000);
const sharedBuffers = parameters?.sharedBufers
? Number.parseInt(parameters.sharedBufers)
: Math.round(0.25 * memory_in_kb);
const effectiveCacheSize = Math.round(0.75 * memory_in_kb);
const workMem = Math.floor(sharedBuffers / maxConnections);
const maintenanceWorkMem = Math.round(0.25 * sharedBuffers);
const tempBuffers = 128 * 1024;
const seqPageCost = 1;
const randomPageCost = 1.1;
return {
maxConnections: `${maxConnections}`,
sharedBuffers: `${sharedBuffers / 8}`, // Represented in 8kb blocks
effectiveCacheSize: `${effectiveCacheSize}`,
workMem: `${workMem}`,
maintenanceWorkMem: `${maintenanceWorkMem}`,
maxLocksPerTransaction: "1024",
tempBuffers: `${tempBuffers}`,
seqPageCost: `${seqPageCost}`,
randomPageCost: `${randomPageCost}`,
};
}
}
export interface PgStacDatabaseProps extends rds.DatabaseInstanceProps {
/**
* Name of database that is to be created and onto which pgSTAC will be installed.
*
* @default pgstac
*/
readonly pgstacDbName?: string;
/**
* Prefix to assign to the generated `secrets_manager.Secret`
*
* @default pgstac
*/
readonly secretsPrefix?: string;
/**
* Name of user that will be generated for connecting to the pgSTAC database.
*
* @default pgstac_user
*/
readonly pgstacUsername?: string;
/**
* Lambda function Custom Resource properties. A custom resource property is going to be created
* to trigger the boostrapping lambda function. This parameter allows the user to specify additional properties
* on top of the defaults ones.
*
*/
readonly customResourceProperties?: {
[key: string]: any;
}
/**
* Optional settings for the bootstrapper lambda function. Can be anything that can be configured on the lambda function, but some will be overwritten by values defined here.
*
* @default - defined in the construct.
*/
readonly bootstrapperLambdaFunctionOptions?: CustomLambdaFunctionProps;
}
export interface DatabaseParameters {
/**
* @default - LEAST({DBInstanceClassMemory/9531392}, 5000)
*/
readonly maxConnections: string;
/**
* Note: This value is measured in 8KB blocks.
*
* @default '{DBInstanceClassMemory/32768}' 25% of instance memory, ie `{(DBInstanceClassMemory/(1024*8)) * 0.25}`
*/
readonly sharedBuffers: string;
/**
* @default - 75% of instance memory
*/
readonly effectiveCacheSize: string;
/**
* @default - shared buffers divided by max connections
*/
readonly workMem: string;
/**
* @default - 25% of shared buffers
*/
readonly maintenanceWorkMem: string;
/**
* @default 1024
*/
readonly maxLocksPerTransaction: string;
/**
* @default 131172 (128 * 1024)
*/
readonly tempBuffers: string;
/**
* @default 1
*/
readonly seqPageCost: string;
/**
* @default 1.1
*/
readonly randomPageCost: string;
}