@stacksjs/ts-cloud-core 0.1.7 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (225) hide show
  1. package/package.json +7 -6
  2. package/src/advanced-features.test.ts +465 -0
  3. package/src/aws/cloudformation.ts +421 -0
  4. package/src/aws/cloudfront.ts +158 -0
  5. package/src/aws/credentials.test.ts +132 -0
  6. package/src/aws/credentials.ts +545 -0
  7. package/src/aws/index.ts +87 -0
  8. package/src/aws/s3.test.ts +188 -0
  9. package/src/aws/s3.ts +1088 -0
  10. package/src/aws/signature.test.ts +670 -0
  11. package/src/aws/signature.ts +1155 -0
  12. package/src/backup/disaster-recovery.test.ts +726 -0
  13. package/src/backup/disaster-recovery.ts +500 -0
  14. package/src/backup/index.ts +34 -0
  15. package/src/backup/manager.test.ts +498 -0
  16. package/src/backup/manager.ts +432 -0
  17. package/src/cicd/circleci.ts +430 -0
  18. package/src/cicd/github-actions.ts +424 -0
  19. package/src/cicd/gitlab-ci.ts +255 -0
  20. package/src/cicd/index.ts +8 -0
  21. package/src/cli/history.ts +396 -0
  22. package/src/cli/index.ts +10 -0
  23. package/src/cli/progress.ts +458 -0
  24. package/src/cli/repl.ts +454 -0
  25. package/src/cli/suggestions.ts +327 -0
  26. package/src/cli/table.test.ts +319 -0
  27. package/src/cli/table.ts +332 -0
  28. package/src/cloudformation/builder.test.ts +327 -0
  29. package/src/cloudformation/builder.ts +378 -0
  30. package/src/cloudformation/builders/api-gateway.ts +449 -0
  31. package/src/cloudformation/builders/cache.ts +334 -0
  32. package/src/cloudformation/builders/cdn.ts +278 -0
  33. package/src/cloudformation/builders/compute.ts +485 -0
  34. package/src/cloudformation/builders/database.ts +392 -0
  35. package/src/cloudformation/builders/functions.ts +343 -0
  36. package/src/cloudformation/builders/messaging.ts +140 -0
  37. package/src/cloudformation/builders/monitoring.ts +300 -0
  38. package/src/cloudformation/builders/network.ts +264 -0
  39. package/src/cloudformation/builders/queue.ts +147 -0
  40. package/src/cloudformation/builders/security.ts +399 -0
  41. package/src/cloudformation/builders/storage.ts +285 -0
  42. package/src/cloudformation/index.ts +30 -0
  43. package/src/cloudformation/types.ts +173 -0
  44. package/src/compliance/aws-config.ts +543 -0
  45. package/src/compliance/cloudtrail.ts +376 -0
  46. package/src/compliance/compliance.test.ts +423 -0
  47. package/src/compliance/guardduty.ts +446 -0
  48. package/src/compliance/index.ts +66 -0
  49. package/src/compliance/security-hub.ts +456 -0
  50. package/src/containers/build-optimization.ts +416 -0
  51. package/src/containers/containers.test.ts +508 -0
  52. package/src/containers/image-scanning.ts +360 -0
  53. package/src/containers/index.ts +9 -0
  54. package/src/containers/registry.ts +293 -0
  55. package/src/containers/service-mesh.ts +520 -0
  56. package/src/database/database.test.ts +762 -0
  57. package/src/database/index.ts +9 -0
  58. package/src/database/migrations.ts +444 -0
  59. package/src/database/performance.ts +528 -0
  60. package/src/database/replicas.ts +534 -0
  61. package/src/database/users.ts +494 -0
  62. package/src/dependency-graph.ts +143 -0
  63. package/src/deployment/ab-testing.ts +582 -0
  64. package/src/deployment/blue-green.ts +452 -0
  65. package/src/deployment/canary.ts +500 -0
  66. package/src/deployment/deployment.test.ts +526 -0
  67. package/src/deployment/index.ts +61 -0
  68. package/src/deployment/progressive.ts +62 -0
  69. package/src/dns/dns.test.ts +641 -0
  70. package/src/dns/dnssec.ts +315 -0
  71. package/src/dns/index.ts +8 -0
  72. package/src/dns/resolver.ts +496 -0
  73. package/src/dns/routing.ts +593 -0
  74. package/src/email/advanced/analytics.ts +445 -0
  75. package/src/email/advanced/index.ts +11 -0
  76. package/src/email/advanced/rules.ts +465 -0
  77. package/src/email/advanced/scheduling.ts +352 -0
  78. package/src/email/advanced/search.ts +412 -0
  79. package/src/email/advanced/shared-mailboxes.ts +404 -0
  80. package/src/email/advanced/templates.ts +455 -0
  81. package/src/email/advanced/threading.ts +281 -0
  82. package/src/email/analytics.ts +467 -0
  83. package/src/email/bounce-handling.ts +425 -0
  84. package/src/email/email.test.ts +431 -0
  85. package/src/email/handlers/__tests__/inbound.test.ts +38 -0
  86. package/src/email/handlers/__tests__/outbound.test.ts +37 -0
  87. package/src/email/handlers/converter.ts +227 -0
  88. package/src/email/handlers/feedback.ts +228 -0
  89. package/src/email/handlers/inbound.ts +169 -0
  90. package/src/email/handlers/outbound.ts +178 -0
  91. package/src/email/index.ts +15 -0
  92. package/src/email/reputation.ts +303 -0
  93. package/src/email/templates.ts +352 -0
  94. package/src/errors/index.test.ts +434 -0
  95. package/src/errors/index.ts +416 -0
  96. package/src/health-checks/index.ts +40 -0
  97. package/src/index.ts +360 -0
  98. package/src/intrinsic-functions.ts +118 -0
  99. package/src/lambda/concurrency.ts +330 -0
  100. package/src/lambda/destinations.ts +345 -0
  101. package/src/lambda/dlq.ts +425 -0
  102. package/src/lambda/index.ts +11 -0
  103. package/src/lambda/lambda.test.ts +840 -0
  104. package/src/lambda/layers.ts +263 -0
  105. package/src/lambda/versions.ts +376 -0
  106. package/src/lambda/vpc.ts +399 -0
  107. package/src/local/config.ts +114 -0
  108. package/src/local/index.ts +6 -0
  109. package/src/local/mock-aws.ts +351 -0
  110. package/src/modules/ai.ts +340 -0
  111. package/src/modules/api.ts +478 -0
  112. package/src/modules/auth.ts +805 -0
  113. package/src/modules/cache.ts +417 -0
  114. package/src/modules/cdn.ts +1062 -0
  115. package/src/modules/communication.ts +1094 -0
  116. package/src/modules/compute.ts +3348 -0
  117. package/src/modules/database.ts +554 -0
  118. package/src/modules/deployment.ts +1079 -0
  119. package/src/modules/dns.ts +337 -0
  120. package/src/modules/email.ts +1538 -0
  121. package/src/modules/filesystem.ts +515 -0
  122. package/src/modules/index.ts +32 -0
  123. package/src/modules/messaging.ts +486 -0
  124. package/src/modules/monitoring.ts +2086 -0
  125. package/src/modules/network.ts +664 -0
  126. package/src/modules/parameter-store.ts +325 -0
  127. package/src/modules/permissions.ts +1081 -0
  128. package/src/modules/phone.ts +494 -0
  129. package/src/modules/queue.ts +1260 -0
  130. package/src/modules/redirects.ts +464 -0
  131. package/src/modules/registry.ts +699 -0
  132. package/src/modules/search.ts +401 -0
  133. package/src/modules/secrets.ts +416 -0
  134. package/src/modules/security.ts +731 -0
  135. package/src/modules/sms.ts +389 -0
  136. package/src/modules/storage.ts +1120 -0
  137. package/src/modules/workflow.ts +680 -0
  138. package/src/multi-account/config.ts +521 -0
  139. package/src/multi-account/index.ts +7 -0
  140. package/src/multi-account/manager.ts +427 -0
  141. package/src/multi-region/cross-region.ts +410 -0
  142. package/src/multi-region/index.ts +8 -0
  143. package/src/multi-region/manager.ts +483 -0
  144. package/src/multi-region/regions.ts +435 -0
  145. package/src/network-security/index.ts +48 -0
  146. package/src/observability/index.ts +9 -0
  147. package/src/observability/logs.ts +522 -0
  148. package/src/observability/metrics.ts +460 -0
  149. package/src/observability/observability.test.ts +782 -0
  150. package/src/observability/synthetics.ts +568 -0
  151. package/src/observability/xray.ts +358 -0
  152. package/src/phone/advanced/analytics.ts +349 -0
  153. package/src/phone/advanced/callbacks.ts +428 -0
  154. package/src/phone/advanced/index.ts +8 -0
  155. package/src/phone/advanced/ivr-builder.ts +504 -0
  156. package/src/phone/advanced/recording.ts +310 -0
  157. package/src/phone/handlers/__tests__/incoming-call.test.ts +40 -0
  158. package/src/phone/handlers/incoming-call.ts +117 -0
  159. package/src/phone/handlers/missed-call.ts +116 -0
  160. package/src/phone/handlers/voicemail.ts +179 -0
  161. package/src/phone/index.ts +9 -0
  162. package/src/presets/api-backend.ts +134 -0
  163. package/src/presets/data-pipeline.ts +204 -0
  164. package/src/presets/extend.test.ts +295 -0
  165. package/src/presets/extend.ts +297 -0
  166. package/src/presets/fullstack-app.ts +144 -0
  167. package/src/presets/index.ts +27 -0
  168. package/src/presets/jamstack.ts +135 -0
  169. package/src/presets/microservices.ts +167 -0
  170. package/src/presets/ml-api.ts +208 -0
  171. package/src/presets/nodejs-server.ts +104 -0
  172. package/src/presets/nodejs-serverless.ts +114 -0
  173. package/src/presets/realtime-app.ts +184 -0
  174. package/src/presets/static-site.ts +64 -0
  175. package/src/presets/traditional-web-app.ts +339 -0
  176. package/src/presets/wordpress.ts +138 -0
  177. package/src/preview/github.test.ts +249 -0
  178. package/src/preview/github.ts +297 -0
  179. package/src/preview/index.ts +37 -0
  180. package/src/preview/manager.test.ts +440 -0
  181. package/src/preview/manager.ts +326 -0
  182. package/src/preview/notifications.test.ts +582 -0
  183. package/src/preview/notifications.ts +341 -0
  184. package/src/queue/batch-processing.ts +402 -0
  185. package/src/queue/dlq-monitoring.ts +402 -0
  186. package/src/queue/fifo.ts +342 -0
  187. package/src/queue/index.ts +9 -0
  188. package/src/queue/management.ts +428 -0
  189. package/src/queue/queue.test.ts +429 -0
  190. package/src/resource-mgmt/index.ts +39 -0
  191. package/src/resource-naming.ts +62 -0
  192. package/src/s3/index.ts +523 -0
  193. package/src/schema/cloud-config.schema.json +554 -0
  194. package/src/schema/index.ts +68 -0
  195. package/src/security/certificate-manager.ts +492 -0
  196. package/src/security/index.ts +9 -0
  197. package/src/security/scanning.ts +545 -0
  198. package/src/security/secrets-manager.ts +476 -0
  199. package/src/security/secrets-rotation.ts +456 -0
  200. package/src/security/security.test.ts +738 -0
  201. package/src/sms/advanced/ab-testing.ts +389 -0
  202. package/src/sms/advanced/analytics.ts +336 -0
  203. package/src/sms/advanced/campaigns.ts +523 -0
  204. package/src/sms/advanced/chatbot.ts +224 -0
  205. package/src/sms/advanced/index.ts +10 -0
  206. package/src/sms/advanced/link-tracking.ts +248 -0
  207. package/src/sms/advanced/mms.ts +308 -0
  208. package/src/sms/handlers/__tests__/send.test.ts +40 -0
  209. package/src/sms/handlers/delivery-status.ts +133 -0
  210. package/src/sms/handlers/receive.ts +162 -0
  211. package/src/sms/handlers/send.ts +174 -0
  212. package/src/sms/index.ts +9 -0
  213. package/src/stack-diff.ts +389 -0
  214. package/src/static-site/index.ts +85 -0
  215. package/src/template-builder.ts +110 -0
  216. package/src/template-validator.ts +574 -0
  217. package/src/utils/cache.ts +291 -0
  218. package/src/utils/diff.ts +269 -0
  219. package/src/utils/hash.ts +227 -0
  220. package/src/utils/index.ts +8 -0
  221. package/src/utils/parallel.ts +294 -0
  222. package/src/validators/credentials.test.ts +274 -0
  223. package/src/validators/credentials.ts +233 -0
  224. package/src/validators/quotas.test.ts +434 -0
  225. package/src/validators/quotas.ts +217 -0
