-
Notifications
You must be signed in to change notification settings - Fork 513
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Lambda function that deletes test Lambda functions (#2960)
Lambda function that deletes all test lambda functions (prefixed with `test_`) that have been created during CI runs. * Lambda function that deletes test Lambda functions. This function is run every Sunday and it is monitored in the `sentry-python` project on Sentry.io with the Crons feature and it is also emitting metrics on how many functions it deletes in each run. --------- Co-authored-by: Ivana Kellyerova <[email protected]>
- Loading branch information
1 parent
f5db9ce
commit a626f01
Showing
3 changed files
with
72 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
aws_lambda_functions | ||
==================== | ||
|
||
In this directory you can place AWS Lambda functions that are used for administrative tasks (or whatever) |
13 changes: 13 additions & 0 deletions
13
scripts/aws_lambda_functions/sentryPythonDeleteTestFunctions/README.md
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,13 @@ | ||
sentryPythonDeleteTestFunctions | ||
=============================== | ||
|
||
This AWS Lambda function deletes all AWS Lambda functions in the current AWS account that are prefixed with `test_`. | ||
The functions that are deleted are created by the Google Actions CI checks running on every PR of the `sentry-python` repository. | ||
|
||
The Lambda function has been deployed here: | ||
- AWS Account ID: `943013980633` | ||
- Region: `us-east-1` | ||
- Function ARN: `arn:aws:lambda:us-east-1:943013980633:function:sentryPythonDeleteTestFunctions` | ||
|
||
This function also emits Sentry Metrics and Sentry Crons checkins to the `sentry-python` project in the `Sentry SDKs` organisation on Sentry.io: | ||
https://sentry-sdks.sentry.io/projects/sentry-python/?project=5461230 |
55 changes: 55 additions & 0 deletions
55
scripts/aws_lambda_functions/sentryPythonDeleteTestFunctions/lambda_function.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,55 @@ | ||
import boto3 | ||
import sentry_sdk | ||
|
||
|
||
monitor_slug = "python-sdk-aws-lambda-tests-cleanup" | ||
monitor_config = { | ||
"schedule": { | ||
"type": "crontab", | ||
"value": "0 12 * * 0", # 12 o'clock on Sunday | ||
}, | ||
"timezone": "UTC", | ||
"checkin_margin": 2, | ||
"max_runtime": 20, | ||
"failure_issue_threshold": 1, | ||
"recovery_threshold": 1, | ||
} | ||
|
||
|
||
@sentry_sdk.crons.monitor(monitor_slug=monitor_slug) | ||
def delete_lambda_functions(prefix="test_"): | ||
""" | ||
Delete all AWS Lambda functions in the current account | ||
where the function name matches the prefix | ||
""" | ||
client = boto3.client("lambda", region_name="us-east-1") | ||
functions_deleted = 0 | ||
|
||
functions_paginator = client.get_paginator("list_functions") | ||
for functions_page in functions_paginator.paginate(): | ||
for func in functions_page["Functions"]: | ||
function_name = func["FunctionName"] | ||
if function_name.startswith(prefix): | ||
try: | ||
response = client.delete_function( | ||
FunctionName=func["FunctionArn"], | ||
) | ||
functions_deleted += 1 | ||
except Exception as ex: | ||
print(f"Got exception: {ex}") | ||
|
||
return functions_deleted | ||
|
||
|
||
def lambda_handler(event, context): | ||
functions_deleted = delete_lambda_functions() | ||
|
||
sentry_sdk.metrics.gauge( | ||
key="num_aws_functions_deleted", | ||
value=functions_deleted, | ||
) | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': f"{functions_deleted} AWS Lambda functions deleted successfully." | ||
} |