From a626f013be03a363d130bc2d20e80734226aa33a Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 25 Apr 2024 16:21:02 +0200 Subject: [PATCH] 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 --- scripts/aws_lambda_functions/README.md | 4 ++ .../sentryPythonDeleteTestFunctions/README.md | 13 +++++ .../lambda_function.py | 55 +++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 scripts/aws_lambda_functions/README.md create mode 100644 scripts/aws_lambda_functions/sentryPythonDeleteTestFunctions/README.md create mode 100644 scripts/aws_lambda_functions/sentryPythonDeleteTestFunctions/lambda_function.py diff --git a/scripts/aws_lambda_functions/README.md b/scripts/aws_lambda_functions/README.md new file mode 100644 index 0000000000..e07b445d5b --- /dev/null +++ b/scripts/aws_lambda_functions/README.md @@ -0,0 +1,4 @@ +aws_lambda_functions +==================== + +In this directory you can place AWS Lambda functions that are used for administrative tasks (or whatever) \ No newline at end of file diff --git a/scripts/aws_lambda_functions/sentryPythonDeleteTestFunctions/README.md b/scripts/aws_lambda_functions/sentryPythonDeleteTestFunctions/README.md new file mode 100644 index 0000000000..de1120a026 --- /dev/null +++ b/scripts/aws_lambda_functions/sentryPythonDeleteTestFunctions/README.md @@ -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 \ No newline at end of file diff --git a/scripts/aws_lambda_functions/sentryPythonDeleteTestFunctions/lambda_function.py b/scripts/aws_lambda_functions/sentryPythonDeleteTestFunctions/lambda_function.py new file mode 100644 index 0000000000..1fc3994176 --- /dev/null +++ b/scripts/aws_lambda_functions/sentryPythonDeleteTestFunctions/lambda_function.py @@ -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." + }