File tree Expand file tree Collapse file tree 3 files changed +68
-1
lines changed
util/clean/lambda_layer_cleanup Expand file tree Collapse file tree 3 files changed +68
-1
lines changed Original file line number Diff line number Diff line change 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()
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' }}
Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change 1+ boto3
You can’t perform that action at this time.
0 commit comments