Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Lambda function that deletes test Lambda functions #2960

Merged
4 changes: 4 additions & 0 deletions scripts/aws_lambda_functions/README.md
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)
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
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."
}
Loading