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
}
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).
Remove the initiation of the database from the lambda and create a health check endpoint.
If the health check endpoint works, your database may be silently dropping your attempts to connect.
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}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.