Skip to content

Commit 70392c3

Browse files
committed
Create a log group cleanup script
1 parent b4b8fde commit 70392c3

File tree

3 files changed

+72
-21
lines changed

3 files changed

+72
-21
lines changed

.github/workflows/terraform-destroy-environment-manual.yml

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,53 @@
11
# .github/workflows/destroy.yml
22

3-
name: 'Destroy (Select Account) Environment'
3+
name: "Destroy (Select Account) Environment"
44

55
on:
66
workflow_dispatch:
77
inputs:
88
build_branch:
9-
default: 'main'
10-
description: 'Branch to use for the destroy action.'
9+
default: "main"
10+
description: "Branch to use for the destroy action."
1111
required: true
1212
sandbox_workspace:
13-
description: 'The sandbox workspace to destroy.'
13+
description: "The sandbox workspace to destroy."
1414
required: true
1515
terraform_vars:
16-
default: 'dev.tfvars'
17-
description: 'Terraform vars file to use.'
16+
default: "dev.tfvars"
17+
description: "Terraform vars file to use."
1818
required: true
1919
environment:
20-
default: 'development'
21-
description: 'Environment for destruction.'
20+
default: "development"
21+
description: "Environment for destruction."
2222
required: true
2323
backend:
24-
default: 'backend.conf'
25-
description: 'Terraform backend configuration.'
24+
default: "backend.conf"
25+
description: "Terraform backend configuration."
2626
required: true
2727
workflow_call:
2828
inputs:
2929
build_branch:
30-
default: 'main'
31-
description: 'Branch to use for the destroy action.'
30+
default: "main"
31+
description: "Branch to use for the destroy action."
3232
required: true
3333
type: "string"
3434
sandbox_workspace:
35-
description: 'The sandbox workspace to destroy.'
35+
description: "The sandbox workspace to destroy."
3636
required: true
3737
type: "string"
3838
terraform_vars:
39-
default: 'dev.tfvars'
40-
description: 'Terraform vars file to use.'
39+
default: "dev.tfvars"
40+
description: "Terraform vars file to use."
4141
required: true
4242
type: "string"
4343
environment:
44-
default: 'development'
45-
description: 'Environment for destruction.'
44+
default: "development"
45+
description: "Environment for destruction."
4646
required: true
4747
type: "string"
4848
backend:
49-
default: 'backend.conf'
50-
description: 'Terraform backend configuration.'
49+
default: "backend.conf"
50+
description: "Terraform backend configuration."
5151
required: true
5252
type: "string"
5353

@@ -62,7 +62,7 @@ jobs:
6262
uses: ./.github/workflows/cleanup-cloudfront-edge-associations.yml
6363
with:
6464
sandbox_workspace: ${{ inputs.sandbox_workspace }}
65-
lambda_function_name: '${{ inputs.sandbox_workspace }}_EdgePresignLambda'
65+
lambda_function_name: "${{ inputs.sandbox_workspace }}_EdgePresignLambda"
6666
python_version: 3.11
6767
build_branch: ${{ inputs.build_branch }}
6868
environment: ${{ inputs.environment}}
@@ -131,3 +131,6 @@ jobs:
131131

132132
- name: Run Terraform Workspace Cleanup Script
133133
run: ./venv/bin/python3 -u scripts/cleanup_terraform_states.py ${{ inputs.sandbox_workspace }}
134+
135+
- name: Run Log Group Cleanup Script
136+
run: ./venv/bin/python3 -u scripts/cleanup_log_groups.py ${{ inputs.sandbox_workspace }}

infrastructure/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ provider "aws" {
4747
}
4848
}
4949

50-
resource "aws_resourcegroups_group" "resource_group" {
50+
resource "aws_resourcegroups_group" "workspace_resource_group" {
5151
name = "${terraform.workspace}-resource_group"
5252
description = "${terraform.workspace} workspace resource group."
5353
tags = {

scripts/cleanup_log_groups.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys
2+
3+
import boto3
4+
from botocore.exceptions import ClientError
5+
6+
7+
class CleanupLogGroups:
8+
9+
def __init__(self):
10+
self.logs_client = boto3.client("logs")
11+
12+
def main(self, sandbox: str):
13+
try:
14+
paginator = self.logs_client.get_paginator("describe_log_groups")
15+
log_groups_to_delete = []
16+
17+
for page in paginator.paginate():
18+
for log_group in page.get("logGroups", []):
19+
log_group_name = log_group["logGroupName"]
20+
if sandbox in log_group_name:
21+
log_groups_to_delete.append(log_group_name)
22+
23+
if not log_groups_to_delete:
24+
print(f"No log groups found matching pattern: {sandbox}")
25+
return
26+
27+
for log_group_name in log_groups_to_delete:
28+
print(f"Found log group: {log_group_name}")
29+
# try:
30+
# self.logs_client.delete_log_group(logGroupName=log_group_name)
31+
# print(f"Deleted log group: {log_group_name}")
32+
# except ClientError as e:
33+
# print(f"Failed to delete log group {log_group_name}: {e}")
34+
35+
except ClientError as e:
36+
print(f"Error during log group cleanup: {e}")
37+
38+
39+
if __name__ == "__main__":
40+
sandbox = sys.argv[1]
41+
exclude_list = ["ndr-dev"]
42+
43+
if sandbox in exclude_list:
44+
print("Cleanup log groups. Cannot delete protected environment")
45+
sys.exit(1)
46+
47+
print(f"Attempting to cleanup the log_groups for: {sandbox}")
48+
CleanupLogGroups().main(sandbox=sandbox)

0 commit comments

Comments
 (0)