@@ -56,6 +56,8 @@ LOCALSTACK_HOSTNAME = (
5656 or "localhost"
5757)
5858EDGE_PORT = int (urlparse (AWS_ENDPOINT_URL ).port or os .environ .get ("EDGE_PORT" ) or 4566 )
59+ AWS_PROVIDER_NAME_SUFFIX = "/hashicorp/aws"
60+ AWS_PROVIDER_VERSION : Optional [version .Version ] = None
5961TF_VERSION : Optional [version .Version ] = None
6062TF_PROVIDER_CONFIG = """
6163provider "aws" {
@@ -128,6 +130,15 @@ SERVICE_ALIASES = [
128130]
129131# service names to be excluded (not yet available in TF)
130132SERVICE_EXCLUSIONS = ["meteringmarketplace" ]
133+
134+ # we can exclude some service endpoints based on the AWS provider version
135+ # those limits are exclusive, meaning 6.0.0b2 is the first version to fail with those endpoints, so only a lower version
136+ # will have that setting
137+ VERSIONED_SERVICE_EXCLUSIONS = {
138+ "iotanalytics" : {"min" : version .Version ("0" ), "max" : version .Version ("6.0.0b2" )},
139+ "iotevents" : {"min" : version .Version ("0" ), "max" : version .Version ("6.0.0b2" )},
140+ }
141+
131142# maps services to be replaced with alternative names
132143# skip services which do not have equivalent endpoint overrides
133144# see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/custom-service-endpoints
@@ -173,7 +184,8 @@ def create_provider_config_file(provider_file_path: str, provider_aliases=None)
173184
174185 # create list of service names
175186 services = list (config .get_service_ports ())
176- services = [srvc for srvc in services if srvc not in SERVICE_EXCLUSIONS ]
187+ services = [srvc for srvc in services if srvc not in SERVICE_EXCLUSIONS and is_service_endpoint_supported (srvc )]
188+
177189 services = [s .replace ("-" , "" ) for s in services ]
178190 for old , new in SERVICE_REPLACEMENTS .items ():
179191 try :
@@ -606,6 +618,33 @@ def get_tf_version(env):
606618 TF_VERSION = version .parse (json .loads (output )["terraform_version" ])
607619
608620
621+ def get_provider_version_from_lock_file () -> Optional [version .Version ]:
622+ global AWS_PROVIDER_VERSION
623+ lock_file = os .path .join (get_default_provider_folder_path (), ".terraform.lock.hcl" )
624+
625+ if not os .path .exists (lock_file ):
626+ return
627+
628+ provider_version = None
629+ with open (lock_file , "r" ) as fp :
630+ result = hcl2 .load (fp )
631+ for provider in result .get ("provider" , []):
632+ for provider_name , provider_config in provider .items ():
633+ if provider_name .endswith (AWS_PROVIDER_NAME_SUFFIX ):
634+ provider_version = provider_config .get ("version" )
635+
636+ if provider_version :
637+ AWS_PROVIDER_VERSION = version .parse (provider_version )
638+
639+
640+ def is_service_endpoint_supported (service_name : str ) -> bool :
641+ if service_name not in VERSIONED_SERVICE_EXCLUSIONS or not AWS_PROVIDER_VERSION :
642+ return True
643+
644+ supported_versions = VERSIONED_SERVICE_EXCLUSIONS [service_name ]
645+ return supported_versions ["min" ] < AWS_PROVIDER_VERSION < supported_versions ["max" ]
646+
647+
609648def run_tf_exec (cmd , env ):
610649 """Run terraform using os.exec - can be useful as it does not require any I/O
611650 handling for stdin/out/err. Does *not* allow us to perform any cleanup logic."""
@@ -686,6 +725,9 @@ def main():
686725 print (f"Unable to determine version. See error message for details: { e } " )
687726 exit (1 )
688727
728+ if len (sys .argv ) > 1 and sys .argv [1 ] != "init" :
729+ get_provider_version_from_lock_file ()
730+
689731 config_override_files = []
690732
691733 for folder_path in get_folder_paths_that_require_an_override_file ():
0 commit comments