Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit d6d3e00

Browse files
authored
fix: bundle lambdas in script instead of command line (#655)
1 parent b308d2a commit d6d3e00

File tree

6 files changed

+72
-35
lines changed

6 files changed

+72
-35
lines changed

lib/cdk-infra-stack.ts

+20-12
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ import {
2222
} from 'aws-cdk-lib/aws-apigateway';
2323
import { AttributeType, BillingMode, StreamViewType, Table, TableEncryption } from 'aws-cdk-lib/aws-dynamodb';
2424
import { Rule, Schedule } from 'aws-cdk-lib/aws-events';
25-
import { Effect, PolicyDocument, PolicyStatement, Role, ServicePrincipal, StarPrincipal } from 'aws-cdk-lib/aws-iam';
25+
import {
26+
AnyPrincipal,
27+
Effect,
28+
PolicyDocument,
29+
PolicyStatement,
30+
Role,
31+
ServicePrincipal,
32+
StarPrincipal,
33+
} from 'aws-cdk-lib/aws-iam';
2634
import { Alias } from 'aws-cdk-lib/aws-kms';
2735
import { Runtime, StartingPosition, Tracing } from 'aws-cdk-lib/aws-lambda';
2836
import { DynamoEventSource, SqsEventSource } from 'aws-cdk-lib/aws-lambda-event-sources';
@@ -381,12 +389,10 @@ export default class FhirWorksStack extends Stack {
381389
// copy all the necessary files for the lambda into the bundle
382390
// this allows the lambda functions for bulk export to have access to these files within the lambda instance
383391
return [
384-
`dir ${outputDir}\\bulkExport || mkdir -p ${outputDir}\\bulkExport\\glueScripts`,
385-
`dir ${outputDir}\\bulkExport\\schema || mkdir ${outputDir}\\bulkExport\\schema`,
386-
`cp ${inputDir}\\bulkExport\\glueScripts\\export-script.py ${outputDir}\\bulkExport\\glueScripts\\export-script.py`,
387-
`cp ${inputDir}\\bulkExport\\schema\\transitiveReferenceParams.json ${outputDir}\\bulkExport\\schema\\transitiveReferenceParams.json`,
388-
`cp ${inputDir}\\bulkExport\\schema\\${PATIENT_COMPARTMENT_V3} ${outputDir}\\bulkExport\\schema\\${PATIENT_COMPARTMENT_V3}`,
389-
`cp ${inputDir}\\bulkExport\\schema\\${PATIENT_COMPARTMENT_V4} ${outputDir}\\bulkExport\\schema\\${PATIENT_COMPARTMENT_V4}`,
392+
`node scripts/build_lambda.js ${inputDir} ${outputDir} bulkExport\\glueScripts\\export-script.py`,
393+
`node scripts/build_lambda.js ${inputDir} ${outputDir} bulkExport\\schema\\transitiveReferenceParams.json`,
394+
`node scripts/build_lambda.js ${inputDir} ${outputDir} bulkExport\\schema\\${PATIENT_COMPARTMENT_V3}`,
395+
`node scripts/build_lambda.js ${inputDir} ${outputDir} bulkExport\\schema\\${PATIENT_COMPARTMENT_V4}`,
390396
];
391397
},
392398
},
@@ -516,10 +522,10 @@ export default class FhirWorksStack extends Stack {
516522
effect: Effect.DENY,
517523
actions: ['SQS:*'],
518524
resources: [subscriptionsMatcherDLQ.queueArn],
519-
principals: [new StarPrincipal()],
525+
principals: [new AnyPrincipal()],
520526
conditions: {
521527
Bool: {
522-
'aws:SecureTransport': 'false',
528+
'aws:SecureTransport': false,
523529
},
524530
},
525531
}),
@@ -545,7 +551,9 @@ export default class FhirWorksStack extends Stack {
545551
afterBundling(inputDir, outputDir) {
546552
// copy all the necessary files for the lambda into the bundle
547553
// this allows the validators to be constructed with the compiled implementation guides
548-
return [`cp -r ${inputDir}\\compiledImplementationGuides ${outputDir}`];
554+
return [
555+
`node scripts/build_lambda.js ${inputDir}\\compiledImplementationGuides ${outputDir}\\compiledImplementationGuides none true`,
556+
];
549557
},
550558
},
551559
},
@@ -874,7 +882,7 @@ export default class FhirWorksStack extends Stack {
874882
},
875883
});
876884
new Rule(this, 'subscriptionReaperScheduleEvent', {
877-
schedule: Schedule.cron({ minute: '5' }),
885+
schedule: Schedule.rate(Duration.minutes(5)),
878886
enabled: props!.enableSubscriptions,
879887
}).addTarget(new LambdaFunction(subscriptionReaper));
880888

