Skip to content

Commit 2bc61b0

Browse files
authored
ref(build): Reenable lambda layer release in craft (#5207)
1 parent 1cfcb0d commit 2bc61b0

File tree

7 files changed

+83
-46
lines changed

7 files changed

+83
-46
lines changed

.craft.yml

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,16 @@ minVersion: '0.23.1'
22
changelogPolicy: simple
33
preReleaseCommand: bash scripts/craft-pre-release.sh
44
targets:
5-
#
6-
# Deactivated for now. This needs to be reactivated if the new Sentry Lambda Extension is deployed to production.
7-
# (ask Anton Pirker if you have questions.)
8-
#
9-
# - name: aws-lambda-layer
10-
# includeNames: /^sentry-node-serverless-\d+.\d+.\d+(-(beta|alpha)\.\d+)?\.zip$/
11-
# layerName: SentryNodeServerlessSDK
12-
# compatibleRuntimes:
13-
# - name: node
14-
# versions:
15-
# - nodejs10.x
16-
# - nodejs12.x
17-
# - nodejs14.x
18-
# license: MIT
5+
- name: aws-lambda-layer
6+
includeNames: /^sentry-node-serverless-\d+.\d+.\d+(-(beta|alpha)\.\d+)?\.zip$/
7+
layerName: SentryNodeServerlessSDK
8+
compatibleRuntimes:
9+
- name: node
10+
versions:
11+
- nodejs10.x
12+
- nodejs12.x
13+
- nodejs14.x
14+
license: MIT
1915
- name: gcs
2016
includeNames: /.*\.js.*$/
2117
bucket: sentry-js-sdk

packages/serverless/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ Another and much simpler way to integrate Sentry to your AWS Lambda function is
6363
- `SENTRY_DSN`: `your dsn`.
6464
- `SENTRY_TRACES_SAMPLE_RATE`: a number between 0 and 1 representing the chance a transaction is sent to Sentry. For more information, see [docs](https://docs.sentry.io/platforms/node/guides/aws-lambda/configuration/options/#tracesSampleRate).
6565

66-
6766
### Google Cloud Functions
6867

6968
To use this SDK, call `Sentry.GCPFunction.init(options)` at the very beginning of your JavaScript file.

packages/serverless/src/awslambda-auto.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ if (lambdaTaskRoot) {
66
if (!handlerString) {
77
throw Error(`LAMBDA_TASK_ROOT is non-empty(${lambdaTaskRoot}) but _HANDLER is not set`);
88
}
9-
Sentry.AWSLambda.init();
9+
10+
Sentry.AWSLambda.init({
11+
_invokedByLambdaLayer: true,
12+
});
13+
1014
Sentry.AWSLambda.tryPatchHandler(lambdaTaskRoot, handlerString);
1115
} else {
1216
throw Error('LAMBDA_TASK_ROOT environment variable is not set');

packages/serverless/src/awslambda.ts

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '@sentry/node';
1212
import { extractTraceparentData } from '@sentry/tracing';
1313
import { Integration } from '@sentry/types';
14-
import { isString, logger, parseBaggageSetMutability } from '@sentry/utils';
14+
import { dsnFromString, dsnToString, isString, logger, parseBaggageSetMutability } from '@sentry/utils';
1515
// NOTE: I have no idea how to fix this right now, and don't want to waste more time, as it builds just fine — Kamil
1616
// eslint-disable-next-line import/no-unresolved
1717
import { Context, Handler } from 'aws-lambda';
@@ -57,10 +57,39 @@ export interface WrapperOptions {
5757

5858
export const defaultIntegrations: Integration[] = [...Sentry.defaultIntegrations, new AWSServices({ optional: true })];
5959

60+
/**
61+
* Changes a Dsn to point to the `relay` server running in the Lambda Extension.
62+
*
63+
* This is only used by the serverless integration for AWS Lambda.
64+
*
65+
* @param originalDsn The original Dsn of the customer.
66+
* @returns Dsn pointing to Lambda extension.
67+
*/
68+
function extensionRelayDSN(originalDsn: string | undefined): string | undefined {
69+
if (originalDsn === undefined) {
70+
return undefined;
71+
}
72+
73+
const dsn = dsnFromString(originalDsn);
74+
dsn.host = 'localhost';
75+
dsn.port = '3000';
76+
dsn.protocol = 'http';
77+
78+
return dsnToString(dsn);
79+
}
80+
81+
interface AWSLambdaOptions extends Sentry.NodeOptions {
82+
/**
83+
* Internal field that is set to `true` when init() is called by the Sentry AWS Lambda layer.
84+
*
85+
*/
86+
_invokedByLambdaLayer?: boolean;
87+
}
88+
6089
/**
6190
* @see {@link Sentry.init}
6291
*/
63-
export function init(options: Sentry.NodeOptions = {}): void {
92+
export function init(options: AWSLambdaOptions = {}): void {
6493
if (options.defaultIntegrations === undefined) {
6594
options.defaultIntegrations = defaultIntegrations;
6695
}
@@ -78,6 +107,12 @@ export function init(options: Sentry.NodeOptions = {}): void {
78107
version: Sentry.SDK_VERSION,
79108
};
80109

110+
// If invoked by the Sentry Lambda Layer point the SDK to the Lambda Extension (inside the layer) instead of the host
111+
// specified in the DSN
112+
if (options._invokedByLambdaLayer) {
113+
options.dsn = extensionRelayDSN(options.dsn);
114+
}
115+
81116
Sentry.init(options);
82117
Sentry.addGlobalEventProcessor(serverlessEventProcessor);
83118
}

packages/serverless/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
"compilerOptions": {
77
// package-specific options
8-
"target": "ES2018",
8+
"target": "ES2018"
99
}
1010
}

packages/utils/src/dsn.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function dsnToString(dsn: DsnComponents, withPassword: boolean = false):
3232
* @param str A Dsn as string
3333
* @returns Dsn as DsnComponents
3434
*/
35-
function dsnFromString(str: string): DsnComponents {
35+
export function dsnFromString(str: string): DsnComponents {
3636
const match = DSN_REGEX.exec(str);
3737

3838
if (!match) {
@@ -106,24 +106,3 @@ export function makeDsn(from: DsnLike): DsnComponents {
106106
validateDsn(components);
107107
return components;
108108
}
109-
110-
/**
111-
* Changes a Dsn to point to the `relay` server running in the Lambda Extension.
112-
*
113-
* This is only used by the serverless integration for AWS Lambda.
114-
*
115-
* @param originalDsn The original Dsn of the customer.
116-
* @returns Dsn pointing to Lambda extension.
117-
*/
118-
export function extensionRelayDSN(originalDsn: string | undefined): string | undefined {
119-
if (originalDsn === undefined) {
120-
return undefined;
121-
}
122-
123-
const dsn = dsnFromString(originalDsn);
124-
dsn.host = 'localhost';
125-
dsn.port = '3000';
126-
dsn.protocol = 'http';
127-
128-
return dsnToString(dsn);
129-
}

scripts/aws-deploy-local-layer.sh

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
set -euo pipefail
1212

13-
# Cleanup
13+
# Remove old distribution directories and zip files.
1414
echo "Preparing local directories for new build..."
1515
rm -rf dist-serverless/
1616
rm -rf ./packages/serverless/build
@@ -37,12 +37,36 @@ echo "Done copying Lambda layer in ./packages/serverless/build/aws/dist-serverle
3737
# is building the Lambda layer in production!
3838
# see: https://github.com/getsentry/action-build-aws-lambda-extension/blob/main/action.yml#L23-L40
3939

40-
# Adding Sentry Lambda extension to Lambda layer
41-
echo "Adding Sentry Lambda extension to Lambda layer in ./dist-serverless..."
40+
echo "Downloading relay..."
41+
# Make directory (if not existing)
42+
mkdir -p dist-serverless/relay
43+
# Download releay from release registry to dist-serverless/relay/relay
44+
curl -0 --silent \
45+
--output dist-serverless/relay/relay \
46+
"$(curl -s https://release-registry.services.sentry.io/apps/relay/latest | jq -r .files.\"relay-Linux-x86_64\".url)"
47+
# Make file executable
48+
chmod +x dist-serverless/relay/relay
49+
echo "Done downloading relay."
50+
51+
echo "Creating start script..."
52+
# Make directory (if not existing)
4253
mkdir -p dist-serverless/extensions
43-
curl -0 --silent --output dist-serverless/extensions/sentry-lambda-extension $(curl -s https://release-registry.services.sentry.io/apps/sentry-lambda-extension/latest | jq -r .files.\"sentry-lambda-extension\".url)
54+
# Create 'sentry-lambda-extension' script that starts relay.
55+
# The file has to have exactly this name, because the executable files of
56+
# Lambda extensions need to have same file name as the name that they use
57+
# to register with AWS Lambda environment
58+
cat > dist-serverless/extensions/sentry-lambda-extension << EOT
59+
#!/bin/bash
60+
set -euo pipefail
61+
exec /opt/relay/relay run \
62+
--mode=proxy \
63+
--shutdown-timeout=2 \
64+
--upstream-dsn="\$SENTRY_DSN" \
65+
--aws-runtime-api="\$AWS_LAMBDA_RUNTIME_API"
66+
EOT
67+
# Make script executable
4468
chmod +x dist-serverless/extensions/sentry-lambda-extension
45-
echo "Done adding Sentry Lambda extension to Lambda layer in ./dist-serverless."
69+
echo "Done creating start script."
4670

4771
# Zip Lambda layer and included Lambda extension
4872
echo "Zipping Lambda layer and included Lambda extension..."

0 commit comments

Comments
 (0)