Skip to content

Commit

Permalink
implement lambda handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
pymonger committed May 22, 2024
1 parent 8fcc86c commit 942e960
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/unity_initiator/cloud/lambda_handler.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
from tempfile import mkstemp

Expand All @@ -7,7 +8,8 @@
ROUTER = None


def lambda_handler(event, context):
def lambda_handler_base(event, context):
"""Base lambda handler that instantiates a router, globally, and executes actions for a single payload."""
logger.info("context: %s", context)

# TODO: Should use either AppConfig or retrieve router config from S3 location.
Expand All @@ -22,3 +24,26 @@ def lambda_handler(event, context):
ROUTER = Router(router_file)
os.unlink(router_file)
return ROUTER.execute_actions(event["payload"])


def lambda_handler_multiple_payloads(event, context):
"""Lambda handler that executes actions for a list of event payloads."""

return [lambda_handler_base(evt["payload"], context) for evt in event]


def lambda_handler_initiator(event, context):
"""Lambda handler that executes actions for a list of S3 notification events propagated through SNS->SQS."""

payloads = []
sqs_messages = event["Messages"]
for sqs_message in sqs_messages:
sns_message = json.loads(sqs_message["Body"])
s3_records = json.loads(sns_message["Message"])
for s3_record in s3_records:
payloads.append(
{
"payload": f"s3://{s3_record['s3']['bucket']['name']}/{s3_record['s3']['object']['key']}"
}
)
return lambda_handler_multiple_payloads(payloads, context)
4 changes: 2 additions & 2 deletions tests/test_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from importlib_resources import files
from moto import mock_aws

from unity_initiator.cloud.lambda_handler import lambda_handler as lh
from unity_initiator.cloud.lambda_handler import lambda_handler_base
from unity_initiator.utils.logger import logger

# mock default region
Expand Down Expand Up @@ -54,7 +54,7 @@ def lambda_handler(event, context):
)
)

return lh(event, context)
return lambda_handler_base(event, context)


def get_role_name():
Expand Down

0 comments on commit 942e960

Please sign in to comment.