-
Notifications
You must be signed in to change notification settings - Fork 554
Add Lambda function that deletes test Lambda functions #2960
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
Merged
antonpirker
merged 7 commits into
master
from
antonpirker/cron-for-deleting-aws-lambda-test-functions
Apr 25, 2024
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5d23b82
Lambda function that deletes test Lambda functions
antonpirker a684096
Merge branch 'master' into antonpirker/cron-for-deleting-aws-lambda-t…
antonpirker 6a26940
Update scripts/aws_lambda_functions/sentryPythonDeleteTestFunctions/R…
antonpirker bb70061
Merge branch 'master' into antonpirker/cron-for-deleting-aws-lambda-t…
antonpirker caea226
Merge branch 'master' into antonpirker/cron-for-deleting-aws-lambda-t…
antonpirker 46346d8
Merge branch 'master' into antonpirker/cron-for-deleting-aws-lambda-t…
antonpirker e6d6e1c
Merge branch 'master' into antonpirker/cron-for-deleting-aws-lambda-t…
antonpirker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 function 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 hidden or 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." | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.