Skip to content

Commit a882020

Browse files
praneetapShreya
authored andcommitted
fix: adding support to point to api gateway distribution (#1230)
1 parent dbc8f79 commit a882020

15 files changed

+255
-436
lines changed

examples/2016-10-31/custom_domains_with_route53/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ Example SAM template for setting up Api Gateway resources for custom domains.
66
1. A domain name. You can purchase a domain name from a domain name provider.
77
1. A certificate ARN. Set up or import a valid certificate into AWS Certificate Manager. If the endpoint is EDGE, the certificate must be created in us-east-1.
88
1. A HostedZone in Route53 for the domain name.
9-
1. A Cloudfront Distribution for the domain if the endpoint is set to EDGE.
109

1110
## PostRequisites
1211
After deploying the template, make sure you configure the DNS settings on the domain name provider's website. You will need to add Type A and Type AAAA DNS records that are point to ApiGateway's Hosted Zone Id. Read more [here](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-to-api-gateway.html)
@@ -19,3 +18,5 @@ $ sam deploy \
1918
--stack-name my-new-stack \
2019
--capabilities CAPABILITY_IAM
2120
```
21+
22+
Curl to the endpoint "http://example.com/home/fetch" should hit the Api.

examples/2016-10-31/custom_domains_with_route53/template.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ Resources:
3737
CertificateArn: !Ref ACMCertificateArn
3838
EndpointConfiguration: EDGE
3939
BasePath:
40-
- /fetch
40+
- /home
4141
Route53:
4242
HostedZoneId: ZQ1UAL4EFZVME
4343
IpV6: true
44-
DistributionDomainName: !GetAtt Distribution.DomainName
44+
## ====== Everything below here is optional, leave this out if you want to use the internal Api Gateway distribution =======
45+
DistributionDomainName: !GetAtt Distribution.DomainName
4546

4647
Distribution:
4748
Type: AWS::CloudFront::Distribution

samtranslator/model/api/api_generator.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,7 @@ def _construct_alias_target(self, domain):
326326
alias_target['DNSName'] = fnGetAtt(self.domain.get('ApiDomainName'), 'RegionalDomainName')
327327
else:
328328
if route53.get('DistributionDomainName') is None:
329-
raise InvalidResourceException(self.logical_id,
330-
"Custom Domains support for EDGE requires the name "
331-
"of a Distribution resource")
329+
route53['DistributionDomainName'] = fnGetAtt(self.domain.get('ApiDomainName'), 'DistributionDomainName')
332330
alias_target['HostedZoneId'] = 'Z2FDTNDATAQYW2'
333331
alias_target['DNSName'] = route53.get('DistributionDomainName')
334332
return alias_target

samtranslator/model/apigateway.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ def make_auto_deployable(self, stage, openapi_version=None, swagger=None, domain
9898
if openapi_version:
9999
hash_input.append(str(openapi_version))
100100
if domain:
101-
hash_input.append(self._X_HASH_DELIMITER)
102101
hash_input.append(json.dumps(domain))
103102

104103
data = self._X_HASH_DELIMITER.join(hash_input)

tests/translator/input/api_with_custom_domain_route53.yaml

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,7 @@ Resources:
3737
CertificateArn: !Ref ACMCertificateArn
3838
EndpointConfiguration: EDGE
3939
BasePath:
40-
- /fetch
40+
- /one
4141
Route53:
4242
HostedZoneId: ZQ1UAL4EFZVME
43-
IpV6: true
44-
DistributionDomainName: !GetAtt Distribution.DomainName
45-
46-
Distribution:
47-
Type: AWS::CloudFront::Distribution
48-
Properties:
49-
DistributionConfig:
50-
Enabled: true
51-
HttpVersion: http2
52-
Origins:
53-
- DomainName: !Ref DomainName
54-
Id: !Ref DomainName
55-
CustomOriginConfig:
56-
HTTPPort: 80
57-
HTTPSPort: 443
58-
OriginProtocolPolicy: https-only
59-
DefaultCacheBehavior:
60-
AllowedMethods: [ HEAD, DELETE, POST, GET, OPTIONS, PUT, PATCH ]
61-
ForwardedValues:
62-
QueryString: false
63-
SmoothStreaming: false
64-
Compress: true
65-
TargetOriginId: !Ref DomainName
66-
ViewerProtocolPolicy: redirect-to-https
67-
PriceClass: PriceClass_100
68-
ViewerCertificate:
69-
SslSupportMethod: sni-only
70-
AcmCertificateArn: !Ref ACMCertificateArn
43+
IpV6: true

tests/translator/output/api_with_basic_custom_domain.json

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"Type": "AWS::ApiGateway::Stage",
9191
"Properties": {
9292
"DeploymentId": {
93-
"Ref": "MyAnotherApiDeploymente89573907a"
93+
"Ref": "MyAnotherApiDeployment48a61be119"
9494
},
9595
"RestApiId": {
9696
"Ref": "MyAnotherApi"
@@ -119,18 +119,6 @@
119119
}
120120
}
121121
},
122-
"MyAnotherApiBasePathMapping": {
123-
"Type": "AWS::ApiGateway::BasePathMapping",
124-
"Properties": {
125-
"DomainName": "another-example.com",
126-
"RestApiId": {
127-
"Ref": "MyAnotherApi"
128-
},
129-
"Stage": {
130-
"Ref": "MyAnotherApiProdStage"
131-
}
132-
}
133-
},
134122
"MyAnotherApi": {
135123
"Type": "AWS::ApiGateway::RestApi",
136124
"Properties": {
@@ -159,13 +147,17 @@
159147
}
160148
}
161149
},
162-
"MyApiDeploymentddf2dae73e": {
163-
"Type": "AWS::ApiGateway::Deployment",
150+
"MyApifetchBasePathMapping": {
151+
"Type": "AWS::ApiGateway::BasePathMapping",
164152
"Properties": {
153+
"BasePath": "fetch",
154+
"DomainName": "api-example.com",
165155
"RestApiId": {
166156
"Ref": "MyApi"
167157
},
168-
"Description": "RestApi deployment id: ddf2dae73e4e91595e8de4be7a30a1d207f32aab"
158+
"Stage": {
159+
"Ref": "MyApiProdStage"
160+
}
169161
}
170162
},
171163
"MyFunctionRole": {
@@ -245,13 +237,16 @@
245237
}
246238
}
247239
},
248-
"MyAnotherApiDeploymente89573907a": {
249-
"Type": "AWS::ApiGateway::Deployment",
240+
"MyAnotherApiBasePathMapping": {
241+
"Type": "AWS::ApiGateway::BasePathMapping",
250242
"Properties": {
243+
"DomainName": "another-example.com",
251244
"RestApiId": {
252245
"Ref": "MyAnotherApi"
253246
},
254-
"Description": "RestApi deployment id: e89573907aa0c18379ba3bcc497b31454e88a664"
247+
"Stage": {
248+
"Ref": "MyAnotherApiProdStage"
249+
}
255250
}
256251
},
257252
"ApiGatewayDomainNameeab65c1531": {
@@ -266,39 +261,35 @@
266261
"DomainName": "another-example.com"
267262
}
268263
},
269-
"MyApifetchBasePathMapping": {
270-
"Type": "AWS::ApiGateway::BasePathMapping",
264+
"ServerlessRestApiDeployment3c44da8ffd": {
265+
"Type": "AWS::ApiGateway::Deployment",
271266
"Properties": {
272-
"BasePath": "fetch",
273-
"DomainName": "api-example.com",
274267
"RestApiId": {
275-
"Ref": "MyApi"
268+
"Ref": "ServerlessRestApi"
276269
},
277-
"Stage": {
278-
"Ref": "MyApiProdStage"
279-
}
270+
"Description": "RestApi deployment id: 3c44da8ffdf3025dc792391a61590b92e8e2ff48",
271+
"StageName": "Stage"
280272
}
281273
},
282274
"ServerlessRestApiProdStage": {
283275
"Type": "AWS::ApiGateway::Stage",
284276
"Properties": {
285277
"DeploymentId": {
286-
"Ref": "ServerlessRestApiDeployment7db01b740c"
278+
"Ref": "ServerlessRestApiDeployment3c44da8ffd"
287279
},
288280
"RestApiId": {
289281
"Ref": "ServerlessRestApi"
290282
},
291283
"StageName": "Prod"
292284
}
293285
},
294-
"ServerlessRestApiDeployment7db01b740c": {
286+
"MyApiDeploymentbfb0243af9": {
295287
"Type": "AWS::ApiGateway::Deployment",
296288
"Properties": {
297289
"RestApiId": {
298-
"Ref": "ServerlessRestApi"
290+
"Ref": "MyApi"
299291
},
300-
"Description": "RestApi deployment id: 7db01b740cb897c4fc4175031f25e8123cc57ca9",
301-
"StageName": "Stage"
292+
"Description": "RestApi deployment id: bfb0243af9bffaabaf3bbc9d980266c6d6eb8471"
302293
}
303294
},
304295
"ServerlessRestApiBasePathMapping": {
@@ -317,14 +308,23 @@
317308
"Type": "AWS::ApiGateway::Stage",
318309
"Properties": {
319310
"DeploymentId": {
320-
"Ref": "MyApiDeploymentddf2dae73e"
311+
"Ref": "MyApiDeploymentbfb0243af9"
321312
},
322313
"RestApiId": {
323314
"Ref": "MyApi"
324315
},
325316
"StageName": "Prod"
326317
}
327318
},
319+
"MyAnotherApiDeployment48a61be119": {
320+
"Type": "AWS::ApiGateway::Deployment",
321+
"Properties": {
322+
"RestApiId": {
323+
"Ref": "MyAnotherApi"
324+
},
325+
"Description": "RestApi deployment id: 48a61be119a247807ea0ef8378e6aa16592f90fa"
326+
}
327+
},
328328
"ServerlessRestApi": {
329329
"Type": "AWS::ApiGateway::RestApi",
330330
"Properties": {

tests/translator/output/api_with_basic_custom_domain_intrinsics.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,6 @@
7878
},
7979
"Condition": "C1"
8080
},
81-
"MyApiDeployment6e1ee39b06": {
82-
"Type": "AWS::ApiGateway::Deployment",
83-
"Properties": {
84-
"RestApiId": {
85-
"Ref": "MyApi"
86-
},
87-
"Description": "RestApi deployment id: 6e1ee39b068c65688d05f859c3952e41396ecede"
88-
},
89-
"Condition": "C1"
90-
},
9181
"ServerlessRestApiProdStage": {
9282
"Type": "AWS::ApiGateway::Stage",
9383
"Properties": {
@@ -226,7 +216,7 @@
226216
"Type": "AWS::ApiGateway::Stage",
227217
"Properties": {
228218
"DeploymentId": {
229-
"Ref": "MyApiDeployment6e1ee39b06"
219+
"Ref": "MyApiDeployment462f07ab8f"
230220
},
231221
"RestApiId": {
232222
"Ref": "MyApi"
@@ -235,6 +225,16 @@
235225
},
236226
"Condition": "C1"
237227
},
228+
"MyApiDeployment462f07ab8f": {
229+
"Type": "AWS::ApiGateway::Deployment",
230+
"Properties": {
231+
"RestApiId": {
232+
"Ref": "MyApi"
233+
},
234+
"Description": "RestApi deployment id: 462f07ab8fcdc437955985b08464280d2a9244be"
235+
},
236+
"Condition": "C1"
237+
},
238238
"ServerlessRestApi": {
239239
"Type": "AWS::ApiGateway::RestApi",
240240
"Properties": {

0 commit comments

Comments
 (0)