@@ -11,6 +11,24 @@ class Command(ZappaCommand):
1111 requires_system_checks = False
1212
1313 help = '''Update the the lambda package for a given Zappa deployment.'''
14+
15+ def remove_old_version_lambda_functions (self ):
16+ client = self .zappa .boto_session .client ('lambda' )
17+ versions_response = client .list_versions_by_function (FunctionName = self .lambda_name )
18+ available_versions_list = []
19+ for version in versions_response ['Versions' ]:
20+ available_versions_list .append (version ['Version' ])
21+ print ("----- All Available Versions ------" )
22+ aliases_response = client .list_aliases (FunctionName = self .lambda_name )
23+ aliased_versions_list = []
24+ for alias in aliases_response ['Aliases' ]:
25+ aliased_versions_list .append (alias ['FunctionVersion' ])
26+ untagged_versions = set (available_versions_list ) - set (aliased_versions_list ) - {'$LATEST' }
27+ print ("Versions that are not aliased" , untagged_versions )
28+ for version in untagged_versions :
29+ print ("Deleting Untagged Versions" )
30+ print (client .delete_function (FunctionName = self .lambda_name , Qualifier = version ))
31+
1432
1533 def add_arguments (self , parser ):
1634 parser .add_argument ('environment' , nargs = '+' , type = str )
@@ -61,13 +79,17 @@ def handle(self, *args, **options): # NoQA
6179 lambda_arn = self .zappa .update_lambda_function (
6280 self .s3_bucket_name , self .zip_path , self .lambda_name )
6381
82+ # remove old version_lambda functions
83+ self .remove_old_version_lambda_functions ()
84+
6485 # Remove the uploaded zip from S3, because it is now registered..
6586 self .zappa .remove_from_s3 (self .zip_path , self .s3_bucket_name )
6687
6788 # Finally, delete the local copy our zip package
6889 if self .zappa_settings [self .api_stage ].get ('delete_zip' , True ) and not options ['zip' ]:
6990 os .remove (self .zip_path )
7091
92+
7193 #Remove the local settings
7294 self .remove_s3_local_settings ()
7395
0 commit comments