简体   繁体   English

使用圣杯部署“端点请求超时”

[英]"Endpoint request timed out" with chalice deploy

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".此代码在使用 'chalice local' 部署时工作正常,但是当我使用 'chalice deploy' 部署它并向端点发送发布请求时,我收到一个状态:504 网关超时和消息:“端点请求超时”。

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.网关超时,因为 lambda 在 30 秒超时内没有响应。

It is probably a problem connecting with the database (ip blocked or similar).这可能是与数据库连接的问题(ip 被阻止或类似)。

  1. Remove the initiation of the database from the lambda and create a health check endpoint.从 lambda 中删除数据库的启动并创建健康检查端点。

  2. If the health check endpoint works, your database may be silently dropping your attempts to connect.如果运行状况检查端点有效,您的数据库可能会默默地放弃您的连接尝试。

  3. Open database connections from any IP从任何 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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM