logandk/serverless-wsgi

[Question] Possible reasons of getting Unable to import core.wsgi.application with Django app?

ShahriyarR opened this issue · 2 comments

Got the following error from the API Gateway:

Wed Aug 25 14:39:51 UTC 2021 : Endpoint response body before transformations: 
{"errorMessage": "Unable to import core.wsgi.application", "errorType": "Exception", "stackTrace": ["  File \"/var/lang/lib/python3.8/imp.py\", line 234, in load_module\n    return load_source(name, filename, file)\n", "  File \"/var/lang/lib/python3.8/imp.py\", line 171, in load_source\n    module = _load(spec)\n", "  File \"<frozen importlib._bootstrap>\", line 702, in _load\n", "  File \"<frozen importlib._bootstrap>\", line 671, in _load_unlocked\n", "  File \"<frozen importlib._bootstrap_external>\", line 848, in exec_module\n", "  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\n", "  File \"/var/task/wsgi_handler.py\", line 115, in <module>\n    wsgi_app = import_app(config)\n", "  
File \"/var/task/wsgi_handler.py\", line 48, in import_app\n    
raise Exception(\"Unable to import {}\".format(config[\"app\"]))\n"]}
Wed Aug 25 14:39:51 UTC 2021 : Lambda execution failed with status 200 due to customer function error: Unable to import core.wsgi.application. Lambda request id: 47507347-6ca0-4452-a215-e240d4138c1a
Wed Aug 25 14:39:51 UTC 2021 : Method completed with status: 502

serverless.yml file:

service:
  name: CustomLambdaAuth

frameworkVersion: '2'

plugins:
  - serverless-pseudo-parameters
  - serverless-python-requirements
  - serverless-wsgi
  - serverless-dynamodb-local
  - serverless-offline
custom:
  userTable: user
  tableThroughputs:
    dev: "10"
  customProviderName: my.ether.login
  wsgi:
    app: core.wsgi.application
    packRequirements: false
    environment:
      DJANGO_SETTINGS_MODULE: core.settings.local
  pythonRequirements:
    zip: true
    dockerizePip: non-linux
    invalidateCaches: true
    pythonBin: python3
    useDownloadCache: false
    useStaticCache: false
  dynamodb:
    start:
      migrate: true
    stages:
      - ${self:provider.stage}

provider:
  region: ap-southeast-2
  name: aws
  runtime: python3.9
  lambdaHashingVersion: '20201221'
  environment:
    DEVELOPER_PROVIDER_NAME: ${self:custom.customProviderName}
    USERTABLE_NAME: ${self:custom.userTable}
    IDENTITY_POOL_ID: { Ref: CognitoIdentityPool }
    DJANGO_SETTINGS_MODULE: core.settings.local
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "dynamodb:GetItem"
        - "dynamodb:PutItem"
        - "dynamodb:UpdateItem"
        - "dynamodb:ExecuteStatement"
        - "dynamodb:PartiQLSelect"
      Resource:
        - "Fn::GetAtt": [UserTable, Arn]
        - "Fn::Join":
            - "/"
            - - { "Fn::GetAtt": [UserTable, Arn] }
              - "index/*"
    - Effect: "Allow"
      Action:
        - "cognito-identity:GetCredentialsForIdentity"
        - "cognito-identity:GetOpenIdTokenForDeveloperIdentity"
      Resource:
        - "Fn::Join":
            - "/"
            - - "arn:aws:cognito-identity:#{AWS::Region}:#{AWS::AccountId}:identitypool"
              - Ref: CognitoIdentityPool

functions:
  Login: ${file(resources/functions.yml):Login}
  GetNonce: ${file(resources/functions.yml):GetNonce}
  Signup: ${file(resources/functions.yml):Signup}
  Hello: ${file(resources/functions.yml):Hello}

resources:
  Resources:
    UserTable: ${file(resources/tables.yml):UserTable}
    CognitoIdentityPool: ${file(resources/cognito.yml):CognitoIdentityPool}
    CognitoAuthorizedRole: ${file(resources/cognito.yml):CognitoAuthorizedRole}
    CognitoUnAuthorizedRole: ${file(resources/cognito.yml):CognitoUnAuthorizedRole}
    CognitoIdentityPoolRolesMapping: ${file(resources/cognito.yml):CognitoIdentityPoolRolesMapping}

package:
  exclude:
    - node_modules/**
    - venv/**
    - .dynamodb

I am having the exact same issue

I have figured out the reason. In that cases please run the Lambda function directly and see the output.
Here is the error which was shown in API Gateway side:

Test Event Name
test

Response
{
  "errorMessage": "Unable to import core.wsgi.application",
  "errorType": "Exception",
  "stackTrace": [
    "  File \"/var/lang/lib/python3.8/imp.py\", line 234, in load_module\n    return load_source(name, filename, file)\n",
    "  File \"/var/lang/lib/python3.8/imp.py\", line 171, in load_source\n    module = _load(spec)\n",
    "  File \"<frozen importlib._bootstrap>\", line 702, in _load\n",
    "  File \"<frozen importlib._bootstrap>\", line 671, in _load_unlocked\n",
    "  File \"<frozen importlib._bootstrap_external>\", line 843, in exec_module\n",
    "  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\n",
    "  File \"/var/task/wsgi_handler.py\", line 115, in <module>\n    wsgi_app = import_app(config)\n",
    "  File \"/var/task/wsgi_handler.py\", line 48, in import_app\n    raise Exception(\"Unable to import {}\".format(config[\"app\"]))\n"
  ]
}

Again nothing informative.
But in Function logs:

Function Logs
START RequestId: 071448c6-7dc3-4830-ad3d-a20519d7a65b Version: $LATEST
Traceback (most recent call last):
File "/var/task/wsgi_handler.py", line 43, in import_app
wsgi_module = importlib.import_module(wsgi_fqn_parts[-1])
File "/var/lang/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 843, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/var/task/core/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/opt/python/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/opt/python/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/opt/python/django/conf/__init__.py", line 82, in getattr
self._setup(name)
File "/opt/python/django/conf/__init__.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/opt/python/django/conf/__init__.py", line 170, in init
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/var/lang/lib/python3.8/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 843, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/var/task/core/settings/local.py", line 6, in <module>
import('pysqlite3')
ModuleNotFoundError: No module named 'pysqlite3'
[ERROR] Exception: Unable to import core.wsgi.application
Traceback (most recent call last):
  File "/var/lang/lib/python3.8/imp.py", line 234, in load_module
    return load_source(name, filename, file)
  File "/var/lang/lib/python3.8/imp.py", line 171, in load_source
    module = _load(spec)
  File "<frozen importlib._bootstrap>", line 702, in _load
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/task/wsgi_handler.py", line 115, in <module>
    wsgi_app = import_app(config)
  File "/var/task/wsgi_handler.py", line 48, in import_app
    raise Exception("Unable to import {}".format(config["app"]))END RequestId: 071448c6-7dc3-4830-ad3d-a20519d7a65b
REPORT RequestId: 071448c6-7dc3-4830-ad3d-a20519d7a65b  Duration: 1274.90 ms  Billed Duration: 1275 ms  Memory Size: 1024 MB  Max Memory Used: 33 MB  
Unknown application error occurred.

The main error is ->

import('pysqlite3')
ModuleNotFoundError: No module named 'pysqlite3'

But instead of giving actual error it gives generalized error:

[ERROR] Exception: Unable to import core.wsgi.application

That is definitely a problem.