0

This code works fine when deployed with 'chalice local' but when I deploy it with 'chalice deploy' and send a post request to the endpoint I am greeted with a status: 504 gateway timeout and message: "Endpoint request timed out".

from chalice import Chalice
from sqlalchemy import create_engine

app = Chalice(app_name='demo')
app.debug = True

engine = create_engine('postgresql://postgres:postgres@DATABASE_URI:5432/playground')

@app.route('/', methods=['POST'])
def index():
    req_data = app.current_request.to_dict()
    query_params = req_data['query_params']
    
    name = str(query_params['name'])
    age = int(query_params['age'])

    with engine.connect() as conn:
        conn.execute("INSERT INTO demo VALUES (%s, %s);", (name, age))

    return {
        'message': 'successfully inserted data with:',
        'name': name,
        'age': age
    }
Pritam Banik
  • 121
  • 1
  • 7

1 Answers1

0

The gateway is timing out as lambda is not answering inside 30 seconds timeout.

It is probably a problem connecting with the database (ip blocked or similar).

  1. Remove the initiation of the database from the lambda and create a health check endpoint.

  2. If the health check endpoint works, your database may be silently dropping your attempts to connect.

  3. Open database connections from any IP

Updated code:

   from chalice import Chalice
   from sqlalchemy import create_engine

   app = Chalice(app_name='demo')
   app.debug = True
   @app.route('/', methods=['POST'])
   def index():
     engine = create_engine('postgresql://postgres:postgres@DATABASE_URI:5432/playground')
     req_data = app.current_request.to_dict()
     query_params = req_data['query_params']
    
     name = str(query_params['name'])
     age = int(query_params['age'])

     with engine.connect() as conn:
        conn.execute("INSERT INTO demo VALUES (%s, %s);", (name, age))

     return {
        'message': 'successfully inserted data with:',
        'name': name,
        'age': age
     }

   @app.route('/healthcheck', methods=['POST'])
   def index():
    return {"success": True}
albert
  • 4,290
  • 1
  • 21
  • 42