@@ -0,0 +1,534 @@
1
+ /**
2
+ * Database Replicas & Connection Pooling
3
+ * Read replica management and RDS Proxy for connection pooling
4
+ */
5
+
6
+ export interface ReadReplica {
7
+ id: string
8
+ name: string
9
+ sourceDatabase: string
10
+ region: string
11
+ instanceClass: string
12
+ multiAZ?: boolean
13
+ autoMinorVersionUpgrade?: boolean
14
+ backupRetentionPeriod?: number
15
+ preferredBackupWindow?: string
16
+ preferredMaintenanceWindow?: string
17
+ replicationLag?: number // milliseconds
18
+ status?: 'creating' | 'available' | 'failing-over' | 'failed'
19
+ }
20
+
21
+ export interface ReplicationGroup {
22
+ id: string
23
+ name: string
24
+ primaryDatabase: string
25
+ replicas: ReadReplica[]
26
+ loadBalancing?: LoadBalancingStrategy
27
+ failoverEnabled?: boolean
28
+ autoScaling?: AutoScalingConfig
29
+ }
30
+
31
+ export interface LoadBalancingStrategy {
32
+ type: 'round-robin' | 'least-connections' | 'weighted' | 'latency-based'
33
+ weights?: Record<string, number> // replica ID -> weight
34
+ }
35
+
36
+ export interface AutoScalingConfig {
37
+ enabled: boolean
38
+ minReplicas: number
39
+ maxReplicas: number
40
+ targetCPU?: number
41
+ targetConnections?: number
42
+ scaleUpCooldown?: number // seconds
43
+ scaleDownCooldown?: number // seconds
44
+ }
45
+
46
+ export interface RDSProxy {
47
+ id: string
48
+ name: string
49
+ engineFamily: 'MYSQL' | 'POSTGRESQL' | 'SQLSERVER'
50
+ targetDatabase: string
51
+ maxConnectionsPercent?: number
52
+ maxIdleConnectionsPercent?: number
53
+ connectionBorrowTimeout?: number // seconds
54
+ sessionPinningFilters?: SessionPinningFilter[]
55
+ requireTLS?: boolean
56
+ idleClientTimeout?: number // seconds
57
+ vpcSubnetIds: string[]
58
+ securityGroupIds: string[]
59
+ secretArn?: string
60
+ }
61
+
62
+ export type SessionPinningFilter =
63
+ | 'EXCLUDE_VARIABLE_SETS'
64
+
65
+ export interface ProxyTarget {
66
+ id: string
67
+ proxyId: string
68
+ targetArn: string
69
+ targetType: 'RDS_INSTANCE' | 'RDS_CLUSTER' | 'TRACKED_CLUSTER'
70
+ isWritable: boolean
71
+ weight?: number
72
+ }
73
+
74
+ export interface ConnectionPoolConfig {
75
+ minPoolSize: number
76
+ maxPoolSize: number
77
+ connectionTimeout: number // seconds
78
+ idleTimeout: number // seconds
79
+ maxLifetime?: number // seconds
80
+ statementTimeout?: number // seconds
81
+ }
82
+
83
+ /**
84
+ * Replica manager
85
+ */
86
+ export class ReplicaManager {
87
+ private replicas: Map<string, ReadReplica> = new Map()
88
+ private replicationGroups: Map<string, ReplicationGroup> = new Map()
89
+ private proxies: Map<string, RDSProxy> = new Map()
90
+ private proxyTargets: Map<string, ProxyTarget> = new Map()
91
+ private replicaCounter = 0
92
+ private groupCounter = 0
93
+ private proxyCounter = 0
94
+ private targetCounter = 0
95
+
96
+ /**
97
+ * Create read replica
98
+ */
99
+ createReplica(replica: Omit<ReadReplica, 'id'>): ReadReplica {
100
+ const id = `replica-${Date.now()}-${this.replicaCounter++}`
101
+
102
+ const readReplica: ReadReplica = {
103
+ id,
104
+ status: 'creating',
105
+ ...replica,
106
+ }
107
+
108
+ this.replicas.set(id, readReplica)
109
+
110
+ return readReplica
111
+ }
112
+
113
+ /**
114
+ * Create read replica for RDS instance
115
+ */
116
+ createRDSReplica(options: {
117
+ sourceDatabase: string
118
+ name: string
119
+ region?: string
120
+ instanceClass?: string
121
+ multiAZ?: boolean
122
+ }): ReadReplica {
123
+ return this.createReplica({
124
+ name: options.name,
125
+ sourceDatabase: options.sourceDatabase,
126
+ region: options.region || 'us-east-1',
127
+ instanceClass: options.instanceClass || 'db.t3.medium',
128
+ multiAZ: options.multiAZ || false,
129
+ autoMinorVersionUpgrade: true,
130
+ backupRetentionPeriod: 7,
131
+ })
132
+ }
133
+
134
+ /**
135
+ * Create cross-region replica
136
+ */
137
+ createCrossRegionReplica(options: {
138
+ sourceDatabase: string
139
+ name: string
140
+ targetRegion: string
141
+ instanceClass?: string
142
+ encrypted?: boolean
143
+ }): ReadReplica {
144
+ return this.createReplica({
145
+ name: options.name,
146
+ sourceDatabase: options.sourceDatabase,
147
+ region: options.targetRegion,
148
+ instanceClass: options.instanceClass || 'db.t3.medium',
149
+ multiAZ: true, // Cross-region replicas should be multi-AZ
150
+ autoMinorVersionUpgrade: true,
151
+ backupRetentionPeriod: 7,
152
+ })
153
+ }
154
+
155
+ /**
156
+ * Create replication group
157
+ */
158
+ createReplicationGroup(group: Omit<ReplicationGroup, 'id'>): ReplicationGroup {
159
+ const id = `replication-group-${Date.now()}-${this.groupCounter++}`
160
+
161
+ const replicationGroup: ReplicationGroup = {
162
+ id,
163
+ ...group,
164
+ }
165
+
166
+ this.replicationGroups.set(id, replicationGroup)
167
+
168
+ return replicationGroup
169
+ }
170
+
171
+ /**
172
+ * Create replication group with auto-scaling
173
+ */
174
+ createAutoScalingReplicationGroup(options: {
175
+ name: string
176
+ primaryDatabase: string
177
+ minReplicas: number
178
+ maxReplicas: number
179
+ targetCPU?: number
180
+ loadBalancing?: LoadBalancingStrategy
181
+ }): ReplicationGroup {
182
+ return this.createReplicationGroup({
183
+ name: options.name,
184
+ primaryDatabase: options.primaryDatabase,
185
+ replicas: [],
186
+ loadBalancing: options.loadBalancing || { type: 'round-robin' },
187
+ failoverEnabled: true,
188
+ autoScaling: {
189
+ enabled: true,
190
+ minReplicas: options.minReplicas,
191
+ maxReplicas: options.maxReplicas,
192
+ targetCPU: options.targetCPU || 70,
193
+ scaleUpCooldown: 300,
194
+ scaleDownCooldown: 600,
195
+ },
196
+ })
197
+ }
198
+
199
+ /**
200
+ * Add replica to group
201
+ */
202
+ addReplicaToGroup(groupId: string, replica: ReadReplica): void {
203
+ const group = this.replicationGroups.get(groupId)
204
+
205
+ if (!group) {
206
+ throw new Error(`Replication group not found: ${groupId}`)
207
+ }
208
+
209
+ group.replicas.push(replica)
210
+ }
211
+
212
+ /**
213
+ * Create RDS Proxy
214
+ */
215
+ createProxy(proxy: Omit<RDSProxy, 'id'>): RDSProxy {
216
+ const id = `rds-proxy-${Date.now()}-${this.proxyCounter++}`
217
+
218
+ const rdsProxy: RDSProxy = {
219
+ id,
220
+ ...proxy,
221
+ }
222
+
223
+ this.proxies.set(id, rdsProxy)
224
+
225
+ return rdsProxy
226
+ }
227
+
228
+ /**
229
+ * Create RDS Proxy for connection pooling
230
+ */
231
+ createConnectionPoolProxy(options: {
232
+ name: string
233
+ engineFamily: 'MYSQL' | 'POSTGRESQL' | 'SQLSERVER'
234
+ targetDatabase: string
235
+ vpcSubnetIds: string[]
236
+ securityGroupIds: string[]
237
+ secretArn: string
238
+ maxConnections?: number
239
+ }): RDSProxy {
240
+ return this.createProxy({
241
+ name: options.name,
242
+ engineFamily: options.engineFamily,
243
+ targetDatabase: options.targetDatabase,
244
+ maxConnectionsPercent: options.maxConnections || 100,
245
+ maxIdleConnectionsPercent: 50,
246
+ connectionBorrowTimeout: 120,
247
+ sessionPinningFilters: ['EXCLUDE_VARIABLE_SETS'],
248
+ requireTLS: true,
249
+ idleClientTimeout: 1800,
250
+ vpcSubnetIds: options.vpcSubnetIds,
251
+ securityGroupIds: options.securityGroupIds,
252
+ secretArn: options.secretArn,
253
+ })
254
+ }
255
+
256
+ /**
257
+ * Create serverless proxy (optimized for Lambda)
258
+ */
259
+ createServerlessProxy(options: {
260
+ name: string
261
+ engineFamily: 'MYSQL' | 'POSTGRESQL' | 'SQLSERVER'
262
+ targetDatabase: string
263
+ vpcSubnetIds: string[]
264
+ securityGroupIds: string[]
265
+ secretArn: string
266
+ }): RDSProxy {
267
+ return this.createProxy({
268
+ name: options.name,
269
+ engineFamily: options.engineFamily,
270
+ targetDatabase: options.targetDatabase,
271
+ maxConnectionsPercent: 100,
272
+ maxIdleConnectionsPercent: 10, // Low for serverless to reduce idle connections
273
+ connectionBorrowTimeout: 60, // Lower timeout for serverless
274
+ sessionPinningFilters: ['EXCLUDE_VARIABLE_SETS'],
275
+ requireTLS: true,
276
+ idleClientTimeout: 300, // Shorter timeout for serverless
277
+ vpcSubnetIds: options.vpcSubnetIds,
278
+ securityGroupIds: options.securityGroupIds,
279
+ secretArn: options.secretArn,
280
+ })
281
+ }
282
+
283
+ /**
284
+ * Add proxy target
285
+ */
286
+ addProxyTarget(target: Omit<ProxyTarget, 'id'>): ProxyTarget {
287
+ const id = `proxy-target-${Date.now()}-${this.targetCounter++}`
288
+
289
+ const proxyTarget: ProxyTarget = {
290
+ id,
291
+ ...target,
292
+ }
293
+
294
+ this.proxyTargets.set(id, proxyTarget)
295
+
296
+ return proxyTarget
297
+ }
298
+
299
+ /**
300
+ * Promote replica to primary
301
+ */
302
+ promoteReplica(replicaId: string): { success: boolean; message: string } {
303
+ const replica = this.replicas.get(replicaId)
304
+
305
+ if (!replica) {
306
+ return { success: false, message: 'Replica not found' }
307
+ }
308
+
309
+ console.log(`Promoting replica to primary: ${replica.name}`)
310
+ console.log(`Region: ${replica.region}`)
311
+ console.log(`\n1. Checking replication lag...`)
312
+
313
+ if (replica.replicationLag && replica.replicationLag > 5000) {
314
+ return {
315
+ success: false,
316
+ message: `Replication lag too high: ${replica.replicationLag}ms`,
317
+ }
318
+ }
319
+
320
+ console.log(` Replication lag: ${replica.replicationLag || 0}ms (acceptable)`)
321
+ console.log(`\n2. Promoting replica to standalone instance...`)
322
+ console.log(`\n3. Updating DNS records...`)
323
+ console.log(`\n4. Updating application configuration...`)
324
+ console.log(`\n✓ Promotion completed successfully`)
325
+
326
+ replica.status = 'available'
327
+
328
+ return { success: true, message: 'Replica promoted successfully' }
329
+ }
330
+
331
+ /**
332
+ * Get replication lag for replica
333
+ */
334
+ getReplicationLag(replicaId: string): number {
335
+ const replica = this.replicas.get(replicaId)
336
+
337
+ if (!replica) {
338
+ throw new Error(`Replica not found: ${replicaId}`)
339
+ }
340
+
341
+ // Simulate replication lag (in production, query RDS metrics)
342
+ return Math.floor(Math.random() * 1000)
343
+ }
344
+
345
+ /**
346
+ * Get replica
347
+ */
348
+ getReplica(id: string): ReadReplica | undefined {
349
+ return this.replicas.get(id)
350
+ }
351
+
352
+ /**
353
+ * List replicas
354
+ */
355
+ listReplicas(): ReadReplica[] {
356
+ return Array.from(this.replicas.values())
357
+ }
358
+
359
+ /**
360
+ * Get replication group
361
+ */
362
+ getReplicationGroup(id: string): ReplicationGroup | undefined {
363
+ return this.replicationGroups.get(id)
364
+ }
365
+
366
+ /**
367
+ * List replication groups
368
+ */
369
+ listReplicationGroups(): ReplicationGroup[] {
370
+ return Array.from(this.replicationGroups.values())
371
+ }
372
+
373
+ /**
374
+ * Get proxy
375
+ */
376
+ getProxy(id: string): RDSProxy | undefined {
377
+ return this.proxies.get(id)
378
+ }
379
+
380
+ /**
381
+ * List proxies
382
+ */
383
+ listProxies(): RDSProxy[] {
384
+ return Array.from(this.proxies.values())
385
+ }
386
+
387
+ /**
388
+ * Generate CloudFormation for read replica
389
+ */
390
+ generateReplicaCF(replica: ReadReplica): any {
391
+ return {
392
+ Type: 'AWS::RDS::DBInstance',
393
+ Properties: {
394
+ SourceDBInstanceIdentifier: replica.sourceDatabase,
395
+ DBInstanceIdentifier: replica.name,
396
+ DBInstanceClass: replica.instanceClass,
397
+ MultiAZ: replica.multiAZ || false,
398
+ AutoMinorVersionUpgrade: replica.autoMinorVersionUpgrade ?? true,
399
+ ...(replica.backupRetentionPeriod && {
400
+ BackupRetentionPeriod: replica.backupRetentionPeriod,
401
+ }),
402
+ ...(replica.preferredBackupWindow && {
403
+ PreferredBackupWindow: replica.preferredBackupWindow,
404
+ }),
405
+ ...(replica.preferredMaintenanceWindow && {
406
+ PreferredMaintenanceWindow: replica.preferredMaintenanceWindow,
407
+ }),
408
+ },
409
+ }
410
+ }
411
+
412
+ /**
413
+ * Generate CloudFormation for RDS Proxy
414
+ */
415
+ generateProxyCF(proxy: RDSProxy): any {
416
+ return {
417
+ Type: 'AWS::RDS::DBProxy',
418
+ Properties: {
419
+ DBProxyName: proxy.name,
420
+ EngineFamily: proxy.engineFamily,
421
+ Auth: [
422
+ {
423
+ AuthScheme: 'SECRETS',
424
+ SecretArn: proxy.secretArn,
425
+ IAMAuth: 'DISABLED',
426
+ },
427
+ ],
428
+ RoleArn: { 'Fn::GetAtt': ['RDSProxyRole', 'Arn'] },
429
+ VpcSubnetIds: proxy.vpcSubnetIds,
430
+ VpcSecurityGroupIds: proxy.securityGroupIds,
431
+ RequireTLS: proxy.requireTLS ?? true,
432
+ IdleClientTimeout: proxy.idleClientTimeout || 1800,
433
+ ...(proxy.maxConnectionsPercent && {
434
+ MaxConnectionsPercent: proxy.maxConnectionsPercent,
435
+ }),
436
+ ...(proxy.maxIdleConnectionsPercent && {
437
+ MaxIdleConnectionsPercent: proxy.maxIdleConnectionsPercent,
438
+ }),
439
+ ...(proxy.connectionBorrowTimeout && {
440
+ ConnectionBorrowTimeout: proxy.connectionBorrowTimeout,
441
+ }),
442
+ },
443
+ }
444
+ }
445
+
446
+ /**
447
+ * Generate CloudFormation for proxy target
448
+ */
449
+ generateProxyTargetCF(target: ProxyTarget, proxy: RDSProxy): any {
450
+ return {
451
+ Type: 'AWS::RDS::DBProxyTargetGroup',
452
+ Properties: {
453
+ DBProxyName: proxy.name,
454
+ TargetGroupName: 'default',
455
+ DBInstanceIdentifiers: [target.targetArn],
456
+ ConnectionPoolConfig: {
457
+ MaxConnectionsPercent: 100,
458
+ MaxIdleConnectionsPercent: 50,
459
+ ConnectionBorrowTimeout: 120,
460
+ },
461
+ },
462
+ }
463
+ }
464
+
465
+ /**
466
+ * Generate CloudFormation for proxy IAM role
467
+ */
468
+ generateProxyRoleCF(): any {
469
+ return {
470
+ Type: 'AWS::IAM::Role',
471
+ Properties: {
472
+ AssumeRolePolicyDocument: {
473
+ Version: '2012-10-17',
474
+ Statement: [
475
+ {
476
+ Effect: 'Allow',
477
+ Principal: {
478
+ Service: 'rds.amazonaws.com',
479
+ },
480
+ Action: 'sts:AssumeRole',
481
+ },
482
+ ],
483
+ },
484
+ Policies: [
485
+ {
486
+ PolicyName: 'RDSProxySecretsPolicy',
487
+ PolicyDocument: {
488
+ Version: '2012-10-17',
489
+ Statement: [
490
+ {
491
+ Effect: 'Allow',
492
+ Action: [
493
+ 'secretsmanager:GetSecretValue',
494
+ 'secretsmanager:DescribeSecret',
495
+ ],
496
+ Resource: 'arn:aws:secretsmanager:*:*:secret:*',
497
+ },
498
+ {
499
+ Effect: 'Allow',
500
+ Action: ['kms:Decrypt'],
501
+ Resource: 'arn:aws:kms:*:*:key/*',
502
+ Condition: {
503
+ StringEquals: {
504
+ 'kms:ViaService': 'secretsmanager.*.amazonaws.com',
505
+ },
506
+ },
507
+ },
508
+ ],
509
+ },
510
+ },
511
+ ],
512
+ },
513
+ }
514
+ }
515
+
516
+ /**
517
+ * Clear all data
518
+ */
519
+ clear(): void {
520
+ this.replicas.clear()
521
+ this.replicationGroups.clear()
522
+ this.proxies.clear()
523
+ this.proxyTargets.clear()
524
+ this.replicaCounter = 0
525
+ this.groupCounter = 0
526
+ this.proxyCounter = 0
527
+ this.targetCounter = 0
528
+ }
529
+ }
530
+
531
+ /**
532
+ * Global replica manager instance
533
+ */
534
+ export const replicaManager: ReplicaManager = new ReplicaManager()