Skip to content

Commit 9019f5d

Browse files
author
Yuriy Bezsonov
committed
Improve clean up
1 parent 8b2aaf3 commit 9019f5d

6 files changed

Lines changed: 91 additions & 171 deletions

File tree

infra/cdk/src/main/java/sample/com/constructs/CfnPreDeleteCleanup.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@ public CfnPreDeleteCleanup(final Construct scope, final String id, final CfnPreD
6666
.resources(List.of("*"))
6767
.build());
6868

69-
// Add CloudWatch Logs permissions for log group cleanup
70-
lambdaRole.addToPolicy(PolicyStatement.Builder.create()
71-
.effect(Effect.ALLOW)
72-
.actions(List.of(
73-
"logs:DescribeLogGroups",
74-
"logs:DeleteLogGroup"
75-
))
76-
.resources(List.of("*"))
77-
.build());
78-
7969
// Add S3 permissions for bucket cleanup
8070
lambdaRole.addToPolicy(PolicyStatement.Builder.create()
8171
.effect(Effect.ALLOW)

infra/cdk/src/main/resources/lambda/cfn-pre-delete-cleanup.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import cfnresponse
44

55
ec2 = boto3.client('ec2')
6-
logs = boto3.client('logs')
76
s3 = boto3.client('s3')
87
s3_resource = boto3.resource('s3')
98

@@ -12,8 +11,8 @@ def lambda_handler(event, context):
1211
Custom Resource handler to cleanup resources before stack deletion.
1312
- GuardDuty VPC endpoints that block VPC deletion
1413
- GuardDuty managed security groups
15-
- CloudWatch log groups with workshop- or unicornstore- prefix
1614
- S3 bucket contents for workshop- buckets
15+
Note: CloudWatch logs are kept for debugging/analysis
1716
"""
1817
print(f"Event: {event}")
1918

@@ -25,8 +24,7 @@ def lambda_handler(event, context):
2524
# Start VPC endpoint deletion (async)
2625
endpoint_ids = start_guardduty_endpoint_deletion(vpc_id)
2726

28-
# While endpoints are deleting, clean up logs and S3
29-
cleanup_cloudwatch_logs()
27+
# While endpoints are deleting, clean up S3
3028
cleanup_s3_buckets()
3129

3230
# Wait for VPC endpoint deletion to complete
@@ -105,26 +103,6 @@ def cleanup_guardduty_security_groups(vpc_id):
105103

106104
print("GuardDuty security group cleanup completed")
107105

108-
def cleanup_cloudwatch_logs():
109-
"""Delete CloudWatch log groups with workshop- or unicornstore- prefix."""
110-
prefixes = ['workshop-', 'unicornstore-', '/aws/lambda/workshop-', '/aws/lambda/unicornstore-']
111-
112-
for prefix in prefixes:
113-
try:
114-
paginator = logs.get_paginator('describe_log_groups')
115-
for page in paginator.paginate(logGroupNamePrefix=prefix):
116-
for log_group in page.get('logGroups', []):
117-
log_group_name = log_group['logGroupName']
118-
print(f"Deleting log group: {log_group_name}")
119-
try:
120-
logs.delete_log_group(logGroupName=log_group_name)
121-
except Exception as e:
122-
print(f"Error deleting log group {log_group_name}: {e}")
123-
except Exception as e:
124-
print(f"Error listing log groups with prefix {prefix}: {e}")
125-
126-
print("CloudWatch log cleanup completed")
127-
128106
def cleanup_s3_buckets():
129107
"""Empty S3 buckets with workshop- prefix."""
130108
try:

infra/cfn/base-stack.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -676,11 +676,6 @@ Resources:
676676
Fn::GetAtt:
677677
- IdeInstanceLauncherFunction803C5A2A
678678
- Arn
679-
InstanceName: ide
680-
IamInstanceProfileArn:
681-
Fn::GetAtt:
682-
- IdeInstanceProfile61B92038
683-
- Arn
684679
VolumeSize: "50"
685680
SubnetIds:
686681
Fn::Join:
@@ -837,6 +832,11 @@ Resources:
837832
exit 1
838833
fi
839834
InstanceTypes: m7g.xlarge,m6g.xlarge,c7g.xlarge,t4g.xlarge
835+
InstanceName: ide
836+
IamInstanceProfileArn:
837+
Fn::GetAtt:
838+
- IdeInstanceProfile61B92038
839+
- Arn
840840
UpdateReplacePolicy: Delete
841841
DeletionPolicy: Delete
842842
IdeEipAssociationDFF81215:

infra/cfn/java-on-amazon-eks-stack.yaml

Lines changed: 32 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,12 @@ Resources:
777777
Fn::GetAtt:
778778
- IdeInstanceLauncherFunction803C5A2A
779779
- Arn
780+
IamInstanceProfileArn:
781+
Fn::GetAtt:
782+
- IdeInstanceProfile61B92038
783+
- Arn
784+
InstanceName: ide
785+
InstanceTypes: m7g.xlarge,m6g.xlarge,c7g.xlarge,t4g.xlarge
780786
UserData:
781787
Fn::Base64:
782788
Fn::Join:
@@ -913,19 +919,8 @@ Resources:
913919
"
914920
exit 1
915921
fi
916-
InstanceTypes: m7g.xlarge,m6g.xlarge,c7g.xlarge,t4g.xlarge
917-
InstanceName: ide
918-
IamInstanceProfileArn:
919-
Fn::GetAtt:
920-
- IdeInstanceProfile61B92038
921-
- Arn
922-
VolumeSize: "50"
923-
SubnetIds:
924-
Fn::Join:
925-
- ""
926-
- - Ref: VpcPublicSubnet1Subnet8E8DEDC0
927-
- ","
928-
- Ref: VpcPublicSubnet2SubnetA811849C
922+
ImageId:
923+
Ref: SsmParameterValueawsserviceamiamazonlinuxlatestal2023amikernel61arm64C96584B6F00A464EAD1953AFF4B05118Parameter
929924
SecurityGroupIds:
930925
Fn::Join:
931926
- ""
@@ -936,8 +931,13 @@ Resources:
936931
- Fn::GetAtt:
937932
- IdeInternalSecurityGroupB0A5D76B
938933
- GroupId
939-
ImageId:
940-
Ref: SsmParameterValueawsserviceamiamazonlinuxlatestal2023amikernel61arm64C96584B6F00A464EAD1953AFF4B05118Parameter
934+
SubnetIds:
935+
Fn::Join:
936+
- ""
937+
- - Ref: VpcPublicSubnet1Subnet8E8DEDC0
938+
- ","
939+
- Ref: VpcPublicSubnet2SubnetA811849C
940+
VolumeSize: "50"
941941
UpdateReplacePolicy: Delete
942942
DeletionPolicy: Delete
943943
IdeEipAssociationDFF81215:
@@ -1526,12 +1526,12 @@ Resources:
15261526
Description: workshop-setup build complete
15271527
EventPattern:
15281528
detail:
1529-
project-name:
1530-
- Ref: CodeBuildProjectA0FF5539
15311529
build-status:
15321530
- SUCCEEDED
15331531
- FAILED
15341532
- STOPPED
1533+
project-name:
1534+
- Ref: CodeBuildProjectA0FF5539
15351535
detail-type:
15361536
- CodeBuild Build State Change
15371537
source:
@@ -1563,13 +1563,13 @@ Resources:
15631563
Fn::GetAtt:
15641564
- CodeBuildStartLambdaFunction8349284F
15651565
- Arn
1566-
ContentHash: "1766247608818"
1567-
ProjectName:
1568-
Ref: CodeBuildProjectA0FF5539
1566+
ContentHash: "1766254404082"
15691567
CodeBuildIamRoleArn:
15701568
Fn::GetAtt:
15711569
- CodeBuildRoleE9A44575
15721570
- Arn
1571+
ProjectName:
1572+
Ref: CodeBuildProjectA0FF5539
15731573
DependsOn:
15741574
- CodeBuildCompleteRuleAllowEventRuleWorkshopStackCodeBuildReportLambdaFunctionD77C60919E0B0C89
15751575
- CodeBuildCompleteRuleEE9277E8
@@ -1921,7 +1921,7 @@ Resources:
19211921
- Ref: AWS::AccountId
19221922
- "-"
19231923
- Ref: AWS::Region
1924-
- "-20251220172009"
1924+
- "-20251220191324"
19251925
PublicAccessBlockConfiguration:
19261926
BlockPublicAcls: true
19271927
BlockPublicPolicy: true
@@ -2157,15 +2157,15 @@ Resources:
21572157
}
21582158
Environment:
21592159
Variables:
2160-
KUBERNETES_AUTH_TYPE: aws
2161-
APP_LABEL: unicorn-store-spring
2162-
K8S_NAMESPACE: unicorn-store-spring
21632160
S3_THREAD_DUMPS_PREFIX: thread-dumps/
2164-
EKS_CLUSTER_NAME:
2165-
Ref: EksClusterB2BDED5B
2161+
K8S_NAMESPACE: unicorn-store-spring
2162+
APP_LABEL: unicorn-store-spring
2163+
KUBERNETES_AUTH_TYPE: aws
2164+
SECRET_NAME: workshop-ide-password
21662165
S3_BUCKET_NAME:
21672166
Ref: WorkshopBucketFD5BC43F
2168-
SECRET_NAME: workshop-ide-password
2167+
EKS_CLUSTER_NAME:
2168+
Ref: EksClusterB2BDED5B
21692169
FunctionName: workshop-thread-dump-lambda
21702170
Handler: index.lambda_handler
21712171
MemorySize: 512
@@ -2709,6 +2709,9 @@ Resources:
27092709
Fn::GetAtt:
27102710
- UnicornUnicornStoreDatabaseSetupFunction04E12F8B
27112711
- Arn
2712+
SqlStatements: |
2713+
CREATE TABLE IF NOT EXISTS unicorns(id TEXT DEFAULT gen_random_uuid() PRIMARY KEY, name TEXT, age TEXT, size TEXT, type TEXT);
2714+
CREATE EXTENSION IF NOT EXISTS vector;
27122715
SecretName:
27132716
Fn::Join:
27142717
- "-"
@@ -2739,9 +2742,6 @@ Resources:
27392742
- Fn::Split:
27402743
- ":"
27412744
- Ref: DatabaseSecret3B817195
2742-
SqlStatements: |
2743-
CREATE TABLE IF NOT EXISTS unicorns(id TEXT DEFAULT gen_random_uuid() PRIMARY KEY, name TEXT, age TEXT, size TEXT, type TEXT);
2744-
CREATE EXTENSION IF NOT EXISTS vector;
27452745
DependsOn:
27462746
- DatabaseClusterDatabaseWriterF4C0B9A6
27472747
- DatabaseCluster5B53A178
@@ -2774,8 +2774,6 @@ Resources:
27742774
- ec2:DeleteVpcEndpoints
27752775
- ec2:DescribeSecurityGroups
27762776
- ec2:DescribeVpcEndpoints
2777-
- logs:DeleteLogGroup
2778-
- logs:DescribeLogGroups
27792777
- s3:DeleteObject
27802778
- s3:DeleteObjectVersion
27812779
- s3:ListAllMyBuckets
@@ -2797,7 +2795,6 @@ Resources:
27972795
import cfnresponse
27982796
27992797
ec2 = boto3.client('ec2')
2800-
logs = boto3.client('logs')
28012798
s3 = boto3.client('s3')
28022799
s3_resource = boto3.resource('s3')
28032800
@@ -2806,8 +2803,8 @@ Resources:
28062803
Custom Resource handler to cleanup resources before stack deletion.
28072804
- GuardDuty VPC endpoints that block VPC deletion
28082805
- GuardDuty managed security groups
2809-
- CloudWatch log groups with workshop- or unicornstore- prefix
28102806
- S3 bucket contents for workshop- buckets
2807+
Note: CloudWatch logs are kept for debugging/analysis
28112808
"""
28122809
print(f"Event: {event}")
28132810
@@ -2819,8 +2816,7 @@ Resources:
28192816
# Start VPC endpoint deletion (async)
28202817
endpoint_ids = start_guardduty_endpoint_deletion(vpc_id)
28212818
2822-
# While endpoints are deleting, clean up logs and S3
2823-
cleanup_cloudwatch_logs()
2819+
# While endpoints are deleting, clean up S3
28242820
cleanup_s3_buckets()
28252821
28262822
# Wait for VPC endpoint deletion to complete
@@ -2899,26 +2895,6 @@ Resources:
28992895
29002896
print("GuardDuty security group cleanup completed")
29012897
2902-
def cleanup_cloudwatch_logs():
2903-
"""Delete CloudWatch log groups with workshop- or unicornstore- prefix."""
2904-
prefixes = ['workshop-', 'unicornstore-', '/aws/lambda/workshop-', '/aws/lambda/unicornstore-']
2905-
2906-
for prefix in prefixes:
2907-
try:
2908-
paginator = logs.get_paginator('describe_log_groups')
2909-
for page in paginator.paginate(logGroupNamePrefix=prefix):
2910-
for log_group in page.get('logGroups', []):
2911-
log_group_name = log_group['logGroupName']
2912-
print(f"Deleting log group: {log_group_name}")
2913-
try:
2914-
logs.delete_log_group(logGroupName=log_group_name)
2915-
except Exception as e:
2916-
print(f"Error deleting log group {log_group_name}: {e}")
2917-
except Exception as e:
2918-
print(f"Error listing log groups with prefix {prefix}: {e}")
2919-
2920-
print("CloudWatch log cleanup completed")
2921-
29222898
def cleanup_s3_buckets():
29232899
"""Empty S3 buckets with workshop- prefix."""
29242900
try:

0 commit comments

Comments
 (0)