Skip to content

Commit 13e3059

Browse files
committed
feat(plugin): update handling of api gateway timeouts in serverless config
1 parent 003f767 commit 13e3059

File tree

4 files changed

+106
-375
lines changed

4 files changed

+106
-375
lines changed

README.md

Lines changed: 74 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Serverless API Gateway Integration Timeout
22

3-
A Serverless Framework plugin to modify the API Gateway integration timeout based on your AWS account's service quota.
3+
A lightweight Serverless Framework plugin that adds support for setting API Gateway integration timeouts directly in HTTP events.
44

55
## Problem
66

7-
By default, Amazon API Gateway has an integration timeout limit of 29 seconds (29,000 milliseconds). This can be too short for more complex or resource-intensive operations, especially for Generative AI workloads with Large Language Models (LLMs).
7+
By default, Amazon API Gateway has an integration timeout limit of 29 seconds (29,000 milliseconds). This can be too short for more complex or resource-intensive operations.
88

9-
As [announced in June 2024](https://aws.amazon.com/about-aws/whats-new/2024/06/amazon-api-gateway-integration-timeout-limit-29-seconds/), AWS now allows integration timeouts beyond 29 seconds for Regional and Private REST APIs, making it possible to support longer-running backend operations.
9+
While Lambda functions can have longer timeout values (up to 15 minutes), API Gateway will terminate the request if it exceeds its integration timeout. The Serverless Framework doesn't natively support setting this timeout directly in HTTP event definitions.
1010

1111
## Solution
1212

13-
This plugin automatically updates the API Gateway integration timeout after deployment, allowing you to set integration timeout values for your API Gateway based on your account's service quota.
13+
This plugin extends the Serverless Framework's HTTP event schema to include a `timeout` property. It intercepts the API Gateway CloudFormation template generation to add the `TimeoutInMillis` property to your integrations.
1414

1515
## Installation
1616

@@ -24,29 +24,35 @@ serverless plugin install -n serverless-api-gateway-integration-timeout
2424

2525
## Usage
2626

27-
Add the plugin to your `serverless.yml` file:
27+
1. Add the plugin to your `serverless.yml` file:
2828

2929
```yaml
3030
plugins:
3131
- serverless-api-gateway-integration-timeout
3232
```
3333
34-
Configure the timeout in the custom section of your `serverless.yml`:
34+
2. Set the timeout directly in your HTTP events:
3535
3636
```yaml
37-
custom:
38-
apiGatewayIntegrationTimeout: 60000 # Desired timeout in milliseconds
39-
apiGatewayMaxTimeout: 60000 # Maximum timeout allowed by your AWS account's service quota
37+
functions:
38+
myFunction:
39+
handler: handler.myFunction
40+
timeout: 60 # Lambda timeout in seconds
41+
events:
42+
- http:
43+
path: /my-path
44+
method: get
45+
timeout: 60 # API Gateway timeout in seconds (converted to milliseconds)
4046
```
4147
42-
You can also set just the Lambda timeout in the provider section, and the plugin will automatically use that value (converted to milliseconds):
43-
44-
```yaml
45-
provider:
46-
timeout: 60 # Lambda timeout in seconds - will be used as 60000ms for API Gateway
47-
```
48+
## Important Notes
4849
49-
If no timeout is specified, the plugin will default to a timeout of 29,000 milliseconds (29 seconds).
50+
- The `timeout` value in HTTP events is in **seconds**. It will be automatically converted to milliseconds.
51+
- The maximum allowed timeout depends on your AWS account's service quota limit
52+
- Standard limit: 29 seconds (29,000 milliseconds)
53+
- Extended limit: May be increased up to 60 or 120 seconds, depending on your account
54+
- The plugin works with these integration types: `AWS`, `AWS_PROXY`, and `LAMBDA`
55+
- The plugin modifies the CloudFormation template during deployment, so it only works with `serverless deploy`
5056

5157
## Service Quota and Maximum Timeout
5258

@@ -60,67 +66,15 @@ To increase your service quota:
6066
2. Navigate to API Gateway service
6167
3. Request an increase for the "Integration timeout" quota
6268

63-
## Configuration Options
64-
65-
| Option | Description | Default |
66-
|--------|-------------|---------|
67-
| `apiGatewayIntegrationTimeout` | The desired timeout in milliseconds | 29000 or provider.timeout * 1000 |
68-
| `apiGatewayMaxTimeout` | The maximum timeout allowed by your AWS account's service quota | 29000 |
69-
| `apiGatewayId` | Optional: Manually specify your API Gateway ID | - |
70-
71-
## Timeout Order of Precedence
72-
73-
The plugin determines the timeout value to set in the following order:
74-
75-
1. `custom.apiGatewayIntegrationTimeout` if defined
76-
2. `provider.timeout` * 1000 (converting from seconds to milliseconds) if defined
77-
3. Default value of 29000ms if neither of the above is defined
78-
79-
## Troubleshooting
80-
81-
### Error: Timeout should be between 50 ms and 60000 ms
82-
83-
This error occurs when your requested timeout exceeds your account's service quota. To resolve this:
84-
85-
1. Check your current service quota in the AWS console
86-
2. Set the `apiGatewayMaxTimeout` parameter to match your account's limit:
87-
88-
```yaml
89-
custom:
90-
apiGatewayIntegrationTimeout: 60000 # Your desired timeout
91-
apiGatewayMaxTimeout: 60000 # Set to your account's maximum limit
92-
```
93-
94-
The plugin will automatically adjust your timeout if it exceeds the maximum value.
69+
## How It Works
9570

96-
### Error: Could not find REST API ID
71+
The plugin works by:
9772

98-
If you encounter the error `Could not find REST API ID in CloudFormation stack outputs`, this usually happens when:
73+
1. Extending the Serverless Framework's HTTP event schema to include a `timeout` property
74+
2. Finding the API Gateway compiler plugin instance
75+
3. Monkey-patching the `getMethodIntegration` method to include the `TimeoutInMillis` property when generating the CloudFormation template
9976

100-
1. You're using multiple API Gateway instances
101-
2. You're using a custom naming convention for your API Gateway
102-
3. Your serverless deployment doesn't use CloudFormation outputs for the API Gateway
103-
104-
The plugin will attempt to find your API Gateway by:
105-
1. First checking the CloudFormation stack outputs for the API ID
106-
2. If not found, it will try to find the API Gateway by its name (service-stage)
107-
108-
If it still cannot find your API Gateway, you can manually specify the API ID in your serverless.yml:
109-
110-
```yaml
111-
custom:
112-
apiGatewayIntegrationTimeout: 60000 # Desired timeout
113-
apiGatewayId: abcdef123 # Your API Gateway ID
114-
```
115-
116-
## Important Notes
117-
118-
- This plugin only works with full deployments (`serverless deploy`), as it needs to modify the API Gateway after deployment.
119-
- The plugin runs during the `after:deploy:deploy` lifecycle hook.
120-
- The default integration timeout in API Gateway is 29,000 milliseconds (29 seconds).
121-
- Integration timeouts above 29 seconds require a service quota increase for your AWS account.
122-
- The minimum integration timeout allowed by AWS is 50 milliseconds.
123-
- After modifying the timeout, the plugin creates a new deployment to apply the changes.
77+
This approach is lightweight and doesn't require any post-deployment modifications to the API Gateway.
12478

12579
## Example
12680

@@ -133,30 +87,62 @@ provider:
13387
name: aws
13488
runtime: nodejs18.x
13589
region: us-east-1
136-
timeout: 60 # Lambda timeout in seconds - will also be used for API Gateway
13790
13891
plugins:
13992
- serverless-api-gateway-integration-timeout
14093
141-
custom:
142-
# Explicitly set API Gateway timeout if needed
143-
# apiGatewayIntegrationTimeout: 60000 # 60 seconds
144-
145-
# Set the maximum allowed by your account's service quota
146-
apiGatewayMaxTimeout: 60000 # Your account's limit
147-
148-
# Optional: Specify API Gateway ID manually if needed
149-
# apiGatewayId: abcdef123
150-
15194
functions:
152-
processData:
153-
handler: handler.processData
95+
processingFunction:
96+
handler: handler.process
15497
timeout: 60 # Lambda timeout in seconds
15598
events:
15699
- http:
157100
path: /process
158101
method: post
159-
cors: true
102+
integration: AWS_PROXY
103+
timeout: 60 # API Gateway timeout in seconds
104+
105+
reportFunction:
106+
handler: handler.report
107+
timeout: 29 # Lambda timeout in seconds
108+
events:
109+
- http:
110+
path: /report/{id}
111+
method: get
112+
timeout: 29 # API Gateway timeout in seconds
113+
```
114+
115+
Example `handler.js`:
116+
117+
```javascript
118+
'use strict';
119+
120+
module.exports.process = async (event) => {
121+
// This function can run for up to 60 seconds
122+
// API Gateway will wait for up to 60 seconds for a response
123+
await new Promise(resolve => setTimeout(resolve, 50000));
124+
125+
return {
126+
statusCode: 200,
127+
body: JSON.stringify({
128+
message: 'Process completed successfully',
129+
}),
130+
};
131+
};
132+
133+
module.exports.report = async (event) => {
134+
// This function can run for up to 29 seconds
135+
// API Gateway will wait for up to 29 seconds for a response
136+
await new Promise(resolve => setTimeout(resolve, 25000));
137+
138+
return {
139+
statusCode: 200,
140+
body: JSON.stringify({
141+
message: 'Report generated successfully',
142+
reportId: event.pathParameters.id,
143+
}),
144+
};
145+
};
160146
```
161147

162148
## License

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
22

3-
const UpdateAPIGatewayIntegrationTimeout = require('./src/updateAPIGatewayIntegrationTimeout');
3+
const IntegrationTimeout = require('./src/updateAPIGatewayIntegrationTimeout');
44

5-
module.exports = UpdateAPIGatewayIntegrationTimeout;
5+
module.exports = IntegrationTimeout;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "serverless-api-gateway-integration-timeout",
3-
"version": "1.0.7",
4-
"description": "Serverless Framework plugin to update API Gateway integration timeout based on AWS service quota",
3+
"version": "2.0.0",
4+
"description": "Serverless Framework plugin to set API Gateway integration timeout directly in http events",
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)