Skip to content

ref(build): Use rollup to build AWS lambda layer #5146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 40 additions & 42 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ env:
${{ github.workspace }}/packages/ember/instance-initializers
${{ github.workspace }}/packages/gatsby/*.d.ts
${{ github.workspace }}/packages/core/src/version.ts
${{ github.workspace }}/dist-serverless
${{ github.workspace }}/packages/serverless
${{ github.workspace }}/packages/utils/cjs
${{ github.workspace }}/packages/utils/esm

Expand Down Expand Up @@ -124,52 +124,50 @@ jobs:
# this file) to a constant and skip rebuilding all of the packages each time CI runs.
if: steps.cache_built_packages.outputs.cache-hit == ''
run: yarn build
- name: Save SDK version for later
run: |
echo "Saving SDK_VERSION for later"
cat packages/core/src/version.ts | awk -F"'" '{print $2}' > dist-serverless/version
[ ! -z $(cat dist-serverless/version) ] && echo SDK_VERSION=$(cat dist-serverless/version) || (echo "Version extraction failed" && exit 1)
outputs:
# this needs to be passed on, because the `needs` context only looks at direct ancestors (so steps which depend on
# `job_build` can't see `job_install_deps` and what it returned)
dependency_cache_key: ${{ needs.job_install_deps.outputs.dependency_cache_key }}

# job_build_aws_lambda_layer:
# name: Build AWS Lambda Layer
# needs: job_build
# runs-on: ubuntu-latest
# steps:
# - name: Check out current commit (${{ env.HEAD_COMMIT }})
# uses: actions/checkout@v2
# with:
# ref: ${{ env.HEAD_COMMIT }}
# - name: Set up Node
# uses: actions/setup-node@v1
# with:
# node-version: ${{ env.DEFAULT_NODE_VERSION }}
# - name: Check dependency cache
# uses: actions/cache@v2
# with:
# path: ${{ env.CACHED_DEPENDENCY_PATHS }}
# key: ${{ needs.job_build.outputs.dependency_cache_key }}
# - name: Check build cache
# uses: actions/cache@v2
# with:
# path: ${{ env.CACHED_BUILD_PATHS }}
# key: ${{ env.BUILD_CACHE_KEY }}
# - name: Get SDK version
# run: |
# export SDK_VERSION=$(cat dist-serverless/version)
# echo "SDK_VERSION=$SDK_VERSION" | tee -a $GITHUB_ENV
# - uses: actions/upload-artifact@v3
# with:
# name: ${{ env.HEAD_COMMIT }}
# path: |
# dist-serverless/*
# - uses: getsentry/action-build-aws-lambda-extension@v1
# with:
# artifact_name: ${{ env.HEAD_COMMIT }}
# zip_file_name: sentry-node-serverless-${{ env.SDK_VERSION }}.zip
job_pack_aws_lambda_layer:
name: Pack and Upload AWS Lambda Layer
needs: [job_get_metadata, job_build]
runs-on: ubuntu-latest
steps:
- name: Check out current commit (${{ needs.job_get_metadata.outputs.commit_label }})
uses: actions/checkout@v2
with:
ref: ${{ env.HEAD_COMMIT }}
- name: Set up Node
uses: actions/setup-node@v1
with:
node-version: ${{ env.DEFAULT_NODE_VERSION }}
- name: Check dependency cache
uses: actions/cache@v2
with:
path: ${{ env.CACHED_DEPENDENCY_PATHS }}
key: ${{ needs.job_build.outputs.dependency_cache_key }}
- name: Check build cache
uses: actions/cache@v2
with:
path: ${{ env.CACHED_BUILD_PATHS }}
key: ${{ env.BUILD_CACHE_KEY }}
- name: Get SDK version
# `jq` reads JSON files, and `tee` pipes its input to the given location and to stdout. (Adding `-a` is the
# equivalent of using >> rather than >.)
run: |
export SDK_VERSION=$(cat packages/core/package.json | jq --raw-output '.version')
echo "SDK_VERSION=$SDK_VERSION" | tee -a $GITHUB_ENV
- name: Move dist-serverless to root directory (requirement for zipping action)
run: |
mv ./packages/serverless/build/aws/dist-serverless .
- name: Create and upload final zip file
uses: getsentry/action-build-aws-lambda-extension@v1
with:
artifact_name: ${{ env.HEAD_COMMIT }}
zip_file_name: sentry-node-serverless-${{ env.SDK_VERSION }}.zip
build_cache_paths: ${{ env.CACHED_BUILD_PATHS }}
build_cache_key: ${{ env.BUILD_CACHE_KEY }}

job_size_check:
name: Size Check
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ scratch/
*.js.map
*.pyc
*.tsbuildinfo
# side effects of running AWS lambda layer zip action locally
dist-serverless/
sentry-node-serverless-*.zip
# transpiled transformers
jest/transformers/*.js
# node tarballs
Expand Down
15 changes: 0 additions & 15 deletions packages/core/.npmignore

This file was deleted.

15 changes: 0 additions & 15 deletions packages/hub/.npmignore

This file was deleted.

15 changes: 0 additions & 15 deletions packages/node/.npmignore

This file was deleted.

14 changes: 14 additions & 0 deletions packages/serverless/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,18 @@ module.exports = {
rules: {
'@sentry-internal/sdk/no-async-await': 'off',
},
overrides: [
{
files: ['scripts/**/*.ts'],
parserOptions: {
project: ['../../tsconfig.dev.json'],
},
},
{
files: ['test/**'],
parserOptions: {
sourceType: 'module',
},
},
],
};
1 change: 0 additions & 1 deletion packages/serverless/.gitignore

