Skip to content

Commit ac5b064

Browse files
authored
feat: add TimeoutInMillis to HttpApi events (#1450)
1 parent 8121fcc commit ac5b064

File tree

7 files changed

+57
-3
lines changed

7 files changed

+57
-3
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ class HttpApi(PushEventSource):
935935
"ApiId": PropertyType(False, is_str()),
936936
"Stage": PropertyType(False, is_str()),
937937
"Auth": PropertyType(False, is_type(dict)),
938+
"TimeoutInMillis": PropertyType(False, is_type(int)),
938939
}
939940

940941
def resources_to_link(self, resources):
@@ -1064,6 +1065,8 @@ def _add_openapi_integration(self, api, function, manage_swagger=False):
10641065
editor.add_lambda_integration(self.Path, self.Method, uri, self.Auth, api.get("Auth"), condition=condition)
10651066
if self.Auth:
10661067
self._add_auth_to_openapi_integration(api, editor)
1068+
if self.TimeoutInMillis:
1069+
editor.add_timeout_to_method(api=api, path=self.Path, method_name=self.Method, timeout=self.TimeoutInMillis)
10671070
api["DefinitionBody"] = editor.openapi
10681071

10691072
def _add_auth_to_openapi_integration(self, api, editor):

samtranslator/open_api/open_api.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,20 @@ def iter_on_path(self):
236236
for path, value in self.paths.items():
237237
yield path
238238

239+
def add_timeout_to_method(self, api, path, method_name, timeout):
240+
"""
241+
Adds a timeout to this path/method.
242+
243+
:param dict api: Reference to the related Api's properties as defined in the template.
244+
:param string path: Path name
245+
:param string method_name: Method name
246+
:param int timeout: Timeout amount, in milliseconds
247+
"""
248+
normalized_method_name = self._normalize_method_name(method_name)
249+
for method_definition in self.get_method_contents(self.get_path(path)[normalized_method_name]):
250+
if self.method_definition_has_integration(method_definition):
251+
method_definition[self._X_APIGW_INTEGRATION]["timeoutInMillis"] = timeout
252+
239253
def add_authorizers_security_definitions(self, authorizers):
240254
"""
241255
Add Authorizer definitions to the securityDefinitions part of Swagger.

tests/translator/input/http_api_existing_openapi.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Parameters:
2+
Timeout:
3+
Default: 15000
4+
Type: Number
15
Resources:
26
HttpApiFunction:
37
Type: AWS::Serverless::Function
@@ -13,10 +17,12 @@ Resources:
1317
Path: /basic
1418
Method: post
1519
ApiId: !Ref MyApi
20+
TimeoutInMillis: 10000
1621
SimpleCase: # path exists, integration doesn't
1722
Type: HttpApi
1823
Properties:
1924
ApiId: !Ref MyApi
25+
TimeoutInMillis: !Ref Timeout
2026
MyApi:
2127
Type: AWS::Serverless::HttpApi
2228
Properties:

tests/translator/output/aws-cn/http_api_existing_openapi.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
2+
"Parameters": {
3+
"Timeout": {
4+
"Default": 15000,
5+
"Type": "Number"
6+
}
7+
},
28
"Resources": {
39
"HttpApiFunctionSimpleCasePermission": {
410
"Type": "AWS::Lambda::Permission",
@@ -100,6 +106,7 @@
100106
"post": {
101107
"x-amazon-apigateway-integration": {
102108
"httpMethod": "POST",
109+
"timeoutInMillis": 10000,
103110
"type": "aws_proxy",
104111
"uri": {
105112
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${DifferentFunction.Arn}/invocations"
@@ -151,6 +158,9 @@
151158
"x-amazon-apigateway-integration": {
152159
"httpMethod": "POST",
153160
"type": "aws_proxy",
161+
"timeoutInMillis": {
162+
"Ref": "Timeout"
163+
},
154164
"uri": {
155165
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
156166
},

tests/translator/output/aws-us-gov/http_api_existing_openapi.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
2+
"Parameters": {
3+
"Timeout": {
4+
"Default": 15000,
5+
"Type": "Number"
6+
}
7+
},
28
"Resources": {
39
"HttpApiFunctionSimpleCasePermission": {
410
"Type": "AWS::Lambda::Permission",
@@ -100,6 +106,7 @@
100106
"post": {
101107
"x-amazon-apigateway-integration": {
102108
"httpMethod": "POST",
109+
"timeoutInMillis": 10000,
103110
"type": "aws_proxy",
104111
"uri": {
105112
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${DifferentFunction.Arn}/invocations"
@@ -151,6 +158,9 @@
151158
"x-amazon-apigateway-integration": {
152159
"httpMethod": "POST",
153160
"type": "aws_proxy",
161+
"timeoutInMillis": {
162+
"Ref": "Timeout"
163+
},
154164
"uri": {
155165
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
156166
},

tests/translator/output/http_api_existing_openapi.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{
2+
"Parameters": {
3+
"Timeout": {
4+
"Default": 15000,
5+
"Type": "Number"
6+
}
7+
},
28
"Resources": {
39
"HttpApiFunctionSimpleCasePermission": {
410
"Type": "AWS::Lambda::Permission",
@@ -99,7 +105,8 @@
99105
"/basic": {
100106
"post": {
101107
"x-amazon-apigateway-integration": {
102-
"httpMethod": "POST",
108+
"httpMethod": "POST",
109+
"timeoutInMillis": 10000,
103110
"type": "aws_proxy",
104111
"uri": {
105112
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${DifferentFunction.Arn}/invocations"
@@ -147,9 +154,12 @@
147154
]
148155
}
149156
],
150-
"isDefaultRoute": true,
157+
"isDefaultRoute": true,
151158
"x-amazon-apigateway-integration": {
152-
"httpMethod": "POST",
159+
"httpMethod": "POST",
160+
"timeoutInMillis": {
161+
"Ref": "Timeout"
162+
},
153163
"type": "aws_proxy",
154164
"uri": {
155165
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"

versions/2016-10-31.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ Path | `string` | Uri path for which this function is invoked. MUST start with `
651651
Method | `string` | HTTP method for which this function is invoked.
652652
ApiId | `string` | Identifier of a HttpApi resource which MUST contain an operation with the given path and method. Typically, this is set to [reference](http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html) an `AWS::Serverless::HttpApi` resource defined in this template. If not defined, a default `AWS::Serverless::HttpApi` resource is created using a generated OpenApi document contains a union of all paths and methods defined by `HttpApi` events defined in this template that do not specify an ApiId.
653653
Auth | [Function Auth Object](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-httpapifunctionauth.html) | Auth configuration for this specific Api+Path+Method. Useful for overriding the API's `DefaultAuthorizer` setting auth config on an individual path when no `DefaultAuthorizer` is specified.
654+
TimeoutInMillis | `int` | Custom timeout between 50 and 29,000 milliseconds. The default value is 5,000 milliseconds, or 5 seconds for HTTP APIs.
654655

655656
##### Example: HttpApi event source object
656657

0 commit comments

Comments
 (0)