This repository has been archived by the owner on Feb 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepareResources.ts
110 lines (104 loc) · 2.61 KB
/
prepareResources.ts
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
import * as path from 'path'
import { promises as fs } from 'fs'
import { getLambdaSourceCodeBucketName } from './getLambdaSourceCodeBucketName'
import { TestStackLambdas } from './TestStack'
import {
packBaseLayer,
packLayeredLambdas,
WebpackMode,
LayeredLambdas,
} from '../src'
import { spawn } from 'child_process'
export const prepareResources = async ({
stackName,
rootDir,
}: {
stackName: string
rootDir: string
}): Promise<{
sourceCodeBucketName: string
baseLayerZipFileName: string
lambdas: LayeredLambdas<TestStackLambdas>
}> => {
// Prepare the output directory
const outDir = path.resolve(rootDir, 'dist', 'lambdas')
try {
await fs.stat(outDir)
} catch (_) {
await fs.mkdir(outDir)
}
const sourceCodeBucketName = await getLambdaSourceCodeBucketName({
stackName,
})
// Pack the baselayer, only with needed dependencies
// - This will contain the package.json to be used for the layer
const layerFolder = path.resolve(
rootDir,
'dist',
'lambdas',
'cloudFormationLayer',
)
try {
await fs.stat(layerFolder)
} catch (_) {
await fs.mkdir(layerFolder)
}
// - Pick relevant dependencies from the project's package.json
// so it they have the right version
const { dependencies } = JSON.parse(
await fs.readFile(path.resolve(rootDir, 'package.json'), 'utf-8'),
)
const cdkLambdaDeps = {
'@aws-sdk/client-s3': dependencies['@aws-sdk/client-s3'],
}
if (Object.values(cdkLambdaDeps).find((v) => v === undefined) !== undefined) {
throw new Error(
`Could not resolve all dependencies in "${JSON.stringify(
cdkLambdaDeps,
)}"`!,
)
}
// - add them to the layers package.json
await fs.writeFile(
path.join(layerFolder, 'package.json'),
JSON.stringify({
dependencies: cdkLambdaDeps,
}),
'utf-8',
)
// - install them
await new Promise<void>((resolve, reject) => {
const p = spawn('npm', ['i', '--ignore-scripts', '--only=prod'], {
cwd: layerFolder,
})
p.on('close', (code) => {
if (code !== 0) {
const msg = `[CloudFormation Layer] npm i in ${layerFolder} exited with code ${code}.`
return reject(new Error(msg))
}
return resolve()
})
})
const baseLayerZipFileName = await packBaseLayer({
srcDir: layerFolder,
outDir,
Bucket: sourceCodeBucketName,
})
// Pack the lambda
const lambdas = await packLayeredLambdas<TestStackLambdas>({
id: 'test-lambdas',
mode: WebpackMode.production,
srcDir: rootDir,
outDir,
Bucket: sourceCodeBucketName,
lambdas: {
uuid: path.resolve(rootDir, 'test', 'uuidLambda.ts'),
},
tsConfig: path.resolve(rootDir, 'tsconfig.json'),
})
return {
sourceCodeBucketName,
baseLayerZipFileName,
lambdas,
}
}