Skip to content

Commit 419a8e9

Browse files
committed
Add lambda layer resource cleanup.
1 parent 754f5ac commit 419a8e9

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

.github/workflows/resource-cleanup.yml

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,40 @@ jobs:
8181
python -m pip install -r requirements.txt
8282
python cleaner.py
8383
84+
cleanup-lambda-layer:
85+
runs-on: ubuntu-latest
86+
steps:
87+
- uses: actions/checkout@v3
88+
- uses: actions/setup-python@v5
89+
with:
90+
python-version: '3.10'
91+
92+
- name: Configure AWS credentials for IAD account access
93+
uses: aws-actions/configure-aws-credentials@v4
94+
with:
95+
role-to-assume: ${{ secrets.E2E_IAD_TEST_ACCOUNT_ARN }}
96+
aws-region: us-east-1
97+
98+
- name: Retrieve account id for the region
99+
uses: aws-actions/aws-secretsmanager-get-secrets@v1
100+
with:
101+
secret-ids:
102+
ACCOUNT_ID, region-account/${{ matrix.aws-region }}
103+
104+
- name: Configure AWS credentials for the regional account access
105+
uses: aws-actions/configure-aws-credentials@v4
106+
with:
107+
role-to-assume: arn:aws:iam::${{ env.ACCOUNT_ID }}:role/${{ secrets.RESOURCE_CLEANER_ROLE_NAME }}
108+
aws-region: ${{ matrix.aws-region }}
109+
110+
- name: Cleanup Lambda Layer
111+
working-directory: .github/workflows/util/clean/lambda_layer_cleanup
112+
env:
113+
AWS_DEFAULT_REGION: ${{ matrix.aws-region }}
114+
run: |
115+
python -m pip install -r requirements.txt
116+
python cleaner.py
117+
84118
publish-metric:
85119
needs: [ cleanup-ec2-instances, cleanup-k8s-cluster ]
86120
if: always()
@@ -89,4 +123,4 @@ jobs:
89123
with:
90124
aws-region: 'us-east-1'
91125
caller-workflow-name: 'enablement-test-resource-cleanup'
92-
validation-result: ${{ (needs.cleanup-ec2-instances.result == 'success' && needs.cleanup-k8s-cluster.result == 'success') && 'success' || 'failure' }}
126+
validation-result: ${{ (needs.cleanup-ec2-instances.result == 'success' && needs.cleanup-k8s-cluster.result == 'success' && needs.cleanup-lambda-layer.result == 'success') && 'success' || 'failure' }}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import boto3
2+
from datetime import datetime, timezone, timedelta
3+
import time
4+
5+
client = boto3.client('apigateway')
6+
7+
def delete_old_api_gateways(hours_old=3):
8+
now = datetime.now(timezone.utc) # Ensure `now` is timezone-aware
9+
cutoff_time = now - timedelta(hours=hours_old)
10+
11+
print(f"Cutoff time: {cutoff_time}")
12+
13+
apis = client.get_rest_apis()
14+
for api in apis.get('items', []):
15+
created_date = api.get('createdDate') # This is usually UTC already
16+
if created_date and isinstance(created_date, datetime):
17+
# Ensure `created_date` is timezone-aware
18+
created_date = created_date.astimezone(timezone.utc)
19+
20+
if created_date < cutoff_time:
21+
api_id = api['id']
22+
api_name = api.get('name', 'Unnamed API')
23+
print(f"Deleting API: {api_name} (ID: {api_id}), created at {created_date}")
24+
25+
client.delete_rest_api(restApiId=api_id)
26+
print("Deleted successfully. Sleeping for 32 seconds...")
27+
time.sleep(32)
28+
else:
29+
print("Invalid or missing createdDate for API:", api)
30+
31+
if __name__ == "__main__":
32+
delete_old_api_gateways()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boto3

0 commit comments

Comments
 (0)