Skip to content

Commit 49982a9

Browse files
committed
Making AWS Lambda invokation work reliabily
1 parent 9e357ef commit 49982a9

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
def handler(event, context):
3+
1/0
4+
raise Exception("Oh!")
5+
6+
return {
7+
"event": event,
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
def handler(event, context):
3+
return {
4+
"event": event,
5+
}

tests/integrations/aws_lambda/test_aws_lambda.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import pytest
44
import subprocess
5+
import tempfile
56
import time
67
import yaml
78

@@ -30,17 +31,23 @@ def test_environment():
3031
with open(SAM_TEMPLATE_FILE, "w") as f:
3132
yaml.dump(template, f)
3233

34+
debug_log_file = tempfile.gettempdir() + "/sentry_aws_lambda_tests_sam_debug.log"
35+
debug_log = open(debug_log_file, "w")
36+
print(f"Writing SAM debug log to: {debug_log_file}")
37+
3338
# Start SAM local
3439
process = subprocess.Popen(
3540
[
3641
"sam",
3742
"local",
3843
"start-lambda",
44+
"--debug",
3945
"--template",
4046
SAM_TEMPLATE_FILE,
47+
"--warm-containers", "EAGER",
4148
],
42-
stdout=subprocess.PIPE,
43-
stderr=subprocess.PIPE,
49+
stdout=debug_log,
50+
stderr=debug_log,
4451
text=True, # This makes stdout/stderr return strings instead of bytes
4552
)
4653

@@ -88,7 +95,7 @@ def lambda_client():
8895

8996
# def test_basic_ok(lambda_client, test_environment):
9097
# response = lambda_client.invoke(
91-
# FunctionName="BasicOk",
98+
# FunctionName="BasicOk",
9299
# Payload=json.dumps({"name": "Ivana"}),
93100
# )
94101
# result = json.loads(response["Payload"].read().decode())
@@ -102,27 +109,25 @@ def lambda_client():
102109

103110

104111
def test_xxx(lambda_client, test_environment):
105-
for x in range(20):
112+
for x in range(20):
106113
test_environment["server"].clear_envelopes()
107114
print(f"*** BasicException {x} ***")
108115
response = lambda_client.invoke(
109-
FunctionName="BasicException",
116+
FunctionName="BasicException",
110117
Payload=json.dumps({}),
111118
)
112119
print("- RESPONSE")
113120
print(response)
114121
print("- PAYLOAD")
115122
print(response["Payload"].read().decode())
116-
print(f'- ENVELOPES {len(test_environment["server"].envelopes)}')
117-
118-
119-
120-
assert False
123+
num_envelopes = len(test_environment["server"].envelopes)
124+
print(f'- ENVELOPES {num_envelopes}')
125+
assert num_envelopes == 2
121126

122127

123128
# def test_basic(lambda_client, test_environment):
124129
# response = lambda_client.invoke(
125-
# FunctionName="BasicException",
130+
# FunctionName="BasicException",
126131
# Payload=json.dumps({"name": "Neel"}),
127132
# )
128133
# print("RESPONSE")

tests/integrations/aws_lambda/utils.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818

1919
SAM_PORT = 3001
20+
FUNCTIONS_DIR = "./tests/integrations/aws_lambda/lambda_functions/"
2021

2122

2223
class DummyLambdaStack(Stack):
@@ -29,13 +30,14 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
2930
self.template_options.template_format_version = "2010-09-09"
3031
self.template_options.transforms = ["AWS::Serverless-2016-10-31"]
3132

32-
# Create Sentry Lambda layer to the SAM stack
33+
print("- Create Sentry Lambda layer package")
3334
filename = "sentry-sdk-lambda-layer.zip"
3435
build_packaged_zip(
3536
make_dist=True,
3637
out_zip_filename=filename,
3738
)
3839

40+
print("- Add Sentry Lambda layer containing the Sentry SDK to the SAM stack")
3941
self.sentry_layer = CfnResource(
4042
self,
4143
"SentryPythonServerlessSDK",
@@ -54,10 +56,9 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
5456
},
5557
)
5658

57-
# Add all lambda functions from /tests/integrations/aws_lambda/lambda_functions/ to the SAM stack
58-
FUNCTIONS_DIR = "./tests/integrations/aws_lambda/lambda_functions/"
59+
print("- Add all Lambda functions defined in /tests/integrations/aws_lambda/lambda_functions/ to the SAM stack")
5960
lambda_dirs = [
60-
d for d in os.listdir(FUNCTIONS_DIR)
61+
d for d in os.listdir(FUNCTIONS_DIR)
6162
if os.path.isdir(os.path.join(FUNCTIONS_DIR, d))
6263
]
6364
for lambda_dir in lambda_dirs:
@@ -71,6 +72,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
7172
"CodeUri": os.path.join(FUNCTIONS_DIR, lambda_dir),
7273
"Handler": "sentry_sdk.integrations.init_serverless_sdk.sentry_lambda_handler",
7374
"Runtime": "python3.12",
75+
"Timeout": 15,
7476
"Layers": [{"Ref": self.sentry_layer.logical_id}], # Add layer containing the Sentry SDK to function.
7577
"Environment": {
7678
"Variables": {
@@ -81,7 +83,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
8183
},
8284
},
8385
)
84-
print(f"- created function: {lambda_name} / {os.path.join(FUNCTIONS_DIR, lambda_dir)}")
86+
print(f" - Created Lambda function: {lambda_name} / {os.path.join(FUNCTIONS_DIR, lambda_dir)}")
8587

8688

8789
@classmethod
@@ -93,7 +95,7 @@ def wait_for_stack(cls, timeout=30, port=SAM_PORT):
9395
while True:
9496
if time.time() - start_time > timeout:
9597
raise TimeoutError(
96-
"SAM failed to start within {} seconds".format(timeout)
98+
"SAM failed to start within {} seconds. (Maybe Docker is not running?)".format(timeout)
9799
)
98100

99101
try:

0 commit comments

Comments
 (0)