AWS lambda: Unable to import application.application

Hi All,
I am new to serverless and I was using serverless to deploy a python flask application to AWS Lambda function with following instruction: https://serverless.com/blog/flask-python-rest-api-serverless-lambda-dynamodb/#converting-an-existing-flask-application. However I was getting following error

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

There is the serverless.yml file:
service: chromeagentapiservices

plugins:
  - serverless-python-requirements
  - serverless-wsgi

custom:
  wsgi:
    app: application.application
    packRequirements: false
  pythonRequirements:
    dockerizePip: non-linux

provider:
  name: aws
  runtime: python3.7
  stage: dev
  region: ap-southeast-2
  service: execute-api

functions:
  app:
    handler: wsgi.handler
    environment: 
      AppId: A string here
      Chrome_DB: another string here
    events:
      - http: ANY /
      - https: 'ANY{proxy+}'

Did I configure it correctly? Why does this issue happens?

FYI, I was building the app on Ubuntu

Were you able to resolve this?

Can someone know if this error is resolved

So I was able to successfully deploy the lambda flask API with the above configuration.
There are couple of issues that was in my case, not sure if its the same thing with your case as well. Just incase if others stumble upon, they may find it useful

So the error - Unable to import application.application - predominantly means the app can be successfully deployed but your flask server cannot be started.

In your code, please remove any thing related to app.run . This is not needed, serverless-wsgi plugin takes care of it automatically. You shouldn’t start the flask app, leave it to the lambda to take that decision

There were also several environment variables which I forgot to initialise. My app is designed to be database action centric and if I cannot establish a connection, I retry and fail. The error message wasnt appropriate, but nevertheless I forgot to mention database related environment /secrets and that caused the issue as well.

1 Like

Can anyone give more context on the cause of this error & how to resolve it? I’m having the same error when anyone on my team (other than me) tries to deploy our Serverless API. I’m the only person who can deploy, so this is a huge blocker. Been stuck on this issue for months.