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.

76 lines
2.5 KiB

  1. from datetime import date
  2. import boto3
  3. ################################################################################################
  4. #
  5. # Configuration Parameters
  6. #
  7. ################################################################################################
  8. endpoint_url = "http://localhost.localstack.cloud:4566"
  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. # roleName = 'arn:aws:iam::309000625112:role/service-role/cloudcomp-counter-demo-role-6rs7pah3'
  19. roleName = 'arn:aws:iam::919927306708:role/cloudcomp-s3-access'
  20. ################################################################################################
  21. #
  22. # boto3 code
  23. #
  24. ################################################################################################
  25. def cleanup_s3_bucket(s3_bucket):
  26. # Deleting objects
  27. for s3_object in s3_bucket.objects.all():
  28. s3_object.delete()
  29. # Deleting objects versions if S3 versioning enabled
  30. for s3_object_ver in s3_bucket.object_versions.all():
  31. s3_object_ver.delete()
  32. client = boto3.setup_default_session(region_name=region)
  33. s3Client = boto3.client('s3')
  34. s3Resource = boto3.resource('s3', endpoint_url=endpoint_url)
  35. lClient = boto3.client('lambda', endpoint_url=endpoint_url)
  36. apiClient = boto3.client("apigatewayv2", endpoint_url=endpoint_url)
  37. print("Deleting old API gateway...")
  38. print("------------------------------------")
  39. response = apiClient.get_apis()
  40. for api in response["Items"]:
  41. if api["Name"] == functionName + '-api':
  42. responseDelete = apiClient.delete_api(
  43. ApiId=api["ApiId"]
  44. )
  45. print("Deleting old function...")
  46. print("------------------------------------")
  47. try:
  48. response = lClient.delete_function(
  49. FunctionName=functionName,
  50. )
  51. except lClient.exceptions.ResourceNotFoundException:
  52. print('Function not available. No need to delete it.')
  53. print("Deleting old bucket...")
  54. print("------------------------------------")
  55. try:
  56. currentBucket = s3Resource.Bucket(globallyUniqueS3GroupBucketName)
  57. cleanup_s3_bucket(currentBucket)
  58. currentBucket.delete()
  59. except ClientError as e:
  60. print(e)