@@ -415,6 +415,31 @@ def setup_sts_credentials(region, request):
415415 unset_credentials ()
416416
417417
418+ def get_availability_zones (region , credential ):
419+ """
420+ Return a list of availability zones for the given region.
421+
422+ Note that this function is called by the vpc_stacks fixture. Because vcp_stacks is session-scoped,
423+ it cannot utilize setup_sts_credentials, which is required in opt-in regions in order to call
424+ describe_availability_zones.
425+ """
426+ set_credentials (region , credential )
427+ az_list = []
428+ try :
429+ client = boto3 .client ("ec2" , region_name = region )
430+ response_az = client .describe_availability_zones (
431+ Filters = [
432+ {"Name" : "region-name" , "Values" : [str (region )]},
433+ {"Name" : "zone-type" , "Values" : ["availability-zone" ]},
434+ ]
435+ )
436+ for az in response_az .get ("AvailabilityZones" ):
437+ az_list .append (az .get ("ZoneName" ))
438+ finally :
439+ unset_credentials ()
440+ return az_list
441+
442+
418443@pytest .fixture (scope = "session" , autouse = True )
419444def vpc_stacks (cfn_stacks_factory , request ):
420445 """Create VPC used by integ tests in all configured regions."""
@@ -430,17 +455,7 @@ def vpc_stacks(cfn_stacks_factory, request):
430455 availability_zones = random .sample (AVAILABILITY_ZONE_OVERRIDES .get (region ), k = 2 )
431456 # else if region is not in AVAILABILITY_ZONE_OVERRIDES keys, find available zones mapping to the region
432457 else :
433- # get ec2 client
434- client = boto3 .client ("ec2" , region_name = region )
435- response_az = client .describe_availability_zones (
436- Filters = [
437- {"Name" : "region-name" , "Values" : [str (region )]},
438- {"Name" : "zone-type" , "Values" : ["availability-zone" ]},
439- ]
440- )
441- az_list = []
442- for az in response_az .get ("AvailabilityZones" ):
443- az_list .append (az .get ("ZoneName" ))
458+ az_list = get_availability_zones (region , request .config .getoption ("credential" ))
444459 # if number of available zones is smaller than 2, available zones should be [None, None]
445460 if len (az_list ) < 2 :
446461 availability_zones = [None , None ]
0 commit comments