@@ -926,7 +934,7 @@ export default class FhirWorksStack extends Stack {
926934
'dynamodb:ListStreams',
927935
'dynamodb:GetRecords',
928936
],
929-
resources: [resourceDynamoDbTable.tableArn],
937+
resources: [resourceDynamoDbTable.tableStreamArn!],
930938
}),
931939
new PolicyStatement({
932940
effect: Effect.ALLOW,

lib/subscriptions.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default class SubscriptionsResources {
7777
this.restHookDLQ.addToResourcePolicy(
7878
new PolicyStatement({
7979
effect: Effect.DENY,
80-
actions: ['SQS:*'],
80+
actions: ['sqs:*'],
8181
resources: [this.restHookDLQ.queueArn],
8282
principals: [new StarPrincipal()],
8383
conditions: {
@@ -91,20 +91,20 @@ export default class SubscriptionsResources {
9191
this.restHookQueue.addToResourcePolicy(
9292
new PolicyStatement({
9393
effect: Effect.DENY,
94-
actions: ['SQS:*'],
94+
actions: ['sqs:*'],
9595
resources: [this.restHookQueue.queueArn],
9696
principals: [new StarPrincipal()],
9797
conditions: {
9898
Bool: {
99-
'aws:SecureTransport': 'false',
99+
'aws:SecureTransport': false,
100100
},
101101
},
102102
}),
103103
);
104104
this.restHookQueue.addToResourcePolicy(
105105
new PolicyStatement({
106106
effect: Effect.ALLOW,
107-
actions: ['SQS:SendMessage'],
107+
actions: ['sqs:SendMessage'],
108108
resources: [this.restHookQueue.queueArn],
109109
principals: [new ServicePrincipal('sns.amazonaws.com')],
110110
conditions: {
@@ -136,7 +136,7 @@ export default class SubscriptionsResources {
136136
}),
137137
new PolicyStatement({
138138
effect: Effect.ALLOW,
139-
actions: ['xray:PutTraceSegments', 'scray:PutTelemetryRecords'],
139+
actions: ['xray:PutTraceSegments', 'xray:PutTelemetryRecords'],
140140
resources: ['*'],
141141
}),
142142
new PolicyStatement({

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"devDependencies": {
4141
"@types/chance": "^1.1.1",
4242
"@types/express": "^4.17.2",
43+
"@types/fs-extra": "^9.0.13",
4344
"@types/jest": "^26.0.19",
4445
"@types/jsonwebtoken": "^8.5.4",
4546
"@types/lodash": "^4.14.182",
@@ -87,6 +88,7 @@
8788
"fhir-works-on-aws-persistence-ddb": "3.11.0",
8889
"fhir-works-on-aws-routing": "6.5.0",
8990
"fhir-works-on-aws-search-es": "3.12.0",
91+
"fs-extra": "^10.1.0",
9092
"lodash": "^4.17.21",
9193
"p-settle": "^4.1.1",
9294
"path": "^0.12.7",

scripts/build_lambda.js

+25-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
path = require('path');
2-
esbuild = require('esbuild');
1+
var fs = require('fs');
2+
var fse = require('fs-extra');
3+
var path = require('path');
34

4-
esbuild.build({
5-
entryPoints: [path.join(__dirname, '../../src/index.ts')],
6-
bundle: true,
7-
platform: 'node',
8-
target: 'node14',
9-
external: ['aws-sdk'],
10-
outfile: path.join(__dirname, '../index.js'),
11-
}).catch(() => process.exit(1));
5+
// expected usage: `node build_lambda.js <path> <path> <pathToFile> <fileName>`
6+
// for use with NodeJsFunction command hooks to add files to Lambda functions,
7+
// so <path> <path> will usually be the inputDir and outputDir variables, respectively
8+
var inputDir = process.argv[2];
9+
var outputDir = process.argv[3];
10+
var fileToMove = process.argv[4];
11+
var isDirectory = process.argv.length > 5 ? true : false;
12+
13+
function ensureDirectoryExistence(filePath) {
14+
var dirname = path.dirname(filePath);
15+
if (fs.existsSync(dirname)) {
16+
return true;
17+
}
18+
fs.mkdirSync(dirname, { recursive: true });
19+
}
20+
21+
if (isDirectory) {
22+
fse.copySync(inputDir, outputDir);
23+
} else {
24+
ensureDirectoryExistence(`${outputDir}\\${fileToMove}`);
25+
fs.copyFileSync(`${inputDir}\\${fileToMove}`, `${outputDir}\\${fileToMove}`);
26+
}

src/subscriptions/restHookLambda/restHook.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { SubscriptionNotification } from 'fhir-works-on-aws-search-es';
55
import { metricScope, Unit } from 'aws-embedded-metrics';
66
import https from 'https';
77
import pSettle from 'p-settle';
8-
import { ensureAsyncInit } from '../../index';
98
import { AllowListInfo, getAllowListHeaders } from './allowListUtil';
109

1110
const logger = makeLogger({ component: 'subscriptions' });
@@ -67,8 +66,9 @@ export default class RestHookHandler {
6766
event: SQSEvent,
6867
allowListPromise: Promise<{ [key: string]: AllowListInfo }>,
6968
): Promise<SQSBatchResponse> {
70-
await ensureAsyncInit(allowListPromise);
69+
logger.debug(allowListPromise);
7170
const allowList = await allowListPromise;
71+
logger.debug(allowList);
7272
const messages = event.Records.map((record: any): SubscriptionNotification => {
7373
const body = JSON.parse(record.body);
7474
return JSON.parse(body.Message);

yarn.lock

+18-6
Original file line numberDiff line numberDiff line change
@@ -2829,6 +2829,13 @@
28292829
"@types/qs" "*"
28302830
"@types/serve-static" "*"
28312831

2832+
"@types/fs-extra@^9.0.13":
2833+
version "9.0.13"
2834+
resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.13.tgz#7594fbae04fe7f1918ce8b3d213f74ff44ac1f45"
2835+
integrity sha512-nEnwB++1u5lVDM2UI4c1+5R+FYaKfaAzS4OococimjVm3nQw3TuzH5UNsocrcTBbhnerblyHj4A49qXbIiZdpA==
2836+
dependencies:
2837+
"@types/node" "*"
2838+
28322839
"@types/graceful-fs@^4.1.2":
28332840
version "4.1.5"
28342841
resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.5.tgz#21ffba0d98da4350db64891f92a9e5db3cdb4e15"
@@ -2919,7 +2926,7 @@
29192926
dependencies:
29202927
"@types/node" "*"
29212928

2922-
"@types/node@*", "@types/node@>=13.7.0":
2929+
"@types/node@*":
29232930
version "17.0.38"
29242931
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.38.tgz#f8bb07c371ccb1903f3752872c89f44006132947"
29252932
integrity sha512-5jY9RhV7c0Z4Jy09G+NIDTsCZ5G0L5n+Z+p+Y7t5VJHM30bgwzSjVtlcBxqAj+6L/swIlvtOSzr8rBk/aNyV2g==
@@ -2929,6 +2936,11 @@
29292936
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.0.0.tgz#67c7b724e1bcdd7a8821ce0d5ee184d3b4dd525a"
29302937
integrity sha512-cHlGmko4gWLVI27cGJntjs/Sj8th9aYwplmZFwmmgYQQvL5NUsgVJG7OddLvNfLqYS31KFN0s3qlaD9qCaxACA==
29312938

2939+
"@types/node@^17.0.33":
2940+
version "17.0.45"
2941+
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.45.tgz#2c0fafd78705e7a18b7906b5201a522719dc5190"
2942+
integrity sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw==
2943+
29322944
"@types/normalize-package-data@^2.4.0":
29332945
version "2.4.1"
29342946
resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.1.tgz#d3357479a0fdfdd5907fe67e17e0a85c906e1301"
@@ -3594,7 +3606,7 @@ async-hook-jl@^1.7.6:
35943606
dependencies:
35953607
stack-chain "^1.3.7"
35963608

3597-
async@^2.6.1, async@^2.6.2, async@^3.1.0, async@^3.2.0, async@^3.2.2:
3609+
async@^2.6.1, async@^2.6.2, async@^3.2.2, async@^3.2.3:
35983610
version "3.2.3"
35993611
resolved "https://registry.yarnpkg.com/async/-/async-3.2.3.tgz#ac53dafd3f4720ee9e8a160628f18ea91df196c9"
36003612
integrity sha512-spZRyzKL5l5BZQrr/6m/SqFdBN0q3OCI0f9rjfBzCMBIP4p75P620rR3gTmaksNOhmzgdxcaxdNfMy6anrbM0g==
@@ -9387,10 +9399,10 @@ mkdirp@^0.5.1, mkdirp@^0.5.3:
93879399
dependencies:
93889400
minimist "^1.2.6"
93899401

9390-
moment@^2.14.1, moment@^2.29.1:
9391-
version "2.29.3"
9392-
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.3.tgz#edd47411c322413999f7a5940d526de183c031f3"
9393-
integrity sha512-c6YRvhEo//6T2Jz/vVtYzqBzwvPT95JBQ+smCytzf7c50oMZRsR/a4w88aD34I+/QVSfnoAnSBFPJHItlOMJVw==
9402+
moment@2.29.2, moment@^2.14.1, moment@^2.29.1:
9403+
version "2.29.2"
9404+
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.2.tgz#00910c60b20843bcba52d37d58c628b47b1f20e4"
9405+
integrity sha512-UgzG4rvxYpN15jgCmVJwac49h9ly9NurikMWGPdVxm8GZD6XjkKPxDTjQQ43gtGgnV3X0cAyWDdP2Wexoquifg==
93949406

93959407
moo@^0.5.0:
93969408
version "0.5.1"

0 commit comments

Comments
 (0)