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.

70 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. endpoint_url = "http://localhost.localstack.cloud:4566"
  10. groupNr = 22
  11. currentYear = date.today().year
  12. globallyUniqueS3GroupBucketName = "cloudcomp-counter-" + str(currentYear) + "-group" + str(groupNr)
  13. # region = 'eu-central-1'
  14. region = 'us-east-1'
  15. functionName = 'cloudcomp-counter-lambda-demo'
  16. ################################################################################################
  17. #
  18. # boto3 code
  19. #
  20. ################################################################################################
  21. def cleanup_s3_bucket(s3_bucket):
  22. # Deleting objects
  23. for s3_object in s3_bucket.objects.all():
  24. s3_object.delete()
  25. # Deleting objects versions if S3 versioning enabled
  26. for s3_object_ver in s3_bucket.object_versions.all():
  27. s3_object_ver.delete()
  28. client = boto3.setup_default_session(region_name=region)
  29. s3Client = boto3.client('s3')
  30. s3Resource = boto3.resource('s3', endpoint_url=endpoint_url)
  31. lClient = boto3.client('lambda', endpoint_url=endpoint_url)
  32. apiClient = boto3.client("apigatewayv2", endpoint_url=endpoint_url)
  33. print("Deleting old API gateway...")
  34. print("------------------------------------")
  35. response = apiClient.get_apis()
  36. for api in response["Items"]:
  37. if api["Name"] == functionName + '-api':
  38. responseDelete = 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 ClientError as e:
  56. print(e)