generated from amazon-archives/__template_Apache-2.0
-
Couldn't load subscription status.
- Fork 23
Add lambda layer resource cleanup. #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
.github/workflows/util/clean/lambda_layer_cleanup/cleaner.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import boto3 | ||
| from botocore.exceptions import ClientError | ||
| from datetime import datetime, timezone, timedelta | ||
| import time | ||
|
|
||
| client = boto3.client('apigateway') | ||
|
|
||
| def delete_api_with_retries(client, api_id, retries=5): | ||
| """Deletes an API with retries and exponential backoff.""" | ||
| delay = 10 | ||
| for attempt in range(retries): | ||
| try: | ||
| client.delete_rest_api(restApiId=api_id) | ||
| print(f"API {api_id} deleted successfully. Sleeping for 32 seconds...") | ||
| time.sleep(32) | ||
| return | ||
| except ClientError as e: | ||
| if e.response['Error']['Code'] == 'TooManyRequestsException': | ||
| print(f"Rate limit exceeded. Retrying in {delay} seconds (Attempt {attempt + 1}/{retries})...") | ||
| time.sleep(delay) | ||
| delay *= 2 # Exponential backoff | ||
| else: | ||
| print(f"Error deleting API {api_id}: {e}") | ||
| raise # Re-raise other exceptions | ||
| print(f"Failed to delete API {api_id} after {retries} attempts.") | ||
|
|
||
| def delete_old_api_gateways(hours_old=3, batch_size=5): | ||
| """Deletes API Gateways older than the specified hours in batches.""" | ||
| now = datetime.now(timezone.utc) # Ensure `now` is timezone-aware | ||
| cutoff_time = now - timedelta(hours=hours_old) | ||
|
|
||
| print(f"Cutoff time: {cutoff_time}") | ||
|
|
||
| # Fetch all APIs | ||
| apis = client.get_rest_apis() | ||
| batch_counter = 0 | ||
|
|
||
| for api in apis.get('items', []): | ||
| created_date = api.get('createdDate') # This is usually UTC already | ||
| if created_date and isinstance(created_date, datetime): | ||
| # Ensure `created_date` is timezone-aware | ||
| created_date = created_date.astimezone(timezone.utc) | ||
|
|
||
| api_id = api['id'] | ||
| api_name = api.get('name', 'Unnamed API') | ||
| if "AdotLambda" in api_name and "SampleApp" in api_name and created_date < cutoff_time: | ||
| print(f"Preparing to delete API: {api_name} (ID: {api_id}), created at {created_date}") | ||
|
|
||
| # Attempt to delete the API with retries | ||
| delete_api_with_retries(client, api_id) | ||
|
|
||
| batch_counter += 1 | ||
|
|
||
| # Pause after every batch | ||
| if batch_counter % batch_size == 0: | ||
| print("Pausing for 2 minutes to avoid rate-limiting...") | ||
| time.sleep(120) | ||
| else: | ||
| print("Invalid or missing createdDate for API:", api) | ||
|
|
||
| if __name__ == "__main__": | ||
| delete_old_api_gateways() | ||
1 change: 1 addition & 0 deletions
1
.github/workflows/util/clean/lambda_layer_cleanup/requirements.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| boto3 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.