You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.7 KiB

  1. from datetime import date
  2. import boto3
  3. from botocore.exceptions import ClientError
  4. ################################################################################################
  5. #
  6. # Configuration Parameters
  7. #
  8. ################################################################################################
  9. groupNr = 22
  10. currentYear = date.today().year
  11. globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-group" + str(groupNr)
  12. # region = 'eu-central-1'
  13. region = 'us-east-1'
  14. functionName = 'cloudcomp-counter-lambda-demo'
  15. ################################################################################################
  16. #
  17. # boto3 code
  18. #
  19. ################################################################################################
  20. def cleanup_s3_bucket(s3_bucket):
  21. # Deleting objects
  22. for s3_object in s3_bucket.objects.all():
  23. s3_object.delete()
  24. # Deleting objects versions if S3 versioning enabled
  25. for s3_object_ver in s3_bucket.object_versions.all():
  26. s3_object_ver.delete()
  27. client = boto3.setup_default_session(region_name=region)
  28. s3Client = boto3.client('s3')
  29. s3Resource = boto3.resource('s3')
  30. lClient = boto3.client('lambda')
  31. print("Deleting old function...")
  32. print("------------------------------------")
  33. try:
  34. response = lClient.delete_function(
  35. FunctionName=functionName,
  36. )
  37. except lClient.exceptions.ResourceNotFoundException:
  38. print('Function not available. No need to delete it.')
  39. print("Deleting old bucket...")
  40. print("------------------------------------")
  41. try:
  42. currentBucket = s3Resource.Bucket(globallyUniqueS3GroupBucketName)
  43. cleanup_s3_bucket(currentBucket)
  44. currentBucket.delete()
  45. except ClientError as e:
  46. print(e)