-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.mjs
executable file
·382 lines (319 loc) · 12.7 KB
/
deploy.mjs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env node
import path from 'path'
import fs from 'fs'
import { execSync } from 'child_process'
import AdmZip from 'adm-zip'
// Only packages not compatible with Bun directly
import * as AWS from '@aws-sdk/client-lambda'
import superagent from 'superagent'
import { Option, program } from 'commander'
import inquirer from 'inquirer'
import chalk from 'chalk'
import { parse, stringify } from 'yaml'
import equal from 'deep-equal'
import logSymbols from './logSymbols.mjs'
program
.option('--init', 'Begin the setup to deploy to Lambda.')
.option('--deploy <src>', 'Deploy to AWS')
.option('--build <src>', 'Build for AWS')
.option('-o, --out <dest>', 'Where to output the build zip.')
.option('-r, --region <region>', 'AWS region to deploy to.')
.option('--role <role>', 'AWS role to attach to the lambda.')
.option('--name <name>', 'Name of the lambda.')
.option('--layers <layers...>', 'AWS layers to attach to the lambda.')
.option('-c , --config <config>', 'Path to a yaml/json config file.')
.option('--description <desc>', 'Description of the lambda.')
.option('--memory <MiB>', 'Memory to allocate to the lambda, in MiB.', '128')
.addOption(new Option('--arch <architecture>', 'AWS architecture to deploy to.',).choices(['arm64', 'x86_64']))
program.parse(process.argv)
const options = program.opts()
if (options.init) {
console.log(chalk.blue('Welcome to the Elysia Lambda Deployer!'))
console.log(chalk.blue('This will walk you through the setup to deploy to AWS Lambda.'))
let layer = ''
// Ask for the region
const { region } = await inquirer.prompt({
type: 'input',
name: 'region',
message: 'What region do you want to deploy to?',
default: 'us-east-1'
})
// Ask for the architecture
const { architecture } = await inquirer.prompt({
type: 'list',
name: 'architecture',
message: 'What architecture do you want to deploy to?',
choices: ['arm64', 'x86_64'],
default: 'x86_64'
})
// Yes / No; have you created the bun layer yet?
const { bunLayer } = await inquirer.prompt({
type: 'confirm',
name: 'bunLayer',
message: 'Have you created the Bun layer yet?',
default: false
})
if (!bunLayer) {
console.log(chalk.blue('Ok! We will create that layer together.'))
// Ask for which version of Bun to use, set a User-Agent header
const { body } = await superagent.get('https://api.github.com/repos/oven-sh/bun/tags')
.set('User-Agent', 'elysia-lambda-deployer')
const versions = body.map(tag => tag.name).filter(i => i.startsWith('bun-v')).map(i => i.substring(5))
// order the versions
// this is copilot code
versions.sort((a, b) => {
const aParts = a.split('.')
const bParts = b.split('.')
for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) {
if (aParts[i] === undefined) return -1
if (bParts[i] === undefined) return 1
if (parseInt(aParts[i]) > parseInt(bParts[i])) return -1
if (parseInt(aParts[i]) < parseInt(bParts[i])) return 1
}
return 0
})
const { bunVersion } = await inquirer.prompt({
type: 'list',
name: 'bunVersion',
message: 'What version of Bun do you want to use?',
choices: ['latest', 'canary', ...versions],
default: 'latest'
})
execSync('git clone https://github.com/oven-sh/bun')
const output = execSync(`cd bun/packages/bun-lambda && bun install && bun run publish-layer --arch ${architecture === 'x86_64' ? 'x64' : 'aarch64'} --region ${region} --release ${bunVersion}`)
// use a regex to get the arn of the layer, starts with "arn:aws:lambda:" all the way to the next whitespace character
layer = output.toString().replace(/\r?\n/g, ' ').match(/arn:aws:lambda:[^\s]+/)[0]
console.log(chalk.blue(`Created the layer with ARN ${layer}`))
// delete the bun folder
fs.rm('bun', { recursive: true }, () => {})
}
else {
// Ask for the ARN of the layer
layer = (await inquirer.prompt({
type: 'input',
name: 'layerArn',
message: 'What is the ARN of the Bun layer?',
})).layerArn
}
// Ask for the ARN of the role
const { roleArn } = await inquirer.prompt({
type: 'input',
name: 'roleArn',
message: 'What is the ARN of the role you want to use?',
})
// Ask for the name
const { name } = await inquirer.prompt({
type: 'input',
name: 'name',
message: 'What do you want to name the lambda?',
default: 'elysia-lambda'
})
// Ask for the memory
const { memory } = await inquirer.prompt({
type: 'input',
name: 'memory',
message: 'How much memory do you want to allocate to the lambda, in MiB?',
default: '128'
})
// Ask for the description
const { description } = await inquirer.prompt({
type: 'input',
name: 'description',
message: 'What do you want the description to be?',
default: 'Elysia Lambda'
})
// Ask for the entry point
const { entry } = await inquirer.prompt({
type: 'input',
name: 'entry',
message: 'What is the entry point?',
default: 'index.js'
})
// Ask to name the yaml file
const { yamlName } = await inquirer.prompt({
type: 'input',
name: 'yamlName',
message: 'What do you want to name the yaml file?',
default: 'elysia-lambda.yaml'
})
// Output the configuration
fs.writeFileSync(
yamlName,
stringify({
deploy: entry,
name,
region,
memory,
arch: architecture,
description,
role: roleArn,
layers: [layer]
})
)
try {
const pkg = JSON.parse(fs.readFileSync('package.json'))
// if there is a "deploy" script in the package,
if (!pkg.scripts.deploy) {
// ask if they want to add a deploy script
const { addDeployScript } = await inquirer.prompt({
type: 'confirm',
name: 'addDeployScript',
message: 'Do you want to add a deploy script to your package.json?',
default: true
})
if (addDeployScript) {
pkg.scripts.deploy = `elysia-lambda --config ${yamlName}`
fs.writeFileSync('package.json', JSON.stringify(pkg, null, 2))
}
else {
console.log(logSymbols.success, chalk.green(`Run: elysia-lambda --config ${yamlName}`))
}
}
}
catch (err) {
console.log(logSymbols.success, chalk.green(`Run: elysia-lambda --config ${yamlName}`))
}
process.exit(0)
}
if (options.config) Object.assign(options, parse(fs.readFileSync(path.resolve(process.cwd(), options.config), 'utf8')))
if (!options.deploy && !options.build) {
console.error(chalk.red('No command specified. Use --deploy or --build.'))
process.exit(1)
}
if (options.deploy && options.build) {
console.error(chalk.red('Cannot use --deploy and --build together.'))
process.exit(1)
}
if (options.build && !options.out) {
console.error(chalk.red('Must specify --out when using --build.'))
process.exit(1)
}
if (options.deploy) {
let err = ''
if (!options.role) err += '- Must specify --role for the lambda when using --deploy.\n'
if (!options.name) err += '- Must specify --name for the lambda when using --deploy.\n'
if (!options.region) err += '- Must specify --region for the lambda when using --deploy.\n'
if (options.layer) options.layers = options.layer
if (!options.layers) err += '- Must specify at least one layer for the lambda with --layers when using --deploy.\n'
if (typeof options.layers === 'string') options.layers = [options.layers]
if (!options.arch) err += '- Must specify --arch for the lambda when using --deploy.\n'
if (options.arch === 'x64') options.arch = 'x86_64' // minor correction from previous configs.
if (err) {
console.error(chalk.red(`Some deployment issues were encountered:\n${err}`))
process.exit(1)
}
}
const file = path.resolve(process.cwd(), options.build || options.deploy)
const now = new Date().getTime()
const mutatedFile = path.resolve(process.cwd(), options.build || options.deploy, '../', now + '.ts')
fs.writeFileSync(mutatedFile, fs.readFileSync(file, 'utf8').replace(/([, (])lambda([,() ])/g, i => i[0] + 'hijack' + i[i.length - 1]))
const script = `
import { instance } from 'elysia-lambda'
import '${mutatedFile}' // the entry point import
export default {
js: instance().innerHandle || instance().fetch
}`
const out = path.resolve(process.cwd(), now + '.js')
fs.writeFileSync(out, script)
const bundleFile = path.resolve(process.cwd(), 'bundle.' + now + '.js')
execSync(`bun build --minify --outfile=${bundleFile} --external @elysiajs/fn --target=bun ${out}`)
fs.unlinkSync(out)
fs.unlinkSync(mutatedFile)
const zip = new AdmZip()
zip.addLocalFile(bundleFile, '', 'index.js')
fs.unlinkSync(bundleFile)
const timeout = async (ms) => new Promise(resolve => setTimeout(resolve, ms))
const sizes = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi']
/**
* Performs the deployment to AWS.
*/
async function deploy () {
zip.writeZip(`bundle.${now}.zip`)
// Get size of zip
const stats = fs.statSync(`bundle.${now}.zip`)
let size = stats.size
let div = 0
while (size > 1024) {
size /= 1024
div++
}
const lambda = new AWS.Lambda({ region: 'us-east-1' })
// check if lambda exists
const exists = await lambda.getFunction({
FunctionName: options.name
}).catch(() => false)
if (!exists) {
console.log(chalk.magenta(`Deploying "${options.name}"!`))
await lambda.createFunction({
FunctionName: options.name,
Runtime: 'provided.al2',
Handler: 'index.js',
Layers: options.layers,
Description: options.description || 'Elysia Lambda',
Code: {
ZipFile: fs.readFileSync(`bundle.${now}.zip`)
},
Role: options.role,
Architectures: [options.arch],
...(options.environment && {
Environment: {
Variables: options.environment
}
}),
MemorySize: +options.memory || 128,
})
}
else {
console.log(chalk.magenta(`Lambda "${options.name}" found. Updating code.`))
const result = await lambda.updateFunctionCode({
FunctionName: options.name,
ZipFile: fs.readFileSync(`bundle.${now}.zip`),
})
// check if anything is different
if (!equal({
Architectures: [options.arch],
Handler: 'index.js',
Role: options.role,
MemorySize: +options.memory || 128,
Layers: options.layers,
...(options.environment && {
Environment: {
Variables: options.environment
}
})
}, {
Architectures: result.Architectures,
Handler: result.Handler,
Role: result.Role,
MemorySize: result.MemorySize,
Layers: result.Layers.map(i => i.Arn),
...(result.Environment && {Environment: result.Environment}),
})) {
console.log(chalk.magenta(`Changes detected. Updating configuration.`))
// I tried to use AWS.waitUntilFunctionUpdatedV2, but it would pause indefinitely.
await timeout(2000)
await lambda.updateFunctionConfiguration({
FunctionName: options.name,
Layers: options.layers,
Description: options.description || 'Elysia Lambda',
Role: options.role,
Architectures: [options.arch],
Handler: 'index.js',
...(options.environment && {
Environment: {
Variables: options.environment
}
}),
MemorySize: +options.memory || 128,
})
}
}
console.log(chalk.magenta('Bundle Size: ', chalk.yellow(size.toFixed(2) + sizes[div] + 'B')))
console.log(logSymbols.success, chalk.green('Success!'))
fs.unlinkSync(`bundle.${now}.zip`)
}
if (options.deploy) await deploy()
if (options.build) {
zip.writeZip(options.out)
console.log(logSymbols.success, chalk.green('Zip for Lambda built!'))
}