@medplum/cdk 2.0.19 → 2.0.21

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.
package/src/init.ts DELETED
@@ -1,513 +0,0 @@
1
- import { ACMClient, CertificateSummary, ListCertificatesCommand, RequestCertificateCommand } from '@aws-sdk/client-acm';
2
- import { PutParameterCommand, SSMClient } from '@aws-sdk/client-ssm';
3
- import { GetCallerIdentityCommand, STSClient } from '@aws-sdk/client-sts';
4
- import { generateKeyPairSync, randomUUID } from 'crypto';
5
- import { existsSync, writeFileSync } from 'fs';
6
- import { resolve } from 'path';
7
- import readline from 'readline';
8
- import { MedplumInfraConfig } from './config';
9
-
10
- type MedplumDomainType = 'api' | 'app' | 'storage';
11
- type MedplumDomainSetting = `${MedplumDomainType}DomainName`;
12
- type MedplumDomainCertSetting = `${MedplumDomainType}SslCertArn`;
13
-
14
- const getDomainSetting = (domain: MedplumDomainType): MedplumDomainSetting => `${domain}DomainName`;
15
- const getDomainCertSetting = (domain: MedplumDomainType): MedplumDomainCertSetting => `${domain}SslCertArn`;
16
-
17
- let terminal: readline.Interface;
18
-
19
- export async function main(t: readline.Interface): Promise<void> {
20
- const config = { apiPort: 8103, region: 'us-east-1' } as MedplumInfraConfig;
21
- terminal = t;
22
- header('MEDPLUM');
23
- print('This tool prepares the necessary prerequisites for deploying Medplum in your AWS account.');
24
- print('');
25
- print('Most Medplum infrastructure is deployed using the AWS CDK.');
26
- print('However, some AWS resources must be created manually, such as email addresses and SSL certificates.');
27
- print('This tool will help you create those resources.');
28
- print('');
29
- print('Upon completion, this tool will:');
30
- print(' 1. Generate a Medplum CDK config file (i.e., medplum.demo.config.json)');
31
- print(' 2. Optionally generate an AWS CloudFront signing key');
32
- print(' 3. Optionally request SSL certificates from AWS Certificate Manager');
33
- print(' 4. Optionally write server config settings to AWS Parameter Store');
34
- print('');
35
- print('The Medplum infra config file is an input to the Medplum CDK.');
36
- print('The Medplum CDK will create and manage the necessary AWS resources.');
37
- print('');
38
- print('We will ask a series of questions to generate your infra config file.');
39
- print('Some questions have predefined options in [square brackets].');
40
- print('Some questions have default values in (parentheses), which you can accept by pressing Enter.');
41
- print('Press Ctrl+C at any time to exit.');
42
-
43
- header('ENVIRONMENT NAME');
44
- print('Medplum deployments have a short environment name such as "prod", "staging", "alice", or "demo".');
45
- print('The environment name is used in multiple places:');
46
- print(' 1. As part of config file names (i.e., medplum.demo.config.json)');
47
- print(' 2. As the base of CloudFormation stack names (i.e., MedplumDemo)');
48
- print(' 3. AWS Parameter Store keys (i.e., /medplum/demo/...)');
49
- config.name = await ask('What is your environment name?', 'demo');
50
- print('Using environment name "' + config.name + '"...');
51
-
52
- header('CONFIG FILE');
53
- print('Medplum Infrastructure will create a config file in the current directory.');
54
- const configFileName = await ask('What is the config file name?', `medplum.${config.name}.config.json`);
55
- if (existsSync(configFileName)) {
56
- print('Config file already exists.');
57
- await checkOk('Do you want to overwrite the config file?');
58
- }
59
- print('Using config file "' + configFileName + '"...');
60
- writeConfig(configFileName, config);
61
-
62
- header('AWS REGION');
63
- print('Most Medplum resources will be created in a single AWS region.');
64
- config.region = await ask('Enter your AWS region:', 'us-east-1');
65
- writeConfig(configFileName, config);
66
-
67
- header('AWS ACCOUNT NUMBER');
68
- print('Medplum Infrastructure will use your AWS account number to create AWS resources.');
69
- const currentAccountId = await getAccountId(config.region);
70
- print('Using the AWS CLI, your current account ID is: ' + currentAccountId);
71
- config.accountNumber = await ask('What is your AWS account number?', currentAccountId);
72
- writeConfig(configFileName, config);
73
-
74
- header('STACK NAME');
75
- print('Medplum will create a CloudFormation stack to manage AWS resources.');
76
- const defaultStackName = 'Medplum' + config.name.charAt(0).toUpperCase() + config.name.slice(1);
77
- config.stackName = await ask('Enter your CloudFormation stack name?', defaultStackName);
78
- writeConfig(configFileName, config);
79
-
80
- header('BASE DOMAIN NAME');
81
- print('Medplum deploys multiple subdomains for various services.');
82
- print('');
83
- print('For example, "api." for the REST API and "app." for the web application.');
84
- print('The base domain name is the common suffix for all subdomains.');
85
- print('');
86
- print('For example, if your base domain name is "example.com",');
87
- print('then the REST API will be "api.example.com".');
88
- print('');
89
- print('Note that you must own the base domain, and it must use Route53 DNS.');
90
- print('Medplum will create subdomains for you, but you must configure the base domain.');
91
- while (!config.domainName) {
92
- config.domainName = await ask('Enter your base domain name:');
93
- }
94
- writeConfig(configFileName, config);
95
-
96
- header('SUPPORT EMAIL');
97
- print('Medplum sends transactional emails to users.');
98
- print('For example, emails to new users or for password reset.');
99
- print('Medplum will use the support email address to send these emails.');
100
- print('Note that you must verify the support email address in SES.');
101
- const supportEmail = await ask('Enter your support email address:');
102
-
103
- header('API DOMAIN NAME');
104
- print('Medplum deploys a REST API for the backend services.');
105
- config.apiDomainName = await ask('Enter your REST API domain name:', 'api.' + config.domainName);
106
- writeConfig(configFileName, config);
107
-
108
- header('APP DOMAIN NAME');
109
- print('Medplum deploys a web application for the user interface.');
110
- config.appDomainName = await ask('Enter your web application domain name:', 'app.' + config.domainName);
111
- writeConfig(configFileName, config);
112
-
113
- header('STORAGE DOMAIN NAME');
114
- print('Medplum deploys a storage service for file uploads.');
115
- config.storageDomainName = await ask('Enter your storage domain name:', 'storage.' + config.domainName);
116
- writeConfig(configFileName, config);
117
-
118
- header('STORAGE BUCKET');
119
- print('Medplum uses an S3 bucket to store binary content such as file uploads.');
120
- print('Medplum will create a the S3 bucket as part of the CloudFormation stack.');
121
- config.storageBucketName = await ask('Enter your storage bucket name:', 'medplum-' + config.name + '-storage');
122
- writeConfig(configFileName, config);
123
-
124
- header('MAX AVAILABILITY ZONES');
125
- print('Medplum API servers can be deployed in multiple availability zones.');
126
- print('This provides redundancy and high availability.');
127
- print('However, it also increases the cost of the deployment.');
128
- print('If you want to use all availability zones, choose a large number such as 99.');
129
- print('If you want to restrict the number, for example to manage EIP limits,');
130
- print('then choose a small number such as 1 or 2.');
131
- config.maxAzs = await chooseInt('Enter the maximum number of availability zones:', [1, 2, 3, 99], 2);
132
-
133
- header('DATABASE INSTANCES');
134
- print('Medplum uses a relational database to store data.');
135
- print('You can set up your own database,');
136
- print('or Medplum can create a new RDS database as part of the CloudFormation stack.');
137
- if (await yesOrNo('Do you want to create a new RDS database as part of the CloudFormation stack?')) {
138
- print('Medplum will create a new RDS database as part of the CloudFormation stack.');
139
- print('');
140
- print('If you need high availability, you can choose multiple instances.');
141
- print('Use 1 for a single instance, or 2 for a primary and a standby.');
142
- config.rdsInstances = await chooseInt('Enter the number of database instances:', [1, 2], 1);
143
- } else {
144
- print('Medplum will not create a new RDS database.');
145
- print('Please create a new RDS database and enter the database name, username, and password.');
146
- print('Set the AWS Secrets Manager secret ARN in the config file in the "rdsSecretsArn" setting.');
147
- config.rdsSecretsArn = 'TODO';
148
- }
149
- writeConfig(configFileName, config);
150
-
151
- header('SERVER INSTANCES');
152
- print('Medplum uses AWS Fargate to run the API servers.');
153
- print('Medplum will create a new Fargate cluster as part of the CloudFormation stack.');
154
- print('Fargate will automatically scale the number of servers up and down.');
155
- print('If you need high availability, you can choose multiple instances.');
156
- config.desiredServerCount = await chooseInt('Enter the number of server instances:', [1, 2, 3, 4, 6, 8], 1);
157
- writeConfig(configFileName, config);
158
-
159
- header('SERVER MEMORY');
160
- print('You can choose the amount of memory for each server instance.');
161
- print('The default is 512 MB, which is sufficient for getting started.');
162
- print('Note that only certain CPU units are compatible with memory units.');
163
- print('Consult AWS Fargate "Task Definition Parameters" for more information.');
164
- config.serverMemory = await chooseInt('Enter the server memory (MB):', [512, 1024, 2048, 4096, 8192, 16384], 512);
165
- writeConfig(configFileName, config);
166
-
167
- header('SERVER CPU');
168
- print('You can choose the amount of CPU for each server instance.');
169
- print('CPU is expressed as an integer using AWS CPU units');
170
- print('The default is 256, which is sufficient for getting started.');
171
- print('Note that only certain CPU units are compatible with memory units.');
172
- print('Consult AWS Fargate "Task Definition Parameters" for more information.');
173
- config.serverCpu = await chooseInt('Enter the server CPU:', [256, 512, 1024, 2048, 4096, 8192, 16384], 256);
174
- writeConfig(configFileName, config);
175
-
176
- header('SERVER IMAGE');
177
- print('Medplum uses Docker images for the API servers.');
178
- print('You can choose the image to use for the servers.');
179
- print('Docker images can be loaded from either Docker Hub or AWS ECR.');
180
- print('The default is the latest Medplum release.');
181
- config.serverImage = await ask('Enter the server image:', 'medplum/medplum-server:latest');
182
- writeConfig(configFileName, config);
183
-
184
- header('SIGNING KEY');
185
- print('Medplum uses AWS CloudFront Presigned URLs for binary content such as file uploads.');
186
- const { privateKey, publicKey, passphrase } = generateSigningKey();
187
- config.storagePublicKey = publicKey;
188
- writeConfig(configFileName, config);
189
-
190
- header('SSL CERTIFICATES');
191
- print(`Medplum will now check for existing SSL certificates for the subdomains.`);
192
- const allCerts = await listAllCertificates(config.region);
193
- print('Found ' + allCerts.length + ' certificate(s).');
194
-
195
- // Process certificates for each subdomain
196
- // Note: The "api" certificate must be created in the same region as the API
197
- // Note: The "app" and "storage" certificates must be created in us-east-1
198
- for (const { region, certName } of [
199
- { region: config.region, certName: 'api' },
200
- { region: 'us-east-1', certName: 'app' },
201
- { region: 'us-east-1', certName: 'storage' },
202
- ] as const) {
203
- print('');
204
- const arn = await processCert(config, allCerts, region, certName);
205
- config[getDomainCertSetting(certName)] = arn;
206
- writeConfig(configFileName, config);
207
- }
208
-
209
- header('AWS PARAMETER STORE');
210
- print('Medplum uses AWS Parameter Store to store sensitive configuration values.');
211
- print('These values will be encrypted at rest.');
212
- print(`The values will be stored in the "/medplum/${config.name}" path.`);
213
-
214
- const serverParams = {
215
- port: config.apiPort,
216
- baseUrl: `https://${config.apiDomainName}/`,
217
- appBaseUrl: `https://${config.appDomainName}/`,
218
- storageBaseUrl: `https://${config.storageDomainName}/binary/`,
219
- binaryStorage: `s3:${config.storageBucketName}`,
220
- signingKey: privateKey,
221
- signingKeyPassphrase: passphrase,
222
- supportEmail: supportEmail,
223
- };
224
-
225
- print(
226
- JSON.stringify(
227
- {
228
- ...serverParams,
229
- signingKey: '****',
230
- signingKeyPassphrase: '****',
231
- },
232
- null,
233
- 2
234
- )
235
- );
236
-
237
- await checkOk('Do you want to store these values in AWS Parameter Store?');
238
- await writeParameters(config.region, `/medplum/${config.name}/`, serverParams);
239
-
240
- header('DONE!');
241
- print('Medplum configuration complete.');
242
- print('You can now proceed to deploying the Medplum infrastructure with CDK.');
243
- print('Run:');
244
- print('');
245
- print(` npx cdk bootstrap -c config=${configFileName}`);
246
- print(` npx cdk synth -c config=${configFileName}`);
247
- if (config.region === 'us-east-1') {
248
- print(` npx cdk deploy -c config=${configFileName}`);
249
- } else {
250
- print(` npx cdk deploy -c config=${configFileName} --all`);
251
- }
252
- print('');
253
- print('See Medplum documentation for more information:');
254
- print('');
255
- print(' https://www.medplum.com/docs/self-hosting/install-on-aws');
256
- print('');
257
- }
258
-
259
- /** Prints to stdout. */
260
- function print(text: string): void {
261
- terminal.write(text + '\n');
262
- }
263
-
264
- /** Prints a header with extra line spacing. */
265
- function header(text: string): void {
266
- print('\n' + text + '\n');
267
- }
268
-
269
- /** Prints a question and waits for user input. */
270
- function ask(text: string, defaultValue?: string | number): Promise<string> {
271
- return new Promise((resolve) => {
272
- terminal.question(text + (defaultValue ? ' (' + defaultValue + ')' : '') + ' ', (answer: string) => {
273
- resolve(answer || defaultValue?.toString() || '');
274
- });
275
- });
276
- }
277
-
278
- /** Prints a question and waits for user to choose one of the provided options. */
279
- async function choose(text: string, options: (string | number)[], defaultValue?: string): Promise<string> {
280
- const str = text + ' [' + options.map((o) => (o === defaultValue ? '(' + o + ')' : o)).join('|') + ']';
281
- // eslint-disable-next-line no-constant-condition
282
- while (true) {
283
- const answer = (await ask(str)) || defaultValue || '';
284
- if (options.includes(answer)) {
285
- return answer;
286
- }
287
- print('Please choose one of the following options: ' + options.join(', '));
288
- }
289
- }
290
-
291
- /** Prints a question and waits for the user to choose a valid integer option. */
292
- async function chooseInt(text: string, options: number[], defaultValue?: number): Promise<number> {
293
- return parseInt(
294
- await choose(
295
- text,
296
- options.map((o) => o.toString()),
297
- defaultValue?.toString() || '0'
298
- )
299
- );
300
- }
301
-
302
- /** Prints a question and waits for the user to choose yes or no. */
303
- async function yesOrNo(text: string): Promise<boolean> {
304
- return (await choose(text, ['y', 'n'])).toLowerCase() === 'y';
305
- }
306
-
307
- /** Prints a question and waits for the user to confirm yes. Throws error on no, and exits the program. */
308
- async function checkOk(text: string): Promise<void> {
309
- if (!(await yesOrNo(text))) {
310
- print('Exiting...');
311
- throw new Error('User cancelled');
312
- }
313
- }
314
-
315
- /**
316
- * Writes a config file to disk.
317
- * @param configFileName The config file name.
318
- * @param config The config file contents.
319
- */
320
- function writeConfig(configFileName: string, config: MedplumInfraConfig): void {
321
- writeFileSync(resolve(configFileName), JSON.stringify(config, undefined, 2), 'utf-8');
322
- }
323
-
324
- /**
325
- * Returns the current AWS account ID.
326
- * This is used as the default value for the "accountNumber" config setting.
327
- * @param region The AWS region.
328
- * @returns The AWS account ID.
329
- */
330
- async function getAccountId(region: string): Promise<string | undefined> {
331
- try {
332
- const client = new STSClient({ region });
333
- const command = new GetCallerIdentityCommand({});
334
- const response = await client.send(command);
335
- return response.Account as string;
336
- } catch (err) {
337
- console.log('Warning: Unable to get AWS account ID', (err as Error).message);
338
- return undefined;
339
- }
340
- }
341
-
342
- /**
343
- * Returns a list of all AWS certificates.
344
- * This is used to find existing certificates for the subdomains.
345
- * If the primary region is not us-east-1, then certificates in us-east-1 will also be returned.
346
- * @param region The AWS region.
347
- * @returns The list of AWS Certificates.
348
- */
349
- async function listAllCertificates(region: string): Promise<CertificateSummary[]> {
350
- const result = await listCertificates(region);
351
- if (region !== 'us-east-1') {
352
- const usEast1Result = await listCertificates('us-east-1');
353
- result.push(...usEast1Result);
354
- }
355
- return result;
356
- }
357
-
358
- /**
359
- * Returns a list of AWS Certificates.
360
- * This is used to find existing certificates for the subdomains.
361
- * @param region The AWS region.
362
- * @returns The list of AWS Certificates.
363
- */
364
- async function listCertificates(region: string): Promise<CertificateSummary[]> {
365
- try {
366
- const client = new ACMClient({ region });
367
- const command = new ListCertificatesCommand({ MaxItems: 1000 });
368
- const response = await client.send(command);
369
- return response.CertificateSummaryList as CertificateSummary[];
370
- } catch (err) {
371
- console.log('Warning: Unable to list certificates', (err as Error).message);
372
- return [];
373
- }
374
- }
375
-
376
- /**
377
- * Processes a required certificate.
378
- *
379
- * 1. If the certificate already exists, return the ARN.
380
- * 2. If the certificate does not exist, and the user wants to create a new certificate, create it and return the ARN.
381
- * 3. If the certificate does not exist, and the user does not want to create a new certificate, return a placeholder.
382
- *
383
- * @param config In-progress config settings.
384
- * @param allCerts List of all existing certificates.
385
- * @param region The AWS region where the certificate is needed.
386
- * @param certName The name of the certificate (api, app, or storage).
387
- * @returns The ARN of the certificate or placeholder if a new certificate is needed.
388
- */
389
- async function processCert(
390
- config: MedplumInfraConfig,
391
- allCerts: CertificateSummary[],
392
- region: string,
393
- certName: 'api' | 'app' | 'storage'
394
- ): Promise<string> {
395
- const domainName = config[getDomainSetting(certName)];
396
- const existingCert = allCerts.find((cert) => cert.CertificateArn?.includes(region) && cert.DomainName === domainName);
397
- if (existingCert) {
398
- print(`Found existing certificate for "${domainName}" in "${region}.`);
399
- return existingCert.CertificateArn as string;
400
- }
401
-
402
- print(`No existing certificate found for "${domainName}" in "${region}.`);
403
- if (!(await yesOrNo('Do you want to request a new certificate?'))) {
404
- print(`Please add your certificate ARN to the config file in the "${getDomainCertSetting(certName)}" setting.`);
405
- return 'TODO';
406
- }
407
-
408
- const arn = await requestCert(region, domainName);
409
- print('Certificate ARN: ' + arn);
410
- return arn;
411
- }
412
-
413
- /**
414
- * Requests an AWS Certificate.
415
- * @param region The AWS region.
416
- * @param domain The domain name.
417
- * @returns The AWS Certificate ARN on success, or undefined on failure.
418
- */
419
- async function requestCert(region: string, domain: string): Promise<string> {
420
- try {
421
- const validationMethod = await choose(
422
- 'Validate certificate using DNS or email validation?',
423
- ['dns', 'email'],
424
- 'dns'
425
- );
426
- const client = new ACMClient({ region });
427
- const command = new RequestCertificateCommand({
428
- DomainName: domain,
429
- ValidationMethod: validationMethod.toUpperCase(),
430
- });
431
- const response = await client.send(command);
432
- return response.CertificateArn as string;
433
- } catch (err) {
434
- console.log('Error: Unable to request certificate', (err as Error).message);
435
- return 'TODO';
436
- }
437
- }
438
-
439
- /**
440
- * Generates an AWS CloudFront signing key.
441
- *
442
- * Requirements:
443
- *
444
- * 1. It must be an SSH-2 RSA key pair.
445
- * 2. It must be in base64-encoded PEM format.
446
- * 3. It must be a 2048-bit key pair.
447
- *
448
- * See: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-trusted-signers.html#private-content-creating-cloudfront-key-pairs
449
- *
450
- * @returns A new signing key.
451
- */
452
- function generateSigningKey(): { publicKey: string; privateKey: string; passphrase: string } {
453
- const passphrase = randomUUID();
454
- const signingKey = generateKeyPairSync('rsa', {
455
- modulusLength: 2048,
456
- publicKeyEncoding: {
457
- type: 'spki',
458
- format: 'pem',
459
- },
460
- privateKeyEncoding: {
461
- type: 'pkcs1',
462
- format: 'pem',
463
- cipher: 'aes-256-cbc',
464
- passphrase,
465
- },
466
- });
467
- return {
468
- publicKey: signingKey.publicKey,
469
- privateKey: signingKey.privateKey,
470
- passphrase,
471
- };
472
- }
473
-
474
- /**
475
- * Writes a parameter to AWS Parameter Store.
476
- * @param region The AWS region.
477
- * @param key The parameter key.
478
- * @param value The parameter value.
479
- */
480
- async function writeParameter(region: string, key: string, value: string): Promise<void> {
481
- const client = new SSMClient({ region });
482
- const command = new PutParameterCommand({
483
- Name: key,
484
- Value: value,
485
- Type: 'SecureString',
486
- Overwrite: true,
487
- });
488
- await client.send(command);
489
- }
490
-
491
- /**
492
- * Writes a collection of parameters to AWS Parameter Store.
493
- * @param region The AWS region.
494
- * @param prefix The AWS Parameter Store prefix.
495
- * @param params The parameters to write.
496
- */
497
- async function writeParameters(region: string, prefix: string, params: Record<string, string | number>): Promise<void> {
498
- for (const [key, value] of Object.entries(params)) {
499
- const valueStr = value.toString();
500
- if (valueStr) {
501
- await writeParameter(region, prefix + key, valueStr);
502
- }
503
- }
504
- }
505
-
506
- if (require.main === module) {
507
- main(readline.createInterface({ input: process.stdin, output: process.stdout }))
508
- .then(() => process.exit(0))
509
- .catch((err) => {
510
- console.error((err as Error).message);
511
- process.exit(1);
512
- });
513
- }