@@ -151,6 +151,47 @@ def update_bucket_principal_arn(bucket_name: str, target_user_id: str, new_assum
151151 except Exception as e :
152152 print (f"Error updating bucket policy: { e } " )
153153
154+ def get_batch_resource_arns (stack_name : str ) -> dict :
155+ """
156+ Retrieves physical IDs of Batch resources from a CloudFormation stack.
157+
158+ Args:
159+ stack_name (str): The name or ID of the CloudFormation stack.
160+
161+ Returns:
162+ dict: Dictionary containing ARNs for:
163+ - JobDefinitionArn
164+ - ComputeEnvironmentArn
165+ - SchedulingPolicyArn
166+ - JobQueueArn
167+ """
168+ cf_client = boto3 .client ("cloudformation" )
169+ result = {}
170+
171+ try :
172+ # Call describe_stack_resources API
173+ response = cf_client .describe_stack_resources (StackName = stack_name )
174+ stack_resources = response .get ("StackResources" , [])
175+
176+ for resource in stack_resources :
177+ resource_type = resource .get ("ResourceType" )
178+ physical_id = resource .get ("PhysicalResourceId" )
179+
180+ if resource_type == "AWS::Batch::JobDefinition" :
181+ result ["JobDefinitionArn" ] = physical_id
182+ elif resource_type == "AWS::Batch::ComputeEnvironment" :
183+ result ["ComputeEnvironmentArn" ] = physical_id
184+ elif resource_type == "AWS::Batch::SchedulingPolicy" :
185+ result ["SchedulingPolicyArn" ] = physical_id
186+ elif resource_type == "AWS::Batch::JobQueue" :
187+ result ["JobQueueArn" ] = physical_id
188+
189+ return result
190+
191+ except cf_client .exceptions .ClientError as e :
192+ print (f"Error retrieving stack resources: { e } " )
193+ return {}
194+
154195
155196def main ():
156197 args = get_args ()
@@ -159,7 +200,8 @@ def main():
159200
160201 # Execute a Service catalog change owner action
161202 sc_client = boto3 .client ("servicecatalog" )
162- print (f"Executing Service Catalog change owner action for product { args .ProvisionedProductId } to new owner { new_owner_arn } " )
203+ print (f"Executing Service Catalog change owner action for product "
204+ f"{ args .ProvisionedProductId } to new owner { new_owner_arn } " )
163205 response = sc_client .update_provisioned_product_properties (
164206 ProvisionedProductId = args .ProvisionedProductId ,
165207 ProvisionedProductProperties = {
@@ -171,47 +213,53 @@ def main():
171213 # Update tags for service catalog products
172214 os .environ ["TEAM_TO_ROLE_ARN_MAP_PARAM_NAME" ] = "/service-catalog/TeamToRoleArnMap"
173215 if args .StackId :
174- print (f"StackId: { args .StackId } " )
175- event = {"StackId" : args .StackId }
216+ stack_id = args .StackId
217+ print (f"StackId: { stack_id } " )
218+ event = {"StackId" : stack_id }
219+ stack_name = stack_id .split ("stack/" )[1 ].split ("/" )[0 ]
176220 try :
177- # monkey patch to always return the Synapse owner id from
178- # the user supplied OwnerArn
221+ # monkey patch to return the Synapse owner id from the passed in OwnerArn
179222 utils .get_synapse_owner_id = lambda tags : new_user_id
180223
224+ # monkey patch to return a dict of batch resource ARNs from the list of cloudformation resources
225+ batch_resources = get_batch_resource_arns (stack_name )
226+ utils .get_property_value = lambda event , key : batch_resources
227+
228+ print (f"Update tags on batch resources: { batch_resources } " )
181229 set_batch_tags .create_or_update (event , None )
182230 print ("Batch tags updated successfully." )
183231 except Exception as e :
184- print (f"Failed to update batch: { e } " )
232+ print (f"Failed to update batch resources : { e } " )
185233 sys .exit (1 )
186234 if args .BucketName :
187235 bucket_name = args .BucketName
188- print (f"Update tags on bucket: { bucket_name } " )
189236 event = {"ResourceProperties" :{"BucketName" : bucket_name }}
190237 try :
191238 # Get existing synapse user id
192239 bucket_tags = set_bucket_tags .get_bucket_tags (bucket_name )
193240 existing_user_id = utils .get_synapse_owner_id (bucket_tags )
194241
195- # monkey patch to always return the Synapse owner id from
196- # the user supplied OwnerArn
242+ # monkey patch to return the Synapse owner id from the passed in OwnerArn
197243 utils .get_synapse_owner_id = lambda tags : new_user_id
198244
245+ print (f"Update tags on bucket: { bucket_name } " )
199246 set_bucket_tags .create_or_update (event , None )
200247 print ("Bucket tags updated successfully." )
201248
202249 # Update the bucket policy to allow new owner access
250+ print (f"Update policy on bucket: { bucket_name } " )
203251 update_bucket_principal_arn (bucket_name , existing_user_id , new_owner_arn )
204252 except Exception as e :
205253 print (f"Failed to update bucket: { e } " )
206254 sys .exit (1 )
207255 if args .InstanceId :
208- print ( f"InstanceId: { args .InstanceId } " )
209- event = {"ResourceProperties" :{"InstanceId" : args . InstanceId }}
256+ instance_id = args .InstanceId
257+ event = {"ResourceProperties" :{"InstanceId" : instance_id }}
210258 try :
211- # monkey patch to always return the Synapse owner id from
212- # the user supplied OwnerArn
259+ # monkey patch to return the Synapse owner id from the passed in OwnerArn
213260 utils .get_synapse_owner_id = lambda tags : new_user_id
214261
262+ print (f"Update tags on EC2 instance: { instance_id } " )
215263 set_instance_tags .create_or_update (event , None )
216264 print ("Instance tags updated successfully." )
217265 except Exception as e :
0 commit comments