Skip to content

Commit 8601d84

Browse files
committed
chore: fixup aws sam lambda paths
1 parent b7cf768 commit 8601d84

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ deps:
117117
poetry install
118118

119119
integration:
120-
sam local invoke ProductEventHandler --event ./__tests__/events/update.json
120+
sam local invoke ProductEventHandler --event ./tests/resources/events/update.json
121121

122122
venv:
123123
@if [ -d "./.venv" ]; then echo "$(red).venv already exists, not continuing!$(sgr0)"; exit 1; fi

src/_lambda/product.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22
import json
3+
import asyncio
34
from typing import Dict
45
from src.product.product_service import receive_product_update
56
logger = logging.getLogger(__name__)
@@ -8,7 +9,7 @@
89
# Actual lambda handler, responsible for extracting message from SNS
910
# and dealing with lambda-related things, passing the encoded message along to the
1011
# message handler
11-
async def handler(event: Dict):
12+
async def handler(event: Dict, context):
1213
logger.info(f'{event=}')
1314

1415
# Read the SNS message and pass the contents to the actual message handler
@@ -19,3 +20,10 @@ async def handler(event: Dict):
1920

2021
# Return the current size of the repository
2122
return len(results.keys())
23+
24+
def lambda_handler(event, context):
25+
loop = asyncio.get_event_loop()
26+
# DynamoDB resource defined above is attached to this loop:
27+
# if you use asyncio.run instead
28+
# you will encounter "Event loop closed" exception
29+
return loop.run_until_complete(handler(event, context))

template.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Resources:
2727
Properties:
2828
Description: A Lambda function that receives a Product update event from the ProductEvent topic
2929
Runtime: python3.11
30-
Handler: src.lambda.product.handler
30+
Handler: src._lambda.product.lambda_handler
3131
# This property associates this Lambda function with the SNS topic defined above, so that whenever the topic
3232
# receives a message, the Lambda function is invoked
3333
Events:

tests/integration/test_lambda.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async def test_lambda_consumes_a_valid_sns_event(mocker):
2020
payload = json.load(f)
2121

2222
# (2) Act: call the actual lambda with a valid SNS message
23-
result = await product.handler(payload)
23+
result = await product.handler(payload, {})
2424

2525
# (3) Assert: should return
2626
assert result == num_products + 1

0 commit comments

Comments
 (0)