Skip to content

Commit 138cf85

Browse files
feat(aws_lambda): add aws_request_id and log_stream_name to the request context (#1624)
Co-authored-by: Michael Brooks <mbrooks@slack-corp.com>
1 parent f5f9ad7 commit 138cf85

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

‎slack_bolt/adapter/aws_lambda/handler.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def handle(self, event, context):
5757
aws_lambda_function_name = context.function_name
5858
bolt_req.context["aws_lambda_function_name"] = aws_lambda_function_name
5959
bolt_req.context["aws_lambda_invoked_function_arn"] = context.invoked_function_arn
60+
bolt_req.context["aws_lambda_request_id"] = context.aws_request_id
61+
bolt_req.context["aws_lambda_log_stream_name"] = context.log_stream_name
6062
bolt_req.context["lambda_request"] = event
6163
bolt_resp = self.app.dispatch(bolt_req)
6264
aws_response = to_aws_response(bolt_resp)

‎tests/adapter_tests/aws/test_aws_lambda.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class LambdaContext:
3131
def __init__(self, function_name: str):
3232
self.function_name = function_name
3333
self.invoked_function_arn = f"arn:aws:lambda:us-east-1:account-id:function:{self.function_name}"
34+
self.aws_request_id = "52fdfc07-2182-154f-163f-5f0f9a621d72"
35+
self.log_stream_name = "2024/03/01/[$LATEST]abcdef0123456789"
3436

3537

3638
class TestAWSLambda:
@@ -67,6 +69,38 @@ def build_headers(self, timestamp: str, body: str):
6769
"x-slack-request-timestamp": [timestamp],
6870
}
6971

72+
def test_lambda_context_in_bolt_context(self):
73+
app = App(client=self.web_client, signing_secret=self.signing_secret)
74+
captured = {}
75+
76+
@app.middleware
77+
def capture(context, next):
78+
captured.update(context)
79+
next()
80+
81+
app.event("app_mention")(lambda: None)
82+
input = {
83+
"token": "verification_token",
84+
"team_id": "T111",
85+
"api_app_id": "A111",
86+
"event": {"type": "app_mention", "text": "hi", "user": "W222", "team": "T111", "channel": "C111"},
87+
"type": "event_callback",
88+
"event_id": "Ev111",
89+
"event_time": 1595926230,
90+
}
91+
timestamp, body = str(int(time())), json.dumps(input)
92+
event = {
93+
"body": body,
94+
"queryStringParameters": {},
95+
"headers": self.build_headers(timestamp, body),
96+
"requestContext": {"http": {"method": "POST"}},
97+
"isBase64Encoded": False,
98+
}
99+
response = SlackRequestHandler(app).handle(event, self.context)
100+
assert response["statusCode"] == 200
101+
assert captured["aws_lambda_request_id"] == self.context.aws_request_id
102+
assert captured["aws_lambda_log_stream_name"] == self.context.log_stream_name
103+
70104
def test_not_found(self):
71105
response = not_found()
72106
assert response["statusCode"] == 404

0 commit comments

Comments
 (0)