This file was deleted.

13 changes: 0 additions & 13 deletions packages/serverless/.npmignore

This file was deleted.

13 changes: 7 additions & 6 deletions packages/serverless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"engines": {
"node": ">=10"
},
"main": "build/cjs/index.js",
"module": "build/esm/index.js",
"types": "build/types/index.d.ts",
"main": "build/npm/cjs/index.js",
"module": "build/npm/esm/index.js",
"types": "build/npm/types/index.d.ts",
"publishConfig": {
"access": "public"
},
Expand All @@ -38,8 +38,9 @@
"read-pkg": "^5.2.0"
},
"scripts": {
"build": "run-p build:rollup build:types && yarn build:extras",
"build:awslambda-layer": "node scripts/build-awslambda-layer.js",
"build": "run-p build:rollup build:types build:bundle && yarn build:extras",
"build:awslambda-layer": "echo 'WARNING: AWS lambda layer build emporarily moved to \\`build:bundle\\`.'",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be refactored in an upcoming PR, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depends on your definition of "upcoming." 😛

But yes, eventually, once I've applied the "wait only as long as you need to" to all of the "extras" steps, then the "extras" build can happen in parallel with the others, and this can move back under that umbrella.

"build:bundle": "yarn ts-node scripts/buildLambdaLayer.ts",
"build:dev": "run-p build:rollup build:types",
"build:extras": "yarn build:awslambda-layer",
"build:rollup": "rollup -c rollup.npm.config.js",
Expand All @@ -48,7 +49,7 @@
"build:dev:watch": "run-s build:watch",
"build:rollup:watch": "rollup -c rollup.npm.config.js --watch",
"build:types:watch": "tsc -p tsconfig.types.json --watch",
"build:npm": "ts-node ../../scripts/prepack.ts && npm pack ./build",
"build:npm": "ts-node ../../scripts/prepack.ts --bundles && npm pack ./build/npm",
"circularDepCheck": "madge --circular src/index.ts",
"clean": "rimraf build dist-awslambda-layer coverage sentry-serverless-*.tgz",
"fix": "run-s fix:eslint fix:prettier",
Expand Down
44 changes: 44 additions & 0 deletions packages/serverless/rollup.aws.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { makeBaseBundleConfig, makeBundleConfigVariants, makeBaseNPMConfig } from '../../rollup/index.js';

export default [
// The SDK
...makeBundleConfigVariants(
makeBaseBundleConfig({
// this automatically sets it to be CJS
bundleType: 'node',
entrypoints: ['src/index.awslambda.ts'],
jsVersion: 'es6',
licenseTitle: '@sentry/serverless',
outputFileBase: () => 'index',
packageSpecificConfig: {
output: {
dir: 'build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/build/npm/cjs',
sourcemap: false,
},
},
}),
// 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
// plus its dependencies can be, and we might as well take up as little of that space as is necessary. We'll rename
// it to be `index.js` in the build script, since it's standing in for the index file of the npm package.
{ variants: ['.min.js'] },
),

// This builds a wrapper file, which our lambda layer integration automatically sets up to run as soon as node
// launches (via the `NODE_OPTIONS="-r @sentry/serverless/dist/awslambda-auto"` variable). Note the inclusion in this
// path of the legacy `dist` folder; for backwards compatibility, in the build script we'll copy the file there.
makeBaseNPMConfig({
entrypoints: ['src/awslambda-auto.ts'],
packageSpecificConfig: {
// Normally `makeNPMConfigVariants` sets both of these values for us, but we don't actually want the ESM variant,
// and the directory structure is different than normal, so we have to do it ourselves.
output: {
format: 'cjs',
dir: 'build/aws/dist-serverless/nodejs/node_modules/@sentry/serverless/build/npm/cjs',
sourcemap: false,
},
// We only want `awslambda-auto.js`, not the modules that it imports, because they're all included in the bundle
// we generate above
external: ['./index'],
},
}),
];
5 changes: 5 additions & 0 deletions packages/serverless/rollup.npm.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js'

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