@@ -378,45 +378,26 @@ def deploy_function(example_name: str, function_name: str | None = None):
378378 except lambda_client .exceptions .ResourceNotFoundException :
379379 lambda_client .create_function (** function_config , Code = {"ZipFile" : zip_content })
380380
381- # Update invoke permission for worker account if needed
382- try :
383- policy_response = lambda_client .get_policy (FunctionName = function_name )
384- policy = json .loads (policy_response ["Policy" ])
385-
386- # Check if permission exists with correct principal
387- needs_update = True
388- for statement in policy .get ("Statement" , []):
389- if (
390- statement .get ("Sid" ) == "dex-invoke-permission"
391- and statement .get ("Principal" , {}).get ("AWS" )
392- == config ["invoke_account_id" ]
393- ):
394- needs_update = False
395- break
396-
397- if needs_update :
398- with contextlib .suppress (
399- lambda_client .exceptions .ResourceNotFoundException
400- ):
401- lambda_client .remove_permission (
402- FunctionName = function_name , StatementId = "dex-invoke-permission"
403- )
404-
405- lambda_client .add_permission (
406- FunctionName = function_name ,
407- StatementId = "dex-invoke-permission" ,
408- Action = "lambda:InvokeFunction" ,
409- Principal = config ["invoke_account_id" ],
410- )
381+ # Update invoke permission for worker account using put_resource_policy
382+ function_arn = f"arn:aws:lambda:{ config ['region' ]} :{ config ['account_id' ]} :function:{ function_name } "
383+
384+ policy_document = {
385+ "Version" : "2012-10-17" ,
386+ "Statement" : [
387+ {
388+ "Sid" : "dex-invoke-permission" ,
389+ "Effect" : "Allow" ,
390+ "Principal" : {"AWS" : config ["invoke_account_id" ]},
391+ "Action" : "lambda:InvokeFunction" ,
392+ "Resource" : f"{ function_arn } :*"
393+ }
394+ ]
395+ }
411396
412- except lambda_client .exceptions .ResourceNotFoundException :
413- # No policy exists, add permission
414- lambda_client .add_permission (
415- FunctionName = function_name ,
416- StatementId = "dex-invoke-permission" ,
417- Action = "lambda:InvokeFunction" ,
418- Principal = config ["invoke_account_id" ],
419- )
397+ lambda_client .put_resource_policy (
398+ ResourceArn = function_arn ,
399+ Policy = json .dumps (policy_document )
400+ )
420401
421402 logger .info ("Function deployed successfully! %s" , function_name )
422403 return True
0 commit comments