pythonamazon-web-servicesflaskaws-lambdazappa

'Flask' object is not iterable | AWS lambda using zappa


I am trying to deploy a Flask app on AWS lambda using zappa. (I am very new to this so if I need to provide more relevant information, please ask!)

The Flask app has this directory structure:

backend
├── __init__.py
├── __pycache__
├── app.py
├── databases
├── home.py
├── static
├── templates
└── utils.py

Where, the app is being instantiated by app.py like so:

import os
from flask import Flask as FlaskApp
from flask_cors import CORS
import boto3
import logging
from backend import home

def initialize_app(test_config=None, *args, **kwargs):

    print("MADE IT HERE TOO")

    # create and configure the app
    app = FlaskApp(__name__, instance_relative_config=True)

    # Set up logging
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    logger.info("Boto3 Version: %s", boto3.__version__)

    print("APP HAS REACHED THIS POINT")       

    s3_client = boto3.client('s3')

    print("Here too...")

    # Example of logging
    try:
        response = s3_client.list_buckets()
        logger.info("S3 Buckets: %s", response['Buckets'])
    except Exception as e:
        logger.error("Error accessing S3: %s", str(e))

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    print("made it here before os makedirs")

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    print("made it here before home")

    app.register_blueprint(home.bp)

    print("made it here after home")

    return app

... the home blueprint is just a python file with lots of functions in it. Some are routed, some are not. But importantly, something is always routed to root '/' like this:

bp = Blueprint('home', __name__, url_prefix='/')

@bp.route('/')
def index():
    return "Welcome to the Home Page!"

I am using these zapper settings to deploy the app in AWS lambda:

{
"dev": {
    "app_function": "backend.app.initialize_app",
    "aws_region": "af-south-1",
    "exclude": [
        "boto3",
        "dateutil",
        "botocore",
        "s3transfer",
        "concurrent",
        "node_modules",
        "frontend",
        "awsTests"
    ],
    "profile_name": "default",
    "project_name": "backend",
    "runtime": "python3.10",
    "s3_bucket": "zappa-htprl75eu",
    "slim_handler": true
}

}

Nothing crazy there.

In the CloudWatch logs I see all the print statements correctly displayed. However, the lambda instance then returns an error:

'Flask' object is not iterable

AWS CloudWatch report logs showing the error

Can anyone help me identify why this error may be occurring?

I have checked my home.py file for anything which may be calling app but it does not. So I can only think that zappa must be using the returned app from initialize_app() in a way which triggers this error.


If it helps, I am using python version 3.10.6 and these are the packages in my requirements.txt file, one level above backend:

argcomplete==3.5.1
blinker==1.7.0
boto3==1.35.32
botocore==1.35.32
certifi==2024.8.30
cffi==1.17.1
cfn-flip==1.3.0
charset-normalizer==3.3.2
click==8.1.7
cryptography==43.0.1
durationpy==0.9
Flask==3.0.2
Flask-Cors==4.0.0
hjson==3.1.0
idna==3.10
itsdangerous==2.1.2
Jinja2==3.1.3
jmespath==1.0.1
kappa==0.6.0
MarkupSafe==2.1.5
pdf2image==1.17.0
pillow==10.3.0
placebo==0.9.0
pycparser==2.22
PyJWT==2.9.0
PyMuPDF==1.24.2
PyMuPDFb==1.24.1
python-dateutil==2.9.0.post0
python-slugify==8.0.4
PyYAML==6.0.2
requests==2.32.3
s3transfer==0.10.2
six==1.16.0
text-unidecode==1.3
toml==0.10.2
tqdm==4.66.5
troposphere==4.8.3
urllib3==2.2.3
Werkzeug==3.0.1
zappa==0.59.0

Solution

  • According to the Zappa documentation, the app_function setting needs to be a reference to your Flask app object e.g. mymodule.app, rather than a reference to a function that returns your Flask app object.

    So, you can convert the initialize_app function into inline, module-level code that initializes the app object and then change the configuration to:

    "app_function": "backend.app"