Skip to content

How do I overcome API Gateway Integration Timeout Constraints with Response Streaming

7 minute read
Content level: Intermediate
0

I want to Configure response streaming Amazon API Gateway REST API to avoid the 29-second integration timeout constraint and support backend operations for up to 15 minutes.

Short description

As the default integration timeout for Amazon API Gateway REST APIs is 29 seconds and if the backend processing takes longer than this, API Gateway returns an HTTP 504 error to the client. Regional and private REST APIs support requesting a timeout increase through Service Quotas, but increasing the integration timeout beyond 29 seconds is not currently supported for edge-optimized REST APIs.

Resolution

As API Gateway now offers response streaming on REST APIs, it provides an alternative approach. Instead of waiting for the backend to finish processing before sending a response, API Gateway forwards rese chunks to the client as your backend produces them. This keeps the connection alive if data continues to flow — supporting up to 15 minutes of backend processing with supported integration types, across all REST API endpoint types, with no quota change required for integration timeout.

Note: Response streaming is supported on REST APIs with Lambda functions, HTTP proxy integrations, and private integrations. It is not supported on HTTP APIs or WebSocket APIs as of now.

Configuring response streaming on a REST API with a Lambda proxy integration.

Step 1: Set up IAM permissions.

When creating a Lambda proxy integration using the API Gateway console, the console automatically adds the required resource-based policy to your Lambda function. No additional IAM configuration is needed.

If you are using the AWS CLI, AWS CloudFormation, or an SDK, ensure that API Gateway has permission to invoke your Lambda function. You can do this in one of two ways:

• Resource-based policy: Add a resource-based policy to your Lambda function that grants lambda:InvokeFunction permission to API Gateway.
• IAM execution role: Create an IAM role with lambda:InvokeFunction permission and specify the role ARN in the integration credentials field when creating the integration. For more information, see Control access to a REST API with IAM permissions.

Step 2: Create a streaming Lambda function

To use response streaming, in this example we are using awslambda.streamifyResponse wrapper. This wrapper allows your function to write data incrementally to the response stream instead of returning a single response at the end.

  1. Open the Lambda console.
  2. Choose Create function.
  3. For Function name, enter a name for your function.
  4. For Runtime, select Node.js 22.x.
  5. Choose Create function.

In the Code editor, replace the default code with the following:

export const handler = awslambda.streamifyResponse( 
 async (event, responseStream, context) => { 
   responseStream = awslambda.HttpResponseStream.from(responseStream, { 
     statusCode: 200, 
     headers: { "Content-Type": "text/plain" } 
   }); 
  for (let i = 0; i < 10; i++) { 
     responseStream.write("Step " + (i + 1) + " of 10\n"); 
     await new Promise(r => setTimeout(r, 5000)); 
   } 
 
  responseStream.write("Complete."); 
   responseStream.end(); 
 } 
);  


Note: Your Lambda function must adhere to the Lambda proxy integration format for response streaming. The HttpResponseStream.from() method used in this example handles the required format automatically. For more information about the required output format, see Set up a Lambda proxy integration with payload response streaming.

This example writes a progress update every 5 seconds, simulating a long-running operation. The HttpResponseStream.from() method sets the HTTP status code and headers before streaming begins. The streamifyResponse wrapper handles the streaming protocol with API Gateway automatically.

  1. Choose Deploy.
  2. Choose the Configuration tab, then General configuration.
  3. Choose Edit.
  4. For Timeout, enter a value that covers your full processing duration (for example, 120 seconds).
  5. Choose Save.

Step 3: Configure response streaming on your REST API

Configure response streaming based on whether you are creating a new integration or updating an existing one.

Option A: Create a new resource and method with response streaming.

If you are setting up a new resource and method, you can configure response streaming and the integration timeout together in a single step.

  1. Open the API Gateway console.
  2. Select your REST API.
  3. In the Resources pane, choose Create resource.
  4. For Resource name, enter a name (for example, stream).
  5. Choose Create resource.
  6. Select the new resource and choose Create method.
  7. For Method type, select your HTTP method (for example, POST).
  8. For Integration type, select Lambda function.
  9. Turn on Lambda proxy integration.
  10. Select the Lambda function you created in Step 2.
  11. (Optional) If you want to use an IAM execution role instead of the default resource-based policy, specify the role ARN in the Execution role field.
  12. For Response transfer mode, select Stream.
  13. For Integration timeout, enter the timeout value in milliseconds (for example, 900000 for 15 minutes).
  14. Choose Create method.

Option B: Update an existing method to enable response streaming If you are updating an existing integration, this requires two separate saves. You must first save the response transfer mode change with the default timeout, then update the integration timeout value in a second edit.

  1. Open the API Gateway console.
  2. Select your REST API.
  3. In the Resources pane, select the existing resource and method.
  4. Choose the Integration request tab.
  5. Choose Edit.
  6. For Response transfer mode, select Stream.
  7. Keep the default integration timeout value.
  8. Choose Save.
  9. Choose Edit again on the integration request.
  10. For Integration timeout, update the value in milliseconds as needed (for example, 900000 for 15 minutes).
  11. Choose Save.
  12. Redeploy your API.

Note: When you set the response transfer mode to Stream, API Gateway uses the InvokeWithResponseStream API to call your Lambda function instead of the standard Invoke API. For more information, see Configure a Lambda proxy integration with payload response streaming in the API Gateway documentation.

Step 4: Test the streaming response

After deploying your API, verify that response streaming is working correctly by confirming that data arrives progressively during the request rather than all at once at the end.

  1. Deploy your API to a stage.
  2. Send a request to your endpoint.
  3. Verify that response data arrives progressively as each step completes, rather than all at once after processing finishes.

Note: Streaming responses from Regional or private endpoints have a 5-minute idle timeout. Edge-optimized endpoints have a 30-second idle timeout. Your backend must send data within these intervals to keep the connection alive.

Note: Once streaming begins, the HTTP status code is sent immediately and cannot be changed. Validate inputs before writing the first chunk.

When to request a timeout increase instead.

Response streaming requires your backend to produce incremental output. If your backend processes the entire request before returning a single response and cannot be adapted to write chunks, response streaming is not the right fit. In that case, you can request a timeout increase for Regional and private REST APIs through Service Quotas. This option is not currently available for edge-optimized REST APIs.

For more information, see Increase the API Gateway integration timeout limit.

Conclusion

Response streaming extends the backend processing time for your REST API to up to 15 minutes without requiring a timeout limit increase. Whether you are building generative AI applications, processing large datasets, or running long-running operations, response streaming provides a direct path to overcome integration timeout constraints. This capability is especially beneficial for edge-optimized REST APIs, where timeout increases are not currently available, making response streaming the recommended approach for that endpoint type.

Related Resource:

  1. Configure a Lambda proxy integration with payload response streaming
  2. Increase the API Gateway integration timeout limit
  3. Building responsive APIs with Amazon API Gateway response streaming
  4. Stream the integration response for your proxy integrations in API Gateway
  5. Set up a Lambda proxy integration with payload response streaming