Skip to content

Commit 47d44f1

Browse files
committed
add build script for lambda layer
1 parent 6793dee commit 47d44f1

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

packages/serverless/.eslintrc.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,18 @@ module.exports = {
66
rules: {
77
'@sentry-internal/sdk/no-async-await': 'off',
88
},
9+
overrides: [
10+
{
11+
files: ['scripts/**/*.ts'],
12+
parserOptions: {
13+
project: ['../../tsconfig.dev.json'],
14+
},
15+
},
16+
{
17+
files: ['test/**'],
18+
parserOptions: {
19+
sourceType: 'module',
20+
},
21+
},
22+
],
923
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* eslint-disable no-console */
2+
import * as childProcess from 'child_process';
3+
import * as fs from 'fs';
4+
import * as rimraf from 'rimraf';
5+
6+
import { ensureBundleBuildPrereqs } from '../../../scripts/ensure-bundle-deps';
7+
8+
/**
9+
* Run the given shell command, piping the shell process's `stdin`, `stdout`, and `stderr` to that of the current
10+
* process. Returns contents of `stdout`.
11+
*/
12+
function run(cmd: string, options?: childProcess.ExecSyncOptions): string {
13+
return String(childProcess.execSync(cmd, { stdio: 'inherit', ...options }));
14+
}
15+
16+
async function buildLambdaLayer(): Promise<void> {
17+
// Create the main SDK bundle
18+
await ensureBundleBuildPrereqs({
19+
dependencies: ['@sentry/utils', '@sentry/hub', '@sentry/core', '@sentry/tracing', '@sentry/node'],
20+
});
21+
run('yarn rollup --config rollup.aws.config.js');
22+
23+
// We build a minified bundle, but it's standing in for the regular `index.js` file listed in `package.json`'s `main`
24+
// property, so we have to rename it so it's findable.
25+
fs.renameSync(
26+
'build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/build/npm/cjs/index.min.js',
27+
'build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/build/npm/cjs/index.js',
28+
);
29+
30+
// We're creating a bundle for the SDK, but still using it in a Node context, so we need to copy in `package.json`,
31+
// purely for its `main` property.
32+
console.log('Copying `package.json` into lambda layer.');
33+
fs.copyFileSync('package.json', 'build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/package.json');
34+
35+
// The layer also includes `awslambda-auto.js`, a helper file which calls `Sentry.init()` and wraps the lambda
36+
// handler. It gets run when Node is launched inside the lambda, using the environment variable
37+
//
38+
// `NODE_OPTIONS="-r @sentry/serverless/dist/awslambda-auto"`.
39+
//
40+
// (The`-r` is what runs the script on startup.) The `dist` directory is no longer where we emit our built code, so
41+
// for backwards compatibility, we create a symlink.
42+
console.log('Creating symlink for `awslambda-auto.js` in legacy `dist` directory.');
43+
fsForceMkdirSync('build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/dist');
44+
fs.symlinkSync(
45+
'../build/npm/cjs/awslambda-auto.js',
46+
'build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/dist/awslambda-auto.js',
47+
);
48+
}
49+
50+
void buildLambdaLayer();
51+
52+
/**
53+
* Make a directory synchronously, overwriting the old directory if necessary.
54+
*
55+
* This is what `fs.mkdirSync(path, { force: true })` would be, if it existed. Primarily useful for local building and
56+
* testing, where scripts are often run more than once (and so the directory you're trying to create may already be
57+
* there), but also harmless when used in CI.
58+
*/
59+
function fsForceMkdirSync(path: string): void {
60+
rimraf.sync(path);
61+
fs.mkdirSync(path);
62+
}

scripts/ensure-bundle-deps.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import * as util from 'util';
77
/**
88
* Ensure that `build:bundle` has all of the dependencies it needs to run. Works at both the repo and package level.
99
*/
10-
async function ensureBundleBuildPrereqs(options: { dependencies: string[]; maxRetries?: number }): Promise<void> {
10+
export async function ensureBundleBuildPrereqs(options: {
11+
dependencies: string[];
12+
maxRetries?: number;
13+
}): Promise<void> {
1114
const { maxRetries = 12, dependencies } = options;
1215

1316
const {

0 commit comments

Comments
 (0)