Skip to content

Commit 6b20a88

Browse files
committed
Improve exception processing for Route53 with invalid type
1 parent ab6943a commit 6b20a88

File tree

3 files changed

+63
-13
lines changed

3 files changed

+63
-13
lines changed

samtranslator/model/api/api_generator.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -515,22 +515,31 @@ def _construct_api_domain(self, rest_api):
515515
record_set_group = None
516516
if self.domain.get("Route53") is not None:
517517
route53 = self.domain.get("Route53")
518-
if route53.get("HostedZoneId") is None and route53.get("HostedZoneName") is None:
518+
if isinstance(route53, dict):
519+
if route53.get("HostedZoneId") is None and route53.get("HostedZoneName") is None:
520+
raise InvalidResourceException(
521+
self.logical_id,
522+
"HostedZoneId or HostedZoneName is required to enable Route53 support on Custom Domains.",
523+
)
524+
logical_id = logical_id_generator.LogicalIdGenerator(
525+
"", route53.get("HostedZoneId") or route53.get("HostedZoneName")
526+
).gen()
527+
record_set_group = Route53RecordSetGroup(
528+
"RecordSetGroup" + logical_id, attributes=self.passthrough_resource_attributes
529+
)
530+
if "HostedZoneId" in route53:
531+
record_set_group.HostedZoneId = route53.get("HostedZoneId")
532+
if "HostedZoneName" in route53:
533+
record_set_group.HostedZoneName = route53.get("HostedZoneName")
534+
record_set_group.RecordSets = self._construct_record_sets_for_domain(self.domain)
535+
else:
519536
raise InvalidResourceException(
520537
self.logical_id,
521-
"HostedZoneId or HostedZoneName is required to enable Route53 support on Custom Domains.",
538+
"Invalid property type '{}' for Route53. "
539+
"Expected a map defines an Amazon Route 53 configuration'.".format(
540+
type(route53).__name__
541+
),
522542
)
523-
logical_id = logical_id_generator.LogicalIdGenerator(
524-
"", route53.get("HostedZoneId") or route53.get("HostedZoneName")
525-
).gen()
526-
record_set_group = Route53RecordSetGroup(
527-
"RecordSetGroup" + logical_id, attributes=self.passthrough_resource_attributes
528-
)
529-
if "HostedZoneId" in route53:
530-
record_set_group.HostedZoneId = route53.get("HostedZoneId")
531-
if "HostedZoneName" in route53:
532-
record_set_group.HostedZoneName = route53.get("HostedZoneName")
533-
record_set_group.RecordSets = self._construct_record_sets_for_domain(self.domain)
534543

535544
return domain, basepath_resource_list, record_set_group
536545

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Resources:
2+
MyFunction:
3+
Type: AWS::Serverless::Function
4+
Properties:
5+
InlineCode: |
6+
exports.handler = async (event) => {
7+
const response = {
8+
statusCode: 200,
9+
body: JSON.stringify('Hello from Lambda!'),
10+
};
11+
return response;
12+
};
13+
Handler: index.handler
14+
Runtime: nodejs12.x
15+
Events:
16+
Api:
17+
Type: Api
18+
Properties:
19+
RestApiId: !Ref MyApi
20+
Method: Put
21+
Path: /get
22+
23+
MyApi:
24+
Type: AWS::Serverless::Api
25+
Properties:
26+
OpenApiVersion: 3.0.1
27+
StageName: Prod
28+
Domain:
29+
DomainName: 'api-example.com'
30+
CertificateArn: 'my-api-cert-arn'
31+
EndpointConfiguration: 'EDGE'
32+
BasePath: [ "/get"]
33+
Route53: 'InvalidString'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errors": [
3+
{
4+
"errorMessage": "Resource with id [MyApi] is invalid. Invalid property type 'Py27UniStr' for Route53. Expected a map defines an Amazon Route 53 configuration'."
5+
}
6+
],
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyApi] is invalid. Invalid property type 'Py27UniStr' for Route53. Expected a map defines an Amazon Route 53 configuration'."
8+
}

0 commit comments

Comments
 (0)