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.

69 lines
2.1 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. apiClient = boto3.client("apigatewayv2")
  32. print("Searching for old API gateway...")
  33. print("------------------------------------")
  34. for api in apiClient.get_apis()["Items"]:
  35. if api["Name"] == functionName + '-api':
  36. print("Deleting old API gateway...")
  37. print("------------------------------------")
  38. response = apiClient.delete_api(
  39. ApiId=api["ApiId"],
  40. )
  41. print("Deleting old function...")
  42. print("------------------------------------")
  43. try:
  44. response = lClient.delete_function(
  45. FunctionName=functionName,
  46. )
  47. except lClient.exceptions.ResourceNotFoundException:
  48. print('Function not available. No need to delete it.')
  49. print("Deleting old bucket...")
  50. print("------------------------------------")
  51. try:
  52. currentBucket = s3Resource.Bucket(globallyUniqueS3GroupBucketName)
  53. cleanup_s3_bucket(currentBucket)
  54. currentBucket.delete()
  55. except s3Client.exceptions.NoSuchBucket:
  56. print('Bucket not available. No need to delete it.')