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.

64 lines
2.1 KiB

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