Skip to content

Commit 4ca6aca

Browse files
committed
fix: Dont crash during chalice deployment
1 parent 429c36b commit 4ca6aca

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

sentry_sdk/integrations/aws_lambda.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,27 @@
77
AnnotatedValue,
88
capture_internal_exceptions,
99
event_from_exception,
10+
logger,
1011
)
1112
from sentry_sdk.integrations import Integration
1213
from sentry_sdk.integrations._wsgi import _filter_headers
1314

14-
import __main__ as lambda_bootstrap
15-
import runtime as lambda_runtime
16-
1715

1816
class AwsLambdaIntegration(Integration):
1917
identifier = "aws_lambda"
2018

2119
def install(self):
20+
import __main__ as lambda_bootstrap
21+
22+
if not hasattr(lambda_bootstrap, "make_final_handler"):
23+
logger.warn(
24+
"Not running in AWS Lambda environment, "
25+
"AwsLambdaIntegration disabled"
26+
)
27+
return
28+
29+
import runtime as lambda_runtime
30+
2231
old_make_final_handler = lambda_bootstrap.make_final_handler
2332

2433
def sentry_make_final_handler(*args, **kwargs):

tests/integrations/aws_lambda/test_aws.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,17 @@ def inner(code, payload):
6464
tmpdir.ensure_dir("lambda_tmp").remove()
6565
tmp = tmpdir.ensure_dir("lambda_tmp")
6666

67-
# https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html
6867
tmp.join("test_lambda.py").write(code)
68+
69+
# Check file for valid syntax first, and that the integration does not
70+
# crash when not running in Lambda (but rather a local deployment tool
71+
# such as chalice's)
72+
subprocess.check_call([sys.executable, str(tmp.join("test_lambda.py"))])
73+
6974
tmp.join("setup.cfg").write("[install]\nprefix=")
7075
subprocess.check_call([sys.executable, "setup.py", "sdist", "-d", str(tmpdir)])
76+
77+
# https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html
7178
subprocess.check_call("pip install ../*.tar.gz -t .", cwd=str(tmp), shell=True)
7279
shutil.make_archive(tmpdir.join("ball"), "zip", str(tmp))
7380

0 commit comments

Comments
 (0)