Skip to content

Commit 58246bf

Browse files
GavinZZhoffa
andauthored
fix: Fix availability dip from API MergeDefinitions property (#3062)
Co-authored-by: Christoffer Rehn <[email protected]>
1 parent d78607b commit 58246bf

File tree

5 files changed

+383
-3
lines changed

5 files changed

+383
-3
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -955,12 +955,15 @@ def _add_swagger_integration( # type: ignore[no-untyped-def] # noqa: too-many-s
955955
)
956956

957957
if merge_definitions:
958-
api["DefinitionBody"] = self._get_merged_definitions(api_id, api["DefinitionBody"], editor.swagger)
958+
api["DefinitionBody"] = self._get_merged_definitions(api_id, api["DefinitionBody"], editor)
959959
else:
960960
api["DefinitionBody"] = editor.swagger
961961

962962
def _get_merged_definitions(
963-
self, api_id: str, source_definition_body: Dict[str, Any], dest_definition_body: Dict[str, Any]
963+
self,
964+
api_id: str,
965+
source_definition_body: Dict[str, Any],
966+
editor: SwaggerEditor,
964967
) -> Dict[str, Any]:
965968
"""
966969
Merge SAM generated swagger definition(dest_definition_body) into inline DefinitionBody(source_definition_body):
@@ -977,7 +980,10 @@ def _get_merged_definitions(
977980

978981
sam_expect(path_method_body, api_id, f"DefinitionBody.paths.{self.Path}.{self.Method}").to_be_a_map()
979982

980-
generated_path_method_body = dest_definition_body["paths"][self.Path][self.Method]
983+
# Normalized version of HTTP Method. It also handle API Gateway specific methods like "ANY"
984+
method = editor._normalize_method_name(self.Method)
985+
dest_definition_body = editor.swagger
986+
generated_path_method_body = dest_definition_body["paths"][self.Path][method]
981987
# this guarantees that the merged definition use SAM generated value for a conflicting key
982988
merged_path_method_body = {**path_method_body, **generated_path_method_body}
983989

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Resources:
2+
WebhooksApi:
3+
Type: AWS::Serverless::Api
4+
Properties:
5+
StageName: live
6+
MergeDefinitions: true
7+
DefinitionBody:
8+
swagger: 2
9+
x-amazon-apigateway-policy:
10+
Version: '2012-10-17'
11+
12+
13+
WebhooksReceiver:
14+
Type: AWS::Serverless::Function
15+
Properties:
16+
CodeUri: s3://bucket/key
17+
Handler: code/handler
18+
Runtime: python3.8
19+
Events:
20+
AllEvent:
21+
Type: Api
22+
Properties:
23+
RestApiId: !Ref WebhooksApi
24+
Path: /proxy
25+
Method: any
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"Resources": {
3+
"WebhooksApi": {
4+
"Properties": {
5+
"Body": {
6+
"swagger": 2,
7+
"x-amazon-apigateway-policy": {
8+
"Version": "2012-10-17"
9+
}
10+
}
11+
},
12+
"Type": "AWS::ApiGateway::RestApi"
13+
},
14+
"WebhooksApiDeployment472b94a845": {
15+
"Properties": {
16+
"Description": "RestApi deployment id: 472b94a845dac11081ca2890e848230a4e30a2aa",
17+
"RestApiId": {
18+
"Ref": "WebhooksApi"
19+
},
20+
"StageName": "Stage"
21+
},
22+
"Type": "AWS::ApiGateway::Deployment"
23+
},
24+
"WebhooksApiliveStage": {
25+
"Properties": {
26+
"DeploymentId": {
27+
"Ref": "WebhooksApiDeployment472b94a845"
28+
},
29+
"RestApiId": {
30+
"Ref": "WebhooksApi"
31+
},
32+
"StageName": "live"
33+
},
34+
"Type": "AWS::ApiGateway::Stage"
35+
},
36+
"WebhooksReceiver": {
37+
"Properties": {
38+
"Code": {
39+
"S3Bucket": "bucket",
40+
"S3Key": "key"
41+
},
42+
"Handler": "code/handler",
43+
"Role": {
44+
"Fn::GetAtt": [
45+
"WebhooksReceiverRole",
46+
"Arn"
47+
]
48+
},
49+
"Runtime": "python3.8",
50+
"Tags": [
51+
{
52+
"Key": "lambda:createdBy",
53+
"Value": "SAM"
54+
}
55+
]
56+
},
57+
"Type": "AWS::Lambda::Function"
58+
},
59+
"WebhooksReceiverAllEventPermissionlive": {
60+
"Properties": {
61+
"Action": "lambda:InvokeFunction",
62+
"FunctionName": {
63+
"Ref": "WebhooksReceiver"
64+
},
65+
"Principal": "apigateway.amazonaws.com",
66+
"SourceArn": {
67+
"Fn::Sub": [
68+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/*/proxy",
69+
{
70+
"__ApiId__": {
71+
"Ref": "WebhooksApi"
72+
},
73+
"__Stage__": "*"
74+
}
75+
]
76+
}
77+
},
78+
"Type": "AWS::Lambda::Permission"
79+
},
80+
"WebhooksReceiverRole": {
81+
"Properties": {
82+
"AssumeRolePolicyDocument": {
83+
"Statement": [
84+
{
85+
"Action": [
86+
"sts:AssumeRole"
87+
],
88+
"Effect": "Allow",
89+
"Principal": {
90+
"Service": [
91+
"lambda.amazonaws.com"
92+
]
93+
}
94+
}
95+
],
96+
"Version": "2012-10-17"
97+
},
98+
"ManagedPolicyArns": [
99+
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
100+
],
101+
"Tags": [
102+
{
103+
"Key": "lambda:createdBy",
104+
"Value": "SAM"
105+
}
106+
]
107+
},
108+
"Type": "AWS::IAM::Role"
109+
}
110+
}
111+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{
2+
"Resources": {
3+
"WebhooksApi": {
4+
"Properties": {
5+
"Body": {
6+
"swagger": 2,
7+
"x-amazon-apigateway-policy": {
8+
"Version": "2012-10-17"
9+
}
10+
},
11+
"EndpointConfiguration": {
12+
"Types": [
13+
"REGIONAL"
14+
]
15+
},
16+
"Parameters": {
17+
"endpointConfigurationTypes": "REGIONAL"
18+
}
19+
},
20+
"Type": "AWS::ApiGateway::RestApi"
21+
},
22+
"WebhooksApiDeployment472b94a845": {
23+
"Properties": {
24+
"Description": "RestApi deployment id: 472b94a845dac11081ca2890e848230a4e30a2aa",
25+
"RestApiId": {
26+
"Ref": "WebhooksApi"
27+
},
28+
"StageName": "Stage"
29+
},
30+
"Type": "AWS::ApiGateway::Deployment"
31+
},
32+
"WebhooksApiliveStage": {
33+
"Properties": {
34+
"DeploymentId": {
35+
"Ref": "WebhooksApiDeployment472b94a845"
36+
},
37+
"RestApiId": {
38+
"Ref": "WebhooksApi"
39+
},
40+
"StageName": "live"
41+
},
42+
"Type": "AWS::ApiGateway::Stage"
43+
},
44+
"WebhooksReceiver": {
45+
"Properties": {
46+
"Code": {
47+
"S3Bucket": "bucket",
48+
"S3Key": "key"
49+
},
50+
"Handler": "code/handler",
51+
"Role": {
52+
"Fn::GetAtt": [
53+
"WebhooksReceiverRole",
54+
"Arn"
55+
]
56+
},
57+
"Runtime": "python3.8",
58+
"Tags": [
59+
{
60+
"Key": "lambda:createdBy",
61+
"Value": "SAM"
62+
}
63+
]
64+
},
65+
"Type": "AWS::Lambda::Function"
66+
},
67+
"WebhooksReceiverAllEventPermissionlive": {
68+
"Properties": {
69+
"Action": "lambda:InvokeFunction",
70+
"FunctionName": {
71+
"Ref": "WebhooksReceiver"
72+
},
73+
"Principal": "apigateway.amazonaws.com",
74+
"SourceArn": {
75+
"Fn::Sub": [
76+
"arn:aws-cn:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/*/proxy",
77+
{
78+
"__ApiId__": {
79+
"Ref": "WebhooksApi"
80+
},
81+
"__Stage__": "*"
82+
}
83+
]
84+
}
85+
},
86+
"Type": "AWS::Lambda::Permission"
87+
},
88+
"WebhooksReceiverRole": {
89+
"Properties": {
90+
"AssumeRolePolicyDocument": {
91+
"Statement": [
92+
{
93+
"Action": [
94+
"sts:AssumeRole"
95+
],
96+
"Effect": "Allow",
97+
"Principal": {
98+
"Service": [
99+
"lambda.amazonaws.com"
100+
]
101+
}
102+
}
103+
],
104+
"Version": "2012-10-17"
105+
},
106+
"ManagedPolicyArns": [
107+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
108+
],
109+
"Tags": [
110+
{
111+
"Key": "lambda:createdBy",
112+
"Value": "SAM"
113+
}
114+
]
115+
},
116+
"Type": "AWS::IAM::Role"
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)