Repository navigation
Expand file tree
/
Copy pathstack.ts
More file actions
159 lines (131 loc) · 5.63 KB
/
Copy pathstack.ts
File metadata and controls
159 lines (131 loc) · 5.63 KB
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
import { Stack, CfnResource, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as path from 'node:path';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as dns from 'node:dns/promises';
import { arch, platform } from 'node:process';
import { execFileSync } from 'node:child_process';
const LAMBDA_FUNCTIONS_DIR = './src/lambda-functions-npm';
const LAMBDA_FUNCTION_TIMEOUT = 10;
export const SAM_PORT = 3001;
/** Match SAM / Docker to this machine so Apple Silicon does not mix arm64 images with an x86_64 template default. */
function samLambdaArchitecture(): 'arm64' | 'x86_64' {
return arch === 'arm64' ? 'arm64' : 'x86_64';
}
function resolvePackagesDir(): string {
// When running via the e2e test runner, tests are copied to a temp directory
// so we need the workspace root passed via env var
const workspaceRoot = process.env.SENTRY_E2E_WORKSPACE_ROOT;
if (workspaceRoot) {
return path.join(workspaceRoot, 'packages');
}
// Fallback for local development when running from the original location
return path.resolve(__dirname, '../../../../../packages');
}
export class LocalLambdaStack extends Stack {
constructor(scope: Construct, id: string, props: StackProps, hostIp: string) {
console.log('[LocalLambdaStack] Creating local SAM Lambda Stack');
super(scope, id, props);
this.templateOptions.templateFormatVersion = '2010-09-09';
this.templateOptions.transforms = ['AWS::Serverless-2016-10-31'];
const dsn = `http://public@${hostIp}:3031/1337`;
console.log(`[LocalLambdaStack] Using Sentry DSN: ${dsn}`);
this.addLambdaFunctions(dsn);
}
private addLambdaFunctions(dsn: string) {
console.log(`[LocalLambdaStack] Add all Lambda functions defined in ${LAMBDA_FUNCTIONS_DIR} to the SAM stack`);
const lambdaDirs = fs
.readdirSync(LAMBDA_FUNCTIONS_DIR)
.filter(dir => fs.statSync(path.join(LAMBDA_FUNCTIONS_DIR, dir)).isDirectory());
for (const lambdaDir of lambdaDirs) {
const functionName = `Npm${lambdaDir}`;
const lambdaPath = path.resolve(LAMBDA_FUNCTIONS_DIR, lambdaDir);
const packageLockPath = path.join(lambdaPath, 'package-lock.json');
const nodeModulesPath = path.join(lambdaPath, 'node_modules');
// `dir` is the package directory under `packages/`; `name` is the published
// npm name (most are `@sentry/<dir>`, but `server-utils` is `@sentry-internal`).
const packagesToLink: Array<{ dir: string; name: string }> = [
{ dir: 'aws-serverless', name: '@sentry/aws-serverless' },
{ dir: 'node', name: '@sentry/node' },
{ dir: 'core', name: '@sentry/core' },
{ dir: 'node-core', name: '@sentry/node-core' },
{ dir: 'opentelemetry', name: '@sentry/opentelemetry' },
{ dir: 'server-utils', name: '@sentry/server-utils' },
];
const dependencies: Record<string, string> = {};
const packagesDir = resolvePackagesDir();
for (const { dir, name } of packagesToLink) {
const pkgDir = path.join(packagesDir, dir);
if (!fs.existsSync(pkgDir)) {
throw new Error(
`[LocalLambdaStack] Workspace package ${name} not found at ${pkgDir}. Did you run the build?`,
);
}
const relativePath = path.relative(lambdaPath, pkgDir);
dependencies[name] = `file:${relativePath.replace(/\\/g, '/')}`;
}
console.log(`[LocalLambdaStack] Install dependencies for ${functionName}`);
if (fs.existsSync(packageLockPath)) {
fs.rmSync(packageLockPath);
}
if (fs.existsSync(nodeModulesPath)) {
fs.rmSync(nodeModulesPath, { recursive: true, force: true });
}
const packageJson = {
dependencies,
};
fs.writeFileSync(path.join(lambdaPath, 'package.json'), JSON.stringify(packageJson, null, 2));
execFileSync('npm', ['install', '--install-links', '--prefix', lambdaPath], { stdio: 'inherit' });
if (!process.env.NODE_VERSION) {
throw new Error('[LocalLambdaStack] NODE_VERSION is not set');
}
new CfnResource(this, functionName, {
type: 'AWS::Serverless::Function',
properties: {
Architectures: [samLambdaArchitecture()],
CodeUri: path.join(LAMBDA_FUNCTIONS_DIR, lambdaDir),
Handler: 'index.handler',
Runtime: `nodejs${process.env.NODE_VERSION}.x`,
Timeout: LAMBDA_FUNCTION_TIMEOUT,
Environment: {
Variables: {
SENTRY_DSN: dsn,
SENTRY_TRACES_SAMPLE_RATE: 1.0,
SENTRY_DEBUG: true,
NODE_OPTIONS: `--import=@sentry/aws-serverless/awslambda-auto`,
},
},
},
});
console.log(`[LocalLambdaStack] Added Lambda function: ${functionName}`);
}
}
static async waitForStack(timeout = 60000, port = SAM_PORT) {
const startTime = Date.now();
const maxWaitTime = timeout;
while (Date.now() - startTime < maxWaitTime) {
try {
const response = await fetch(`http://127.0.0.1:${port}/`);
if (response.ok || response.status === 404) {
console.log(`[LocalLambdaStack] SAM stack is ready`);
return;
}
} catch {
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
throw new Error(`[LocalLambdaStack] Failed to start SAM stack after ${timeout}ms`);
}
}
export async function getHostIp() {
if (process.env.GITHUB_ACTIONS) {
const host = await dns.lookup(os.hostname());
return host.address;
}
if (platform === 'darwin' || platform === 'win32') {
return 'host.docker.internal';
}
const host = await dns.lookup(os.hostname());
return host.address;
}