@@ -122,6 +122,9 @@ def get_customer_control_tower_regions() -> list: # noqa: CCR001
122122
123123 Supports both legacy Control Tower (pre-4.0) using StackSets and Control Tower 4.0+.
124124
125+ Raises:
126+ ClientError: If an unexpected AWS API error occurs.
127+
125128 Returns:
126129 Customer regions chosen in Control Tower
127130 """
@@ -260,6 +263,9 @@ def _get_ct4_cloudformation_ssm_parameter_info(path: str) -> dict:
260263 Args:
261264 path: SSM parameter hierarchy path
262265
266+ Raises:
267+ ValueError: If Audit or Log Archive account cannot be found.
268+
263269 Returns:
264270 Info needed to create SSM parameters and helper data for custom resource
265271 """
@@ -291,7 +297,7 @@ def _get_ct4_cloudformation_ssm_parameter_info(path: str) -> dict:
291297 LOGGER .warning ("Could not find Audit account in Organizations. Ensure account is named 'Audit' or 'Security'." )
292298 raise ValueError (
293299 "Audit account not found. For CT 4.0, ensure your security account is named 'Audit' or 'Security', "
294- "or use pControlTower=false with manual account IDs."
300+ + "or use pControlTower=false with manual account IDs."
295301 )
296302
297303 if ct_accounts ["LogArchiveAccountId" ]:
@@ -301,7 +307,7 @@ def _get_ct4_cloudformation_ssm_parameter_info(path: str) -> dict:
301307 LOGGER .warning ("Could not find Log Archive account in Organizations. Ensure account is named 'Log Archive'." )
302308 raise ValueError (
303309 "Log Archive account not found. For CT 4.0, ensure your log archive account is named 'Log Archive', "
304- "or use pControlTower=false with manual account IDs."
310+ + "or use pControlTower=false with manual account IDs."
305311 )
306312
307313 LOGGER .info (ssm_data ["helper" ])
@@ -317,6 +323,10 @@ def get_cloudformation_ssm_parameter_info(path: str) -> dict: # noqa: CCR001
317323 Args:
318324 path: SSM parameter hierarchy path
319325
326+ Raises:
327+ ClientError: If an unexpected AWS API error occurs.
328+ ValueError: If Log Archive account cannot be found.
329+
320330 Returns:
321331 Info needed to create SSM parameters and helper data for custom resource
322332 """
@@ -337,8 +347,7 @@ def get_cloudformation_ssm_parameter_info(path: str) -> dict: # noqa: CCR001
337347 if error .response ["Error" ]["Code" ] == "StackSetNotFoundException" :
338348 LOGGER .info ("Control Tower 4.0+ detected - AWSControlTowerBP-BASELINE-CONFIG StackSet not found" )
339349 return _get_ct4_cloudformation_ssm_parameter_info (path )
340- else :
341- raise
350+ raise
342351
343352 # Legacy CT (< 4.0): Get Log Archive account from AWSControlTowerLoggingResources StackSet.
344353 try :
@@ -359,7 +368,7 @@ def get_cloudformation_ssm_parameter_info(path: str) -> dict: # noqa: CCR001
359368 )
360369 ssm_data ["helper" ]["LogArchiveAccountId" ] = ct_accounts ["LogArchiveAccountId" ]
361370 else :
362- raise ValueError ("Log Archive account not found in Organizations." )
371+ raise ValueError ("Log Archive account not found in Organizations." ) from None
363372 else :
364373 raise
365374
@@ -674,19 +683,17 @@ def create_update_event(event: CloudFormationCustomResourceEvent, context: Conte
674683 ssm_data3 = get_customer_control_tower_regions_ssm_parameter_info (ssm_data2 ["helper" ]["HomeRegion" ], path = SRA_REGIONS_SSM_PATH )
675684 ssm_data4 = get_enabled_regions_ssm_parameter_info (ssm_data2 ["helper" ]["HomeRegion" ], path = SRA_REGIONS_SSM_PATH )
676685
677- # Discover Config delivery bucket name only for CT environments.
678- # CT 4.0 uses a dedicated bucket with random suffix; CT 3.x uses the legacy pattern.
679- # Non-CT environments don't need this parameter (they use config_org solution instead).
680- ssm_data5 : dict = {"info" : [], "helper" : {}}
681- if CONTROL_TOWER == "true" :
682- config_bucket_name = _get_config_delivery_bucket_name (
683- log_archive_account_id = ssm_data2 ["helper" ].get ("LogArchiveAccountId" , "" ),
684- home_region = ssm_data2 ["helper" ]["HomeRegion" ],
685- )
686- ssm_data5 = {
687- "info" : [{"name" : f"{ SRA_CONTROL_TOWER_SSM_PATH } /config-delivery-bucket-name" , "value" : config_bucket_name , "parameter_type" : "String" }],
688- "helper" : {"ConfigDeliveryBucketName" : config_bucket_name },
689- }
686+ # Discover Config delivery bucket name for all environments.
687+ # CT 4.0: reads from AWSControlTowerBP-CONFIG-CENTRAL-S3-BUCKET StackSet (random suffix).
688+ # CT 3.x / Non-CT: falls back to legacy pattern aws-controltower-logs-{AccountId}-{Region}.
689+ config_bucket_name = _get_config_delivery_bucket_name (
690+ log_archive_account_id = ssm_data2 ["helper" ].get ("LogArchiveAccountId" , "" ),
691+ home_region = ssm_data2 ["helper" ]["HomeRegion" ],
692+ )
693+ ssm_data5 : dict = {
694+ "info" : [{"name" : f"{ SRA_CONTROL_TOWER_SSM_PATH } /config-delivery-bucket-name" , "value" : config_bucket_name , "parameter_type" : "String" }],
695+ "helper" : {"ConfigDeliveryBucketName" : config_bucket_name },
696+ }
690697
691698 ssm_parameters = ssm_data1 ["info" ] + ssm_data2 ["info" ] + ssm_data3 ["info" ] + ssm_data4 ["info" ] + ssm_data5 ["info" ]
692699 create_ssm_parameters_in_regions (ssm_parameters , tags , ssm_data4 ["helper" ]["EnabledRegions" ])
0 commit comments