Skip to content

Commit e76c247

Browse files
lobsterkatieAbhiPrasad
authored andcommitted
ref(build): Use rollup to build AWS lambda layer (#5146)
Our current system for building the AWS lambda layer involves a script which manually traces the serverless package's dependencies, including other monorepo packages, and symlinks them into a `node_modules` folder in a directory set up to mimic an installation from npm. There are a few disadvantages to this system: - The signals we rely on to figure out what is and isn't a dependency aren't exhaustive, and we're not experts at this, both of which have made getting the right files and only the right files into the layer a brittle process, which has broken down more than once and caused issue for our users. - The script is complicated, which makes it harder to figure out exactly what's causing it when something does go wrong. - We symlink in entire packages at a time, regardless of whether or not we're using most of the code. This is true even of the serverless package itself, where we include all of the GCP code along with the AWS code. This refactors our lambda layer build process to use the new bundling config functions we use for CDN bundles, to create a bundle which can take the place of the the index file, but which contains everything it needs internally. This has the following advantages: - This puts Rollup in charge of figuring out dependencies instead of us. - The config is in line with existing configs and is therefore much easier to reason about and maintain. - It lets us easily exclude anything GCP-related in the SDK. Between that, Rollup's treeshaking, and terser's minifying, the layer will now take up much less of the finite size allotted to each lambda function. - Removing extraneous files means less to cache and retrieve from the cache in each GHA job. Key changes: - The layer now builds in `packages/serverless/build/aws` rather than at the top level of the repo. (It is now moved to the top level only in our GHA workflow creating the lambda zip.) - In that workflow, the process to determine the SDK version has been simplified. - The bundle builds based not on the main index file but on a new bundle-specific index file, which only includes AWS code. - There is new rollup config just for the layer, which uses the bundle-building functions to make the main bundle and the npm-package-building functions to create a separate module for the optional script which automatically starts up the SDK in AWS. - The old build script has been replaced by one which runs rollup with that config, and then symlinks the built auto-startup script into its legacy location, so as to be backwards compatible with folks who run it using an environment variable pointing to the old path. - The building of the layer has temporarily been shifted from `build:awslambda-layer` to `build:bundle`, so that it will run during the first phase of the repo-level `yarn build`. (The goal is to eventually do everything in one phase, for greater parallelization and shorter overall build time.) h/t to @antonpirker for all his help testing and thinking through this with me.
1 parent 9b23ef3 commit e76c247

20 files changed

+258
-346
lines changed

.github/workflows/build.yml

+40-42
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ env:
3131
${{ github.workspace }}/packages/ember/instance-initializers
3232
${{ github.workspace }}/packages/gatsby/*.d.ts
3333
${{ github.workspace }}/packages/core/src/version.ts
34-
${{ github.workspace }}/dist-serverless
34+
${{ github.workspace }}/packages/serverless
3535
${{ github.workspace }}/packages/utils/cjs
3636
${{ github.workspace }}/packages/utils/esm
3737
@@ -124,52 +124,50 @@ jobs:
124124
# this file) to a constant and skip rebuilding all of the packages each time CI runs.
125125
if: steps.cache_built_packages.outputs.cache-hit == ''
126126
run: yarn build
127-
- name: Save SDK version for later
128-
run: |
129-
echo "Saving SDK_VERSION for later"
130-
cat packages/core/src/version.ts | awk -F"'" '{print $2}' > dist-serverless/version
131-
[ ! -z $(cat dist-serverless/version) ] && echo SDK_VERSION=$(cat dist-serverless/version) || (echo "Version extraction failed" && exit 1)
132127
outputs:
133128
# this needs to be passed on, because the `needs` context only looks at direct ancestors (so steps which depend on
134129
# `job_build` can't see `job_install_deps` and what it returned)
135130
dependency_cache_key: ${{ needs.job_install_deps.outputs.dependency_cache_key }}
136131

137-
# job_build_aws_lambda_layer:
138-
# name: Build AWS Lambda Layer
139-
# needs: job_build
140-
# runs-on: ubuntu-latest
141-
# steps:
142-
# - name: Check out current commit (${{ env.HEAD_COMMIT }})
143-
# uses: actions/checkout@v2
144-
# with:
145-
# ref: ${{ env.HEAD_COMMIT }}
146-
# - name: Set up Node
147-
# uses: actions/setup-node@v1
148-
# with:
149-
# node-version: ${{ env.DEFAULT_NODE_VERSION }}
150-
# - name: Check dependency cache
151-
# uses: actions/cache@v2
152-
# with:
153-
# path: ${{ env.CACHED_DEPENDENCY_PATHS }}
154-
# key: ${{ needs.job_build.outputs.dependency_cache_key }}
155-
# - name: Check build cache
156-
# uses: actions/cache@v2
157-
# with:
158-
# path: ${{ env.CACHED_BUILD_PATHS }}
159-
# key: ${{ env.BUILD_CACHE_KEY }}
160-
# - name: Get SDK version
161-
# run: |
162-
# export SDK_VERSION=$(cat dist-serverless/version)
163-
# echo "SDK_VERSION=$SDK_VERSION" | tee -a $GITHUB_ENV
164-
# - uses: actions/upload-artifact@v3
165-
# with:
166-
# name: ${{ env.HEAD_COMMIT }}
167-
# path: |
168-
# dist-serverless/*
169-
# - uses: getsentry/action-build-aws-lambda-extension@v1
170-
# with:
171-
# artifact_name: ${{ env.HEAD_COMMIT }}
172-
# zip_file_name: sentry-node-serverless-${{ env.SDK_VERSION }}.zip
132+
job_pack_aws_lambda_layer:
133+
name: Pack and Upload AWS Lambda Layer
134+
needs: [job_get_metadata, job_build]
135+
runs-on: ubuntu-latest
136+
steps:
137+
- name: Check out current commit (${{ needs.job_get_metadata.outputs.commit_label }})
138+
uses: actions/checkout@v2
139+
with:
140+
ref: ${{ env.HEAD_COMMIT }}
141+
- name: Set up Node
142+
uses: actions/setup-node@v1
143+
with:
144+
node-version: ${{ env.DEFAULT_NODE_VERSION }}
145+
- name: Check dependency cache
146+
uses: actions/cache@v2
147+
with:
148+
path: ${{ env.CACHED_DEPENDENCY_PATHS }}
149+
key: ${{ needs.job_build.outputs.dependency_cache_key }}
150+
- name: Check build cache
151+
uses: actions/cache@v2
152+
with:
153+
path: ${{ env.CACHED_BUILD_PATHS }}
154+
key: ${{ env.BUILD_CACHE_KEY }}
155+
- name: Get SDK version
156+
# `jq` reads JSON files, and `tee` pipes its input to the given location and to stdout. (Adding `-a` is the
157+
# equivalent of using >> rather than >.)
158+
run: |
159+
export SDK_VERSION=$(cat packages/core/package.json | jq --raw-output '.version')
160+
echo "SDK_VERSION=$SDK_VERSION" | tee -a $GITHUB_ENV
161+
- name: Move dist-serverless to root directory (requirement for zipping action)
162+
run: |
163+
mv ./packages/serverless/build/aws/dist-serverless .
164+
- name: Create and upload final zip file
165+
uses: getsentry/action-build-aws-lambda-extension@v1
166+
with:
167+
artifact_name: ${{ env.HEAD_COMMIT }}
168+
zip_file_name: sentry-node-serverless-${{ env.SDK_VERSION }}.zip
169+
build_cache_paths: ${{ env.CACHED_BUILD_PATHS }}
170+
build_cache_key: ${{ env.BUILD_CACHE_KEY }}
173171

174172
job_size_check:
175173
name: Size Check

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ scratch/
1414
*.js.map
1515
*.pyc
1616
*.tsbuildinfo
17+
# side effects of running AWS lambda layer zip action locally
18+
dist-serverless/
19+
sentry-node-serverless-*.zip
1720
# transpiled transformers
1821
jest/transformers/*.js
1922
# node tarballs

packages/core/.npmignore

-15
This file was deleted.

packages/hub/.npmignore

-15
This file was deleted.

packages/node/.npmignore

-15
This file was deleted.

packages/serverless/.eslintrc.js

+14
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
};

packages/serverless/.gitignore

-1
This file was deleted.

packages/serverless/.npmignore

-13
This file was deleted.

packages/serverless/package.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
"engines": {
1010
"node": ">=10"
1111
},
12-
"main": "build/cjs/index.js",
13-
"module": "build/esm/index.js",
14-
"types": "build/types/index.d.ts",
12+
"main": "build/npm/cjs/index.js",
13+
"module": "build/npm/esm/index.js",
14+
"types": "build/npm/types/index.d.ts",
1515
"publishConfig": {
1616
"access": "public"
1717
},
@@ -38,8 +38,9 @@
3838
"read-pkg": "^5.2.0"
3939
},
4040
"scripts": {
41-
"build": "run-p build:rollup build:types && yarn build:extras",
42-
"build:awslambda-layer": "node scripts/build-awslambda-layer.js",
41+
"build": "run-p build:rollup build:types build:bundle && yarn build:extras",
42+
"build:awslambda-layer": "echo 'WARNING: AWS lambda layer build emporarily moved to \\`build:bundle\\`.'",
43+
"build:bundle": "yarn ts-node scripts/buildLambdaLayer.ts",
4344
"build:dev": "run-p build:rollup build:types",
4445
"build:extras": "yarn build:awslambda-layer",
4546
"build:rollup": "rollup -c rollup.npm.config.js",
@@ -48,7 +49,7 @@
4849
"build:dev:watch": "run-s build:watch",
4950
"build:rollup:watch": "rollup -c rollup.npm.config.js --watch",
5051
"build:types:watch": "tsc -p tsconfig.types.json --watch",
51-
"build:npm": "ts-node ../../scripts/prepack.ts && npm pack ./build",
52+
"build:npm": "ts-node ../../scripts/prepack.ts --bundles && npm pack ./build/npm",
5253
"circularDepCheck": "madge --circular src/index.ts",
5354
"clean": "rimraf build dist-awslambda-layer coverage sentry-serverless-*.tgz",
5455
"fix": "run-s fix:eslint fix:prettier",
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { makeBaseBundleConfig, makeBundleConfigVariants, makeBaseNPMConfig } from '../../rollup/index.js';
2+
3+
export default [
4+
// The SDK
5+
...makeBundleConfigVariants(
6+
makeBaseBundleConfig({
7+
// this automatically sets it to be CJS
8+
bundleType: 'node',
9+
entrypoints: ['src/index.awslambda.ts'],
10+
jsVersion: 'es6',
11+
licenseTitle: '@sentry/serverless',
12+
outputFileBase: () => 'index',
13+
packageSpecificConfig: {
14+
output: {
15+
dir: 'build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/build/npm/cjs',
16+
sourcemap: false,
17+
},
18+
},
19+
}),
20+
// We only need one copy of the SDK, and we pick the minified one because there's a cap on how big a lambda function
21+
// plus its dependencies can be, and we might as well take up as little of that space as is necessary. We'll rename
22+
// it to be `index.js` in the build script, since it's standing in for the index file of the npm package.
23+
{ variants: ['.min.js'] },
24+
),
25+
26+
// This builds a wrapper file, which our lambda layer integration automatically sets up to run as soon as node
27+
// launches (via the `NODE_OPTIONS="-r @sentry/serverless/dist/awslambda-auto"` variable). Note the inclusion in this
28+
// path of the legacy `dist` folder; for backwards compatibility, in the build script we'll copy the file there.
29+
makeBaseNPMConfig({
30+
entrypoints: ['src/awslambda-auto.ts'],
31+
packageSpecificConfig: {
32+
// Normally `makeNPMConfigVariants` sets both of these values for us, but we don't actually want the ESM variant,
33+
// and the directory structure is different than normal, so we have to do it ourselves.
34+
output: {
35+
format: 'cjs',
36+
dir: 'build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/build/npm/cjs',
37+
sourcemap: false,
38+
},
39+
// We only want `awslambda-auto.js`, not the modules that it imports, because they're all included in the bundle
40+
// we generate above
41+
external: ['./index'],
42+
},
43+
}),
44+
];

packages/serverless/rollup.npm.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js'
22

33
export default makeNPMConfigVariants(
44
makeBaseNPMConfig({
5+
// TODO: `awslambda-auto.ts` is a file which the lambda layer uses to automatically init the SDK. Does it need to be
6+
// in the npm package? Is it possible that some people are using it themselves in the same way the layer uses it (in
7+
// which case removing it would be a breaking change)? Should it stay here or not?
58
entrypoints: ['src/index.ts', 'src/awslambda-auto.ts'],
9+
// packages with bundles have a different build directory structure
10+
hasBundles: true,
611
}),
712
);

0 commit comments

Comments
 (0)