Skip to content

Commit

Permalink
Add Lambda function that deletes test Lambda functions (#2960)
Browse files Browse the repository at this point in the history
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 <ivana.kellyerova@sentry.io>
  • Loading branch information
antonpirker and sentrivana committed Apr 25, 2024
1 parent f5db9ce commit a626f01
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
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."
}

0 comments on commit a626f01

Please sign in to comment.