admin管理员组

文章数量:1309971

As I read here it's possible to extend the API Gateway integration timeout beyond the default 29-second limit. Following this, I increased the timeout to 60 seconds for my endpoints. However, despite the fact that my task executes within 40-45 seconds, I am still encountering a 504 Gateway Timeout error with the message:

{ "message": "Endpoint request timed out" }

Here’s the setup I'm using to reproduce the issue:

requirements.txt

fastapi
mangum
uvicorn

main.py

import time
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from mangum import Mangum

app = FastAPI()
handler = Mangum(app)

@app.get("/test_25")
async def test_25() -> JSONResponse:
    try:
        time.sleep(25)
        return JSONResponse(content="Result After 25 Seconds :)")
    except Exception as e:
        print(e)
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/test_40")
async def test_40() -> JSONResponse:
    try:
        time.sleep(40)
        return JSONResponse(content="Result After 40 Seconds :)")
    except Exception as e:
        print(e)
        raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app)

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  FastAPILambda:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: python3.10
      CodeUri: .
      Handler: main.handler
      Timeout: 60
      Events:
        Test25:
          Type: Api
          Properties:
            Path: /test_25
            Method: GET
            RestApiId: !Ref FastAPIGateway
        Test40:
          Type: Api
          Properties:
            Path: /test_40
            Method: GET
            RestApiId: !Ref FastAPIGateway

  FastAPIGateway:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      DefinitionBody:
        openapi: "3.0.1"
        info:
          title: "FastAPIGateway"
          version: "1.0"
        paths:
          /test_25:
            get:
              x-amazon-apigateway-integration:
                httpMethod: POST
                type: aws_proxy
                uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${FastAPILambda.Arn}/invocations"
                timeoutInMillis: 60000
          /test_40:
            get:
              x-amazon-apigateway-integration:
                httpMethod: POST
                type: aws_proxy
                uri: !Sub "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${FastAPILambda.Arn}/invocations"
                timeoutInMillis: 60000

Outputs:
  ApiEndpoint:
    Description: "API Gateway endpoint URL"
    Value: !Sub "https://${FastAPIGateway}.execute-api.${AWS::Region}.amazonaws/Prod/"

Testing the API:
To test the API, I created the following script to call both the /test_25 and /test_40 endpoints:

import requests

def test_25():
    response = requests.get(f"{BASE_URL}/test_25")
    print(response.status_code)
    print(response.json())

def test_40():
    response = requests.get(f"{BASE_URL}/test_40")
    print(response.status_code)
    print(response.json())

if __name__ == "__main__":

    # BASE_URL = "http://localhost:8000"
    BASE_URL = ";

    test_25()
    test_40()

Output:

For the /test_25 endpoint, I get the following output:

200
Result After 25 Seconds :)

For the /test_40 endpoint, I get:

504

This indicates that the /test_25 endpoint works fine, but the /test_40 endpoint is still timing out.

Any Help is Appreciated :)

本文标签: pythonAWS API Gateway Error 504quotmessagequot quotEndpoint request timed outquot Stack Overflow