Skip to content

table_catalog error when deploying on AWS Lambda #97

@dvd3v

Description

@dvd3v

I'm trying to run TiMVT in AWS Lambda + Aurora (based on https://github.com/developmentseed/serverless-timvt).
This is how my application looks like:

from timvt.db import close_db_connection, connect_to_db, register_table_catalog
from timvt.factory import VectorTilerFactory
from timvt.layer import FunctionRegistry
from fastapi import FastAPI, Request

# Create Application.
app = FastAPI()

# Add Function registry to the application state
app.state.timvt_function_catalog = FunctionRegistry()


# Register Start/Stop application event handler to setup/stop the database connection
# and populate `app.state.table_catalog`
@app.on_event("startup")
async def startup_event():
    """Application startup: register the database connection and create table list."""
    await connect_to_db(app)
    await register_table_catalog(
        app
    )


@app.on_event("shutdown")
async def shutdown_event():
    """Application shutdown: de-register the database connection."""
    await close_db_connection(app)

# Register endpoints.
mvt_tiler = VectorTilerFactory(
    with_tables_metadata=True,
    with_functions_metadata=True,  # add Functions metadata endpoints (/functions.json, /{function_name}.json)
    with_viewer=True,
)
app.include_router(mvt_tiler.router, tags=["Tiles"])

Note; i've added the register_table_catalog function so it loads the tables from the db.
Everything works fine locally, also using the Aurora database. However when I deploy the application to AWS Lambda I run into a few problems. I'm using the geolambda layers from https://github.com/lambgeo/geo-layer.

When using Python 3.8 with GDAL 3.2, all the dependencies load correctly, and I'm able to run the app. However when I call the /tables.json endpoint I run into the following error:

[ERROR]	2022-10-12T13:15:46.051Z	9e67c286-5679-44cd-b502-b709978cea4b	An error occurred running the application.
Traceback (most recent call last):
  File "/var/task/starlette/datastructures.py", line 699, in __getattr__
    return self._state[key]
KeyError: 'table_catalog'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/var/task/mangum/protocols/http.py", line 58, in run
    await app(self.scope, self.receive, self.send)
  File "/var/task/fastapi/applications.py", line 270, in __call__
    await super().__call__(scope, receive, send)
  File "/var/task/starlette/applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/var/task/starlette/middleware/errors.py", line 184, in __call__
    raise exc
  File "/var/task/starlette/middleware/errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "/var/task/starlette/middleware/exceptions.py", line 75, in __call__
    raise exc
  File "/var/task/starlette/middleware/exceptions.py", line 64, in __call__
    await self.app(scope, receive, sender)
  File "/var/task/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/var/task/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/var/task/starlette/routing.py", line 680, in __call__
    await route.handle(scope, receive, send)
  File "/var/task/starlette/routing.py", line 275, in handle
    await self.app(scope, receive, send)
  File "/var/task/starlette/routing.py", line 65, in app
    response = await func(request)
  File "/var/task/fastapi/routing.py", line 231, in app
    raw_response = await run_endpoint_function(
  File "/var/task/fastapi/routing.py", line 160, in run_endpoint_function
    return await dependant.call(**values)
  File "/var/task/timvt/factory.py", line 232, in tables_index
    for r in request.app.state.table_catalog
  File "/var/task/starlette/datastructures.py", line 702, in __getattr__
    raise AttributeError(message.format(self.__class__.__name__, key))
AttributeError: 'State' object has no attribute 'table_catalog'

When using a Python 3.7 or 3.9 environment, the application does not load and returns the following error:

[ERROR] Runtime.ImportModuleError: Unable to import module 'handler': No module named 'asyncpg.protocol.protocol'
Traceback (most recent call last):

This could be related to the c-bindings of the asyncpg package.

I cannot reproduce these errors locally, so it has something to do with the AWS Lambda runtime environment.
Any ideas?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions