Python, Serverless...Getting "Unable to import app.app" when sending a request to lambda AWS Endpoint

Viewed 18

i'm new to programming and trying to make a Serverless Service with Lambda. It deploys just fine and with a Local Server, it works, but when is deployed and I send a request (to any route, any method), it says "message": "Internal Server Error". When cheking the logs the error in tne title shows...

here's my app.py file:

import json
from urllib import request
from flask import Flask, jsonify, make_response, request
import requests


app = Flask(__name__)


@app.route("/")
def hello_from_root():
    return jsonify(message='Hello from gabadiea1!')


@app.route("/hello")
def hello():
    return jsonify(message='Hello from paths!')


@app.errorhandler(404)
def resource_not_found():
    return make_response(jsonify(error='Not found!'), 404)

@app.route('/Ct', methods=['POST'])
def process_json():
    content_type = request.headers.get('Content-Type')
    
    ct_data = request.json
    
    ct_id = str(ct_data[0]["event"]["parent"]["resource_id"])
    movement_id = (ct_data[0]["event"]["resource_id"])
    
    print(movement_id)
    
    
    url_ct = "https://defensoriasalud.thecasetracking.com/ct/cases/" + ct_id

    headers_ct = {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "AUTHTOKEN": "xxxx"
}

    requestct = requests.get(url_ct, headers=headers_ct)
    ct_causa = requestct.json()
    
    print (ct_causa)
    return jsonify(message='Funcionó!')
        

My serverless.yml:

org: gabadie
app: integracion-streak-api
service: TEST11

frameworkVersion: '3'

custom:
  wsgi:
    app: app.py
    packRequirements: false

provider:
  name: aws
  runtime: python3.8

functions:
  api:
    handler: wsgi_handler.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'

plugins:
  - serverless-wsgi
  - serverless-python-requirementsset FLASK_APP=Scripts\app.py
  

And the full error:

[ERROR] Exception: Unable to import app.app Traceback (most recent call last):   File "/var/task/serverless_sdk/__init__.py", line 144, in wrapped_handler     return user_handler(event, context)   File "/var/task/s_api.py", line 25, in error_handler     raise e   File "/var/runtime/bootstrap.py", line 127, in handle_event_request     response = request_handler(event, lambda_context)   File "/var/task/serverless_sdk/__init__.py", line 144, in wrapped_handler     return user_handler(event, context)   File "/var/task/s_api.py", line 25, in error_handler     raise e   File "/var/task/s_api.py", line 20, in <module>     user_handler = serverless_sdk.get_user_handler('wsgi_handler.handler')   File "/var/task/serverless_sdk/__init__.py", line 56, in get_user_handler     user_module = import_module(user_module_name)   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/wsgi_handler.py", line 119, in <module>     wsgi_app = import_app(config)   File "/var/task/wsgi_handler.py", line 49, in import_app     raise Exception("Unable to import {}".format(config["app"]))```

Thanks in advance!

1 Answers

You need to use mangum to make a handler:

from mangum import Mangum

... Your code ...


handler = Mangum(app=app)
Related