|
| 1 | +import { Stack, CfnResource, StackProps } from 'aws-cdk-lib'; |
| 2 | +import { Construct } from 'constructs'; |
| 3 | +import * as path from 'node:path'; |
| 4 | +import * as fs from 'node:fs'; |
| 5 | +import * as os from 'node:os'; |
| 6 | +import * as dns from 'node:dns/promises'; |
| 7 | +import { platform } from 'node:process'; |
| 8 | + |
| 9 | +const LAMBDA_FUNCTION_DIR = './lambda-functions'; |
| 10 | +const LAMBDA_FUNCTION_TIMEOUT = 10; |
| 11 | +export const SAM_PORT = 3001; |
| 12 | +const NODE_RUNTIME = `nodejs${process.version.split('.').at(0)?.replace('v', '')}.x`; |
| 13 | + |
| 14 | +export class LocalLambdaStack extends Stack { |
| 15 | + sentryLayer: CfnResource; |
| 16 | + |
| 17 | + constructor(scope: Construct, id: string, props: StackProps, hostIp: string) { |
| 18 | + console.log('[LocalLambdaStack] Creating local SAM Lambda Stack'); |
| 19 | + super(scope, id, props); |
| 20 | + |
| 21 | + this.templateOptions.templateFormatVersion = '2010-09-09'; |
| 22 | + this.templateOptions.transforms = ['AWS::Serverless-2016-10-31']; |
| 23 | + |
| 24 | + console.log('[LocalLambdaStack] Add Sentry Lambda layer containing the Sentry SDK to the SAM stack'); |
| 25 | + |
| 26 | + this.sentryLayer = new CfnResource(this, 'SentryNodeServerlessSDK', { |
| 27 | + type: 'AWS::Serverless::LayerVersion', |
| 28 | + properties: { |
| 29 | + ContentUri: './sentry-node-serverless-10.2.0.zip', |
| 30 | + CompatibleRuntimes: ['nodejs18.x', 'nodejs20.x', 'nodejs22.x'], |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + const dsn = `http://public@${hostIp}:3031/1337`; |
| 35 | + console.log(`[LocalLambdaStack] Using Sentry DSN: ${dsn}`); |
| 36 | + |
| 37 | + console.log('[LocalLambdaStack] Add all Lambda function defined in ./lambda-functions/ to the SAM stack'); |
| 38 | + |
| 39 | + const lambdaDirs = fs |
| 40 | + .readdirSync(LAMBDA_FUNCTION_DIR) |
| 41 | + .filter(dir => fs.statSync(path.join(LAMBDA_FUNCTION_DIR, dir)).isDirectory()); |
| 42 | + |
| 43 | + for (const lambdaDir of lambdaDirs) { |
| 44 | + const isEsm = fs.existsSync(path.join(LAMBDA_FUNCTION_DIR, lambdaDir, 'index.mjs')); |
| 45 | + |
| 46 | + new CfnResource(this, lambdaDir, { |
| 47 | + type: 'AWS::Serverless::Function', |
| 48 | + properties: { |
| 49 | + CodeUri: path.join(LAMBDA_FUNCTION_DIR, lambdaDir), |
| 50 | + Handler: 'index.handler', |
| 51 | + Runtime: NODE_RUNTIME, |
| 52 | + Timeout: LAMBDA_FUNCTION_TIMEOUT, |
| 53 | + Layers: [ |
| 54 | + { |
| 55 | + Ref: this.sentryLayer.logicalId, |
| 56 | + }, |
| 57 | + ], |
| 58 | + Environment: { |
| 59 | + Variables: { |
| 60 | + SENTRY_DSN: dsn, |
| 61 | + SENTRY_TRACES_SAMPLE_RATE: 1.0, |
| 62 | + SENTRY_DEBUG: true, |
| 63 | + NODE_OPTIONS: `--${isEsm ? 'import' : 'require'}=@sentry/aws-serverless/awslambda-auto`, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }); |
| 68 | + |
| 69 | + console.log(`[LocalLambdaStack] Added Lambda function: ${lambdaDir}`); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + static async waitForStack(timeout = 60000, port = SAM_PORT) { |
| 74 | + const startTime = Date.now(); |
| 75 | + const maxWaitTime = timeout; |
| 76 | + |
| 77 | + while (Date.now() - startTime < maxWaitTime) { |
| 78 | + try { |
| 79 | + const response = await fetch(`http://127.0.0.1:${port}/`); |
| 80 | + |
| 81 | + if (response.ok || response.status === 404) { |
| 82 | + console.log(`[LocalLambdaStack] SAM stack is ready`); |
| 83 | + return; |
| 84 | + } |
| 85 | + } catch { |
| 86 | + await new Promise(resolve => setTimeout(resolve, 1000)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + throw new Error(`[LocalLambdaStack] Failed to start SAM stack after ${timeout}ms`); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +export async function getHostIp() { |
| 95 | + if (process.env.GITHUB_ACTIONS) { |
| 96 | + const host = await dns.lookup(os.hostname()); |
| 97 | + return host.address; |
| 98 | + } |
| 99 | + |
| 100 | + if (platform === 'darwin' || platform === 'win32') { |
| 101 | + return 'host.docker.internal'; |
| 102 | + } |
| 103 | + |
| 104 | + const host = await dns.lookup(os.hostname()); |
| 105 | + return host.address; |
| 106 | +} |
0 commit comments