-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lambda layer resource cleanup. (#339)
*Description of changes:* Add lambda layer resource cleanup script. *Rollback procedure:* Revert the commit. <Can we safely revert this commit if needed? If not, detail what must be done to safely revert and why it is needed.> *Ensure you've run the following tests on your changes and include the link below:* Test workflow passed: https://github.com/aws-observability/aws-application-signals-test-framework/actions/runs/12485707531/job/34844864812 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
- Loading branch information
Showing
3 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 |