I’m getting an error showing like this when I test my API with AWS lambda. I created this api using python flask and severless
{
“errorMessage”: “Unable to import app.app”,
“errorType”: “Exception”,
“requestId”: “”,
“stackTrace”: [
File “/var/lang/lib/python3.12/importlib/init.py”, line 90, in import_modulen return _bootstrap._gcd_import(name[level:], package, level)n”,
File “”, line 1387, in _gcd_importn”,
File “”, line 1360, in _find_and_loadn”,
File “”, line 1331, in _find_and_load_unlockedn”,
File “”, line 935, in _load_unlockedn”,
File “”, line 995, in exec_modulen”,
File “”, line 488, in _call_with_frames_removedn”,
File “/var/task/wsgi_handler.py”, line 115, in n wsgi_app = import_app(config)n”,
File “/var/task/wsgi_handler.py”, line 48, in import_appn raise Exception(“Unable to import {}”.format(config[“app”]))n”
]
}
here Im providing my serverless.yml,DockerFile and wsgi_handler.py
serverless.yml
service: email-sender
provider:
name: aws
runtime: python3.12
region: ap-south-1
functions:
api:
handler: wsgi_handler.handler
events:
- http:
path: /
method: post
- http:
path: /{proxy+}
method: post
plugins:
- serverless-wsgi
- serverless-python-requirements
- serverless-plugin-common-excludes
- serverless-plugin-include-dependencies
custom:
wsgi:
app: app.app
pythonRequirements:
dockerizePip: true # Use Docker to install dependencies
layer: true # Create a Lambda Layer for the requirements
zip: true # Zip the requirements to reduce the deployment package size
commonExcludes:
- '**/tests/**'
Dockerfile
FROM public.ecr.aws/lambda/python:3.9
`COPY app.py ./
COPY wsgi_handler.py ./
COPY requirements.txt ./
RUN pip install -r requirements.txt
ENV FLASK_APP=app.py
CMD [“wsgi_handler.handler”]
`
wsgi_handler.py
import os
from importlib import import_module
def import_app(config):
module_name, app_name = config['app'].rsplit('.', 1)
module = import_module(module_name)
return getattr(module, app_name)
config = {
'app': 'app.app' # Adjust this path if necessary
}
wsgi_app = import_app(config)
def handler(event, context):
return wsgi_app(event, context)
I need to fix the import error, I have tried with different method to import the app in app.py but failed.