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.

63 lines
2.0 KiB

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