Skip to content

Commit

Permalink
feat(apigateway): integration timeout (#14154)
Browse files Browse the repository at this point in the history
Add an option to set a custom timeout when adding a RestApi integration.

fixes #14123


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
Samrose-Ahmed authored Apr 16, 2021
1 parent a1d0164 commit d02770e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
14 changes: 13 additions & 1 deletion packages/@aws-cdk/aws-apigateway/lib/integration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as iam from '@aws-cdk/aws-iam';
import { Lazy } from '@aws-cdk/core';
import { Lazy, Duration } from '@aws-cdk/core';
import { Method } from './method';
import { IVpcLink, VpcLink } from './vpc-link';

Expand Down Expand Up @@ -80,6 +80,14 @@ export interface IntegrationOptions {
*/
readonly requestTemplates?: { [contentType: string]: string };

/**
* The maximum amount of time an integration will run before it returns without a response.
* Must be between 50 milliseconds and 29 seconds.
*
* @default Duration.seconds(29)
*/
readonly timeout?: Duration;

/**
* The response that API Gateway provides after a method's backend completes
* processing a request. API Gateway intercepts the response from the
Expand Down Expand Up @@ -193,6 +201,10 @@ export class Integration {
if (options.connectionType === ConnectionType.INTERNET && options.vpcLink !== undefined) {
throw new Error('cannot set \'vpcLink\' where \'connectionType\' is INTERNET');
}

if (options.timeout && !options.timeout.isUnresolved() && (options.timeout.toMilliseconds() < 50 || options.timeout.toMilliseconds() > 29000)) {
throw new Error('Integration timeout must be between 50 milliseconds and 29 seconds.');
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-apigateway/lib/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export class Method extends Resource {
connectionType: options.connectionType,
connectionId: options.vpcLink ? options.vpcLink.vpcLinkId : undefined,
credentials,
timeoutInMillis: options.timeout?.toMilliseconds(),
};
}

Expand Down
46 changes: 46 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,50 @@ describe('integration', () => {
},
});
});

test('validates timeout is valid', () => {

expect(() => new apigw.Integration({
type: apigw.IntegrationType.HTTP_PROXY,
integrationHttpMethod: 'ANY',
options: {
timeout: cdk.Duration.millis(2),
},
})).toThrow(/Integration timeout must be between 50 milliseconds and 29 seconds/);

expect(() => new apigw.Integration({
type: apigw.IntegrationType.HTTP_PROXY,
integrationHttpMethod: 'ANY',
options: {
timeout: cdk.Duration.seconds(50),
},
})).toThrow(/Integration timeout must be between 50 milliseconds and 29 seconds/);
});

test('sets timeout', () => {

// GIVEN
const stack = new cdk.Stack();
const api = new apigw.RestApi(stack, 'restapi');

// WHEN
const integration = new apigw.Integration({
type: apigw.IntegrationType.HTTP_PROXY,
integrationHttpMethod: 'ANY',
options: {
timeout: cdk.Duration.seconds(1),
},
});
api.root.addMethod('ANY', integration);

// THEN
expect(stack).toHaveResourceLike('AWS::ApiGateway::Method', {
HttpMethod: 'ANY',
Integration: {
TimeoutInMillis: 1000,
},
});

});

});

0 comments on commit d02770e

Please sign in to comment.