|
1 | 1 | import gzip |
2 | 2 | import json |
3 | 3 | import os |
| 4 | +import shutil |
| 5 | +import subprocess |
4 | 6 | import requests |
5 | 7 | import sys |
6 | 8 | import time |
7 | 9 | import threading |
8 | 10 | import socket |
9 | 11 | import platform |
| 12 | +import tempfile |
10 | 13 |
|
11 | 14 | from aws_cdk import ( |
12 | 15 | CfnResource, |
|
20 | 23 |
|
21 | 24 |
|
22 | 25 | LAMBDA_FUNCTION_DIR = "./tests/integrations/aws_lambda/lambda_functions/" |
| 26 | +LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR = "./tests/integrations/aws_lambda/lambda_functions_with_embedded_sdk/" |
23 | 27 | LAMBDA_FUNCTION_TIMEOUT = 10 |
24 | 28 | SAM_PORT = 3001 |
25 | 29 |
|
@@ -47,6 +51,20 @@ def get_host_ip(): |
47 | 51 | return host |
48 | 52 |
|
49 | 53 |
|
| 54 | +def get_project_root(): |
| 55 | + """ |
| 56 | + Returns the absolute path to the project root directory. |
| 57 | + """ |
| 58 | + # Start from the current file's directory |
| 59 | + current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 60 | + |
| 61 | + # Navigate up to the project root (4 levels up from tests/integrations/aws_lambda/) |
| 62 | + # This is equivalent to the multiple dirname() calls |
| 63 | + project_root = os.path.abspath(os.path.join(current_dir, "../../../")) |
| 64 | + |
| 65 | + return project_root |
| 66 | + |
| 67 | + |
50 | 68 | class LocalLambdaStack(Stack): |
51 | 69 | """ |
52 | 70 | Uses the AWS CDK to create a local SAM stack containing Lambda functions. |
@@ -124,6 +142,65 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: |
124 | 142 | ) |
125 | 143 | ) |
126 | 144 |
|
| 145 | + print( |
| 146 | + "[LocalLambdaStack] Add all Lambda functions defined in " |
| 147 | + "/tests/integrations/aws_lambda/lambda_functions_with_embedded_sdk/ to the SAM stack" |
| 148 | + ) |
| 149 | + lambda_dirs = [ |
| 150 | + d |
| 151 | + for d in os.listdir(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR) |
| 152 | + if os.path.isdir(os.path.join(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, d)) |
| 153 | + ] |
| 154 | + for lambda_dir in lambda_dirs: |
| 155 | + # Copy the Sentry SDK into the function directory |
| 156 | + import ipdb; ipdb.set_trace() |
| 157 | + sdk_path = os.path.join(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, lambda_dir, "sentry_sdk") |
| 158 | + if not os.path.exists(sdk_path): |
| 159 | + # Find the Sentry SDK in the current environment |
| 160 | + import sentry_sdk as sdk_module |
| 161 | + sdk_source = os.path.dirname(sdk_module.__file__) |
| 162 | + shutil.copytree(sdk_source, sdk_path) |
| 163 | + |
| 164 | + # Install the requirements of Sentry SDK into the function directory |
| 165 | + requirements_file = os.path.join(get_project_root(), "requirements-aws-lambda-layer.txt") |
| 166 | + |
| 167 | + # Install the package using pip |
| 168 | + subprocess.check_call([ |
| 169 | + sys.executable, |
| 170 | + "-m", |
| 171 | + "pip", |
| 172 | + "install", |
| 173 | + "--target", |
| 174 | + os.path.join(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, lambda_dir), |
| 175 | + "-r", |
| 176 | + requirements_file, |
| 177 | + ]) |
| 178 | + |
| 179 | + |
| 180 | + CfnResource( |
| 181 | + self, |
| 182 | + lambda_dir, |
| 183 | + type="AWS::Serverless::Function", |
| 184 | + properties={ |
| 185 | + "CodeUri": os.path.join(LAMBDA_FUNCTION_WITH_EMBEDDED_SDK_DIR, lambda_dir), |
| 186 | + "Handler": "sentry_sdk.integrations.init_serverless_sdk.sentry_lambda_handler", |
| 187 | + "Runtime": PYTHON_VERSION, |
| 188 | + "Timeout": LAMBDA_FUNCTION_TIMEOUT, |
| 189 | + "Environment": { |
| 190 | + "Variables": { |
| 191 | + "SENTRY_DSN": dsn, |
| 192 | + } |
| 193 | + }, |
| 194 | + }, |
| 195 | + ) |
| 196 | + print( |
| 197 | + "[LocalLambdaStack] - Created Lambda function: %s (%s)" |
| 198 | + % ( |
| 199 | + lambda_dir, |
| 200 | + os.path.join(LAMBDA_FUNCTION_DIR, lambda_dir), |
| 201 | + ) |
| 202 | + ) |
| 203 | + |
127 | 204 | @classmethod |
128 | 205 | def wait_for_stack(cls, timeout=30, port=SAM_PORT): |
129 | 206 | """ |
|
0 commit comments