Skip to content

Commit 979825f

Browse files
authored
Add method for determining service availability in a region (#2321)
1 parent 802334b commit 979825f

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

samtranslator/plugins/application/serverless_app_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def on_before_transform_template(self, template_dict):
111111

112112
if key not in self._applications:
113113
try:
114-
if not RegionConfiguration.is_sar_supported():
114+
if not RegionConfiguration.is_service_supported("serverlessrepo"):
115115
raise InvalidResourceException(
116116
logical_id, "Serverless Application Repository is not available in this region."
117117
)

samtranslator/region_configuration.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,32 @@ def is_apigw_edge_configuration_supported(cls):
2626
]
2727

2828
@classmethod
29-
def is_sar_supported(cls):
29+
def is_service_supported(cls, service, region=None):
3030
"""
31-
SAR is not supported in some regions.
31+
Not all services are supported in all regions. This method returns whether a given
32+
service is supported in a given region. If no region is specified, the current region
33+
(as identified by boto3) is used.
3234
https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/
33-
https://docs.aws.amazon.com/general/latest/gr/serverlessrepo.html
3435
35-
:return: True, if SAR is supported in current region.
36+
:param service: service code (string used to obtain a boto3 client for the service)
37+
:param region: region identifier (e.g., us-east-1)
38+
:return: True, if the service is supported in the region
3639
"""
3740

3841
session = boto3.Session()
3942

40-
# get the current region
41-
region = session.region_name
43+
if not region:
44+
# get the current region
45+
region = session.region_name
4246

43-
# need to handle when region is None so that it won't break
44-
if region is None:
45-
if ArnGenerator.BOTO_SESSION_REGION_NAME is not None:
46-
region = ArnGenerator.BOTO_SESSION_REGION_NAME
47-
else:
48-
raise NoRegionFound("AWS Region cannot be found")
47+
# need to handle when region is None so that it won't break
48+
if region is None:
49+
if ArnGenerator.BOTO_SESSION_REGION_NAME is not None:
50+
region = ArnGenerator.BOTO_SESSION_REGION_NAME
51+
else:
52+
raise NoRegionFound("AWS Region cannot be found")
4953

50-
# boto3 get_available_regions call won't return us-gov and cn regions even if SAR is available
51-
if region.startswith("cn") or region.startswith("us-gov"):
52-
return True
53-
54-
# get all regions where SAR are available
55-
available_regions = session.get_available_regions("serverlessrepo")
54+
# check if the service is available in region
55+
partition = session.get_partition_for_region(region)
56+
available_regions = session.get_available_regions(service, partition_name=partition)
5657
return region in available_regions

tests/unit/test_region_configuration.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,27 @@ def test_when_apigw_edge_configuration_is_not_supported(self, partition):
3636
get_partition_name_patch.return_value = partition
3737

3838
self.assertFalse(RegionConfiguration.is_apigw_edge_configuration_supported())
39+
40+
@parameterized.expand(
41+
[
42+
# use ec2 as it's just about everywhere
43+
["ec2", "cn-north-1"],
44+
["ec2", "us-west-2"],
45+
["ec2", "us-gov-east-1"],
46+
["ec2", "us-isob-east-1"],
47+
["ec2", None],
48+
# test SAR since SAM uses that
49+
["serverlessrepo", "us-east-1"],
50+
["serverlessrepo", "ap-southeast-2"],
51+
]
52+
)
53+
def test_is_service_supported_positive(self, service, region):
54+
self.assertTrue(RegionConfiguration.is_service_supported(service, region))
55+
56+
def test_is_service_supported_negative(self):
57+
# use an unknown service name
58+
self.assertFalse(RegionConfiguration.is_service_supported("ec1", "us-east-1"))
59+
# use a region that does not exist
60+
self.assertFalse(RegionConfiguration.is_service_supported("ec2", "us-east-0"))
61+
# hard to test with a real service, since the test may start failing once that
62+
# service is rolled out to more regions...

0 commit comments

Comments
 (0)