Reputation: 7940
I want to defer some processing load from my Django app to an AWS Lambda.
I'm calling my code from the Lambda like this:
lambda.py:
@bc_lambda(level=logging.INFO, service=LAMBDA_SERVICE)
def task_handler(event, context):
message = event["Records"][0]["body"]
renderer = get_renderer_for(message)
result = renderer.render()
return result
get_renderer_for
is a factory method that returns an instance of the class Renderer
:
from myproject.apps.engine.documents import (
DocumentsLoader,
SourceNotFound,
source_from_version,
)
from myproject.apps.engine.environment import Environment
class Renderer:
def __init__(self, message):
self.message = message
def render(self):
ENVIRONMENT = Environment(DocumentsLoader())
version_id = self.message.get("version_id")
try:
source = source_from_version(version_id)
except SourceNotFound:
source = None
template = ENVIRONMENT.from_string(source)
if template:
return template.render(self.message)
return None
def get_renderer_for(message):
"""
Factory method that returns an instance of the Renderer class
"""
return Renderer(message)
In CloudWatch, I see I'm getting this error: module initialization error. Apps aren't loaded yet.
I understand that Django is not available for the Lambda function, right? How can I fix this? How can I make the rest of the project available to the lambda function?
Upvotes: 1
Views: 501
Reputation: 6256
The only two libraries that Lambda supports out of the box are the standard library and boto3.
There are several ways to install external Python libraries for use in Lambda. I recommend uploading them as a Lambda layer. This is a good guide: https://medium.com/@qtangs/creating-new-aws-lambda-layer-for-python-pandas-library-348b126e9f3e
Upvotes: 1