Skip to content

Commit 72c6b67

Browse files
committed
fix(examples): appease SonarCloud in WebSocket examples
Adds botocore timeouts to Management API clients, moves client construction out of the handler, and enables function and access logging in the getting-started SAM template. Example code, it turns out, is held to production standards — fair enough.
1 parent 073e08e commit 72c6b67

4 files changed

Lines changed: 28 additions & 4 deletions

File tree

docs/core/event_handler/api_gateway_websocket.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ You need an API Gateway WebSocket API with its routes integrated with your Lambd
6565

6666
=== "getting_started_with_websocket_api.yaml"
6767

68-
```yaml hl_lines="27 50-80"
68+
```yaml hl_lines="39 62-92"
6969
--8<-- "examples/event_handler_api_gateway_websocket/sam/getting_started_with_websocket_api.yaml"
7070
```
7171

@@ -243,7 +243,7 @@ A stale connection ID raises `GoneException` — the client is no longer connect
243243

244244
=== "working_with_post_to_connection.py"
245245

246-
```python hl_lines="2 16 28 35 41"
246+
```python hl_lines="2 18 30 37 44"
247247
--8<-- "examples/event_handler_api_gateway_websocket/src/working_with_post_to_connection.py"
248248
```
249249

@@ -254,7 +254,7 @@ A stale connection ID raises `GoneException` — the client is no longer connect
254254

255255
=== "working_with_post_to_connection_report_ready.py"
256256

257-
```python hl_lines="18 23"
257+
```python hl_lines="24 29"
258258
--8<-- "examples/event_handler_api_gateway_websocket/src/working_with_post_to_connection_report_ready.py"
259259
```
260260

examples/event_handler_api_gateway_websocket/sam/getting_started_with_websocket_api.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ Resources:
1818
Properties:
1919
Handler: app.lambda_handler
2020
CodeUri: websocket_handler
21+
LoggingConfig:
22+
LogGroup: !Ref WebSocketHandlerLogs
23+
24+
WebSocketHandlerLogs:
25+
Type: AWS::Logs::LogGroup
26+
Properties:
27+
RetentionInDays: 7
28+
29+
WebSocketAccessLogs:
30+
Type: AWS::Logs::LogGroup
31+
Properties:
32+
RetentionInDays: 7
2133

2234
WebSocketApi:
2335
Type: AWS::ApiGatewayV2::Api
@@ -85,6 +97,9 @@ Resources:
8597
ApiId: !Ref WebSocketApi
8698
StageName: prod
8799
AutoDeploy: true
100+
AccessLogSettings:
101+
DestinationArn: !GetAtt WebSocketAccessLogs.Arn
102+
Format: '{"requestId":"$context.requestId","routeKey":"$context.routeKey","status":"$context.status","connectionId":"$context.connectionId"}'
88103

89104
WebSocketInvokePermission:
90105
Type: AWS::Lambda::Permission

examples/event_handler_api_gateway_websocket/src/working_with_post_to_connection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import boto3
22
import my_connection_store # (1)!
3+
from botocore.config import Config
34

45
from aws_lambda_powertools.event_handler import APIGatewayWebSocketResolver
56
from aws_lambda_powertools.utilities.typing import LambdaContext
67

8+
boto_config = Config(connect_timeout=3, read_timeout=5, retries={"total_max_attempts": 2})
79
app = APIGatewayWebSocketResolver()
810

911

@@ -33,6 +35,7 @@ def broadcast():
3335
client = boto3.client(
3436
"apigatewaymanagementapi",
3537
endpoint_url=app.current_event.request_context.callback_url, # (4)!
38+
config=boto_config,
3639
)
3740
message: str = app.current_event.json_body["message"]
3841
for connection_id in my_connection_store.all_connection_ids():

examples/event_handler_api_gateway_websocket/src/working_with_post_to_connection_report_ready.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66

77
import boto3
88
import my_connection_store
9+
from botocore.config import Config
910

1011
from aws_lambda_powertools import Logger
1112
from aws_lambda_powertools.utilities.typing import LambdaContext
1213

1314
logger = Logger()
15+
boto_config = Config(connect_timeout=3, read_timeout=5, retries={"total_max_attempts": 2})
16+
17+
18+
def management_client_for(callback_url: str):
19+
return boto3.client("apigatewaymanagementapi", endpoint_url=callback_url, config=boto_config)
1420

1521

1622
def lambda_handler(event: dict, context: LambdaContext) -> None:
1723
connection_id = event["connectionId"]
1824
callback_url = my_connection_store.get_callback_url(connection_id) # (1)!
1925

20-
client = boto3.client("apigatewaymanagementapi", endpoint_url=callback_url)
26+
client = management_client_for(callback_url)
2127
try:
2228
client.post_to_connection(ConnectionId=connection_id, Data=json.dumps(event["report"]).encode())
2329
except client.exceptions.GoneException: # (2)!

0 commit comments

Comments
 (0)