Skip to content

Commit

Permalink
chore: fixup aws sam lambda paths
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed Oct 17, 2024
1 parent b7cf768 commit 8601d84
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ deps:
poetry install

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

venv:
@if [ -d "./.venv" ]; then echo "$(red).venv already exists, not continuing!$(sgr0)"; exit 1; fi
Expand Down
10 changes: 9 additions & 1 deletion src/_lambda/product.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import json
import asyncio
from typing import Dict
from src.product.product_service import receive_product_update
logger = logging.getLogger(__name__)
Expand All @@ -8,7 +9,7 @@
# Actual lambda handler, responsible for extracting message from SNS
# and dealing with lambda-related things, passing the encoded message along to the
# message handler
async def handler(event: Dict):
async def handler(event: Dict, context):
logger.info(f'{event=}')

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

# Return the current size of the repository
return len(results.keys())

def lambda_handler(event, context):
loop = asyncio.get_event_loop()
# DynamoDB resource defined above is attached to this loop:
# if you use asyncio.run instead
# you will encounter "Event loop closed" exception
return loop.run_until_complete(handler(event, context))
2 changes: 1 addition & 1 deletion template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Resources:
Properties:
Description: A Lambda function that receives a Product update event from the ProductEvent topic
Runtime: python3.11
Handler: src.lambda.product.handler
Handler: src._lambda.product.lambda_handler
# This property associates this Lambda function with the SNS topic defined above, so that whenever the topic
# receives a message, the Lambda function is invoked
Events:
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async def test_lambda_consumes_a_valid_sns_event(mocker):
payload = json.load(f)

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

# (3) Assert: should return
assert result == num_products + 1
Expand Down

0 comments on commit 8601d84

Please sign in to comment.