1313 ApiGatewayBasePathMappingV2 ,
1414 ApiGatewayDeployment ,
1515 ApiGatewayDomainName ,
16+ ApiGatewayDomainNameAccessAssociation ,
1617 ApiGatewayDomainNameV2 ,
1718 ApiGatewayResponse ,
1819 ApiGatewayRestApi ,
@@ -86,6 +87,7 @@ class ApiDomainResponseV2:
8687 domain : Optional [ApiGatewayDomainNameV2 ]
8788 apigw_basepath_mapping_list : Optional [List [ApiGatewayBasePathMappingV2 ]]
8889 recordset_group : Any
90+ domain_access_association : Any
8991
9092
9193class SharedApiUsagePlan :
@@ -218,6 +220,7 @@ def __init__( # noqa: PLR0913
218220 api_key_source_type : Optional [Intrinsicable [str ]] = None ,
219221 always_deploy : Optional [bool ] = False ,
220222 feature_toggle : Optional [FeatureToggle ] = None ,
223+ policy : Optional [Union [Dict [str , Any ], Intrinsicable [str ]]] = None ,
221224 ):
222225 """Constructs an API Generator class that generates API Gateway resources
223226
@@ -275,6 +278,7 @@ def __init__( # noqa: PLR0913
275278 self .api_key_source_type = api_key_source_type
276279 self .always_deploy = always_deploy
277280 self .feature_toggle = feature_toggle
281+ self .policy = policy
278282
279283 def _construct_rest_api (self ) -> ApiGatewayRestApi :
280284 """Constructs and returns the ApiGateway RestApi.
@@ -328,6 +332,9 @@ def _construct_rest_api(self) -> ApiGatewayRestApi:
328332 if self .api_key_source_type :
329333 rest_api .ApiKeySourceType = self .api_key_source_type
330334
335+ if self .policy :
336+ rest_api .Policy = self .policy
337+
331338 return rest_api
332339
333340 def _validate_properties (self ) -> None :
@@ -602,7 +609,7 @@ def _construct_api_domain_v2(
602609 Constructs and returns the ApiGateway Domain V2 and BasepathMapping V2
603610 """
604611 if self .domain is None :
605- return ApiDomainResponseV2 (None , None , None )
612+ return ApiDomainResponseV2 (None , None , None , None )
606613
607614 sam_expect (self .domain , self .logical_id , "Domain" ).to_be_a_map ()
608615 domain_name : PassThrough = sam_expect (
@@ -657,6 +664,14 @@ def _construct_api_domain_v2(
657664 basepath_mapping .BasePath = path if normalize_basepath else basepath
658665 basepath_resource_list .extend ([basepath_mapping ])
659666
667+ # Create the DomainNameAccessAssociation
668+ domain_access_association = self .domain .get ("AccessAssociation" )
669+ domain_access_association_resource = None
670+ if domain_access_association is not None :
671+ domain_access_association_resource = self ._generate_domain_access_association (
672+ domain_access_association , domain_name_arn , api_domain_name
673+ )
674+
660675 # Create the Route53 RecordSetGroup resource
661676 record_set_group = None
662677 route53 = self .domain .get ("Route53" )
@@ -683,6 +698,7 @@ def _construct_api_domain_v2(
683698 domain ,
684699 basepath_resource_list ,
685700 self ._construct_single_record_set_group (self .domain , domain_name , route53 ),
701+ domain_access_association_resource ,
686702 )
687703
688704 if not record_set_group :
@@ -691,7 +707,7 @@ def _construct_api_domain_v2(
691707
692708 record_set_group .RecordSets += self ._construct_record_sets_for_domain (self .domain , domain_name , route53 )
693709
694- return ApiDomainResponseV2 (domain , basepath_resource_list , record_set_group )
710+ return ApiDomainResponseV2 (domain , basepath_resource_list , record_set_group , domain_access_association_resource )
695711
696712 def _get_basepaths (self ) -> Optional [List [str ]]:
697713 if self .domain is None :
@@ -779,11 +795,14 @@ def _construct_alias_target(self, domain: Dict[str, Any], api_domain_name: str,
779795 if domain .get ("EndpointConfiguration" ) == "REGIONAL" :
780796 alias_target ["HostedZoneId" ] = fnGetAtt (api_domain_name , "RegionalHostedZoneId" )
781797 alias_target ["DNSName" ] = fnGetAtt (api_domain_name , "RegionalDomainName" )
782- else :
798+ elif domain . get ( "EndpointConfiguration" ) == "EDGE" :
783799 if route53 .get ("DistributionDomainName" ) is None :
784800 route53 ["DistributionDomainName" ] = fnGetAtt (api_domain_name , "DistributionDomainName" )
785801 alias_target ["HostedZoneId" ] = "Z2FDTNDATAQYW2"
786802 alias_target ["DNSName" ] = route53 .get ("DistributionDomainName" )
803+ else :
804+ alias_target ["HostedZoneId" ] = route53 .get ("VpcEndpointHostedZoneId" )
805+ alias_target ["DNSName" ] = route53 .get ("VpcEndpointDomainName" )
787806 return alias_target
788807
789808 def _create_basepath_mapping (
@@ -833,12 +852,17 @@ def to_cloudformation(
833852 domain : Union [Resource , None ]
834853 basepath_mapping : Union [List [ApiGatewayBasePathMapping ], List [ApiGatewayBasePathMappingV2 ], None ]
835854 rest_api = self ._construct_rest_api ()
855+ is_private_domain = isinstance (self .domain , dict ) and self .domain .get ("EndpointConfiguration" ) == "PRIVATE"
836856 api_domain_response = (
837857 self ._construct_api_domain_v2 (rest_api , route53_record_set_groups )
838- if isinstance ( self . domain , dict ) and self . domain . get ( "EndpointConfiguration" ) == "PRIVATE"
858+ if is_private_domain
839859 else self ._construct_api_domain (rest_api , route53_record_set_groups )
840860 )
841861
862+ domain_access_association = None
863+ if is_private_domain :
864+ domain_access_association = cast (ApiDomainResponseV2 , api_domain_response ).domain_access_association
865+
842866 domain = api_domain_response .domain
843867 basepath_mapping = api_domain_response .apigw_basepath_mapping_list
844868
@@ -882,6 +906,9 @@ def to_cloudformation(
882906 ]
883907 )
884908
909+ if domain_access_association is not None :
910+ generated_resources .append (domain_access_association )
911+
885912 # Make a list of single resources
886913 generated_resources_list : List [Resource ] = []
887914 for resource in generated_resources :
@@ -1513,3 +1540,24 @@ def _set_endpoint_configuration(self, rest_api: ApiGatewayRestApi, value: Union[
15131540 else :
15141541 rest_api .EndpointConfiguration = {"Types" : [value ]}
15151542 rest_api .Parameters = {"endpointConfigurationTypes" : value }
1543+
1544+ def _generate_domain_access_association (
1545+ self ,
1546+ domain_access_association : Dict [str , Any ],
1547+ domain_name_arn : Dict [str , str ],
1548+ domain_logical_id : str ,
1549+ ) -> ApiGatewayDomainNameAccessAssociation :
1550+ """
1551+ Generate domain access association resource
1552+ """
1553+ vpcEndpointId = domain_access_association .get ("VpcEndpointId" )
1554+ logical_id = LogicalIdGenerator ("DomainNameAccessAssociation" , [vpcEndpointId , domain_logical_id ]).gen ()
1555+
1556+ domain_access_association_resource = ApiGatewayDomainNameAccessAssociation (
1557+ logical_id , attributes = self .passthrough_resource_attributes
1558+ )
1559+ domain_access_association_resource .DomainNameArn = domain_name_arn
1560+ domain_access_association_resource .AccessAssociationSourceType = "VPCE"
1561+ domain_access_association_resource .AccessAssociationSource = vpcEndpointId
1562+
1563+ return domain_access_association_resource
0 commit comments