Skip to content

Commit 6f17fab

Browse files
author
Yuriy Bezsonov
committed
fix(infra): Improve VPC endpoint deletion
1 parent 100bc9b commit 6f17fab

6 files changed

Lines changed: 144 additions & 135 deletions

File tree

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,24 +137,27 @@ def empty_bucket(bucket_name):
137137
print(f"Error emptying bucket {bucket_name}: {e}")
138138

139139
def wait_for_deletion(endpoint_ids, max_wait=300):
140-
"""Poll until endpoints are deleted or timeout."""
140+
"""Poll until endpoints are fully deleted (not just deleting) or timeout."""
141141
start_time = time.time()
142142

143143
while time.time() - start_time < max_wait:
144144
try:
145145
response = ec2.describe_vpc_endpoints(VpcEndpointIds=endpoint_ids)
146-
remaining = [ep for ep in response.get('VpcEndpoints', [])
147-
if ep['State'] not in ['deleted', 'deleting']]
146+
endpoints = response.get('VpcEndpoints', [])
147+
148+
# Wait for fully deleted state, not just deleting
149+
remaining = [ep for ep in endpoints if ep['State'] != 'deleted']
148150

149151
if not remaining:
150-
print("All endpoints deleted")
152+
print("All endpoints fully deleted")
151153
return
152154

153-
print(f"Waiting for {len(remaining)} endpoints to delete...")
155+
states = {ep['VpcEndpointId']: ep['State'] for ep in remaining}
156+
print(f"Waiting for endpoints: {states}")
154157
time.sleep(10)
155158
except ec2.exceptions.ClientError as e:
156159
if 'InvalidVpcEndpointId.NotFound' in str(e):
157-
print("All endpoints deleted")
160+
print("All endpoints deleted (not found)")
158161
return
159162
raise
160163

infra/cfn/base-stack.yaml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,22 @@ Resources:
676676
Fn::GetAtt:
677677
- IdeInstanceLauncherFunction803C5A2A
678678
- Arn
679+
SecurityGroupIds:
680+
Fn::Join:
681+
- ""
682+
- - Fn::GetAtt:
683+
- IdeSecurityGroup73B02454
684+
- GroupId
685+
- ","
686+
- Fn::GetAtt:
687+
- IdeInternalSecurityGroupB0A5D76B
688+
- GroupId
689+
SubnetIds:
690+
Fn::Join:
691+
- ""
692+
- - Ref: VpcPublicSubnet1Subnet8E8DEDC0
693+
- ","
694+
- Ref: VpcPublicSubnet2SubnetA811849C
679695
VolumeSize: "50"
680696
IamInstanceProfileArn:
681697
Fn::GetAtt:
@@ -821,22 +837,6 @@ Resources:
821837
fi
822838
ImageId:
823839
Ref: SsmParameterValueawsserviceamiamazonlinuxlatestal2023amikernel61arm64C96584B6F00A464EAD1953AFF4B05118Parameter
824-
SecurityGroupIds:
825-
Fn::Join:
826-
- ""
827-
- - Fn::GetAtt:
828-
- IdeSecurityGroup73B02454
829-
- GroupId
830-
- ","
831-
- Fn::GetAtt:
832-
- IdeInternalSecurityGroupB0A5D76B
833-
- GroupId
834-
SubnetIds:
835-
Fn::Join:
836-
- ""
837-
- - Ref: VpcPublicSubnet1Subnet8E8DEDC0
838-
- ","
839-
- Ref: VpcPublicSubnet2SubnetA811849C
840840
UpdateReplacePolicy: Delete
841841
DeletionPolicy: Delete
842842
IdeEipAssociationDFF81215:

infra/cfn/java-ai-agents-stack.yaml

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,29 @@ Resources:
757757
Fn::GetAtt:
758758
- IdeInstanceLauncherFunction803C5A2A
759759
- Arn
760+
SecurityGroupIds:
761+
Fn::Join:
762+
- ""
763+
- - Fn::GetAtt:
764+
- IdeSecurityGroup73B02454
765+
- GroupId
766+
- ","
767+
- Fn::GetAtt:
768+
- IdeInternalSecurityGroupB0A5D76B
769+
- GroupId
770+
SubnetIds:
771+
Fn::Join:
772+
- ""
773+
- - Ref: VpcPublicSubnet1Subnet8E8DEDC0
774+
- ","
775+
- Ref: VpcPublicSubnet2SubnetA811849C
776+
VolumeSize: "50"
777+
IamInstanceProfileArn:
778+
Fn::GetAtt:
779+
- IdeInstanceProfile61B92038
780+
- Arn
781+
InstanceName: ide
782+
InstanceTypes: m7g.xlarge,m6g.xlarge,c7g.xlarge,t4g.xlarge
760783
UserData:
761784
Fn::Base64:
762785
Fn::Join:
@@ -895,29 +918,6 @@ Resources:
895918
fi
896919
ImageId:
897920
Ref: SsmParameterValueawsserviceamiamazonlinuxlatestal2023amikernel61arm64C96584B6F00A464EAD1953AFF4B05118Parameter
898-
SecurityGroupIds:
899-
Fn::Join:
900-
- ""
901-
- - Fn::GetAtt:
902-
- IdeSecurityGroup73B02454
903-
- GroupId
904-
- ","
905-
- Fn::GetAtt:
906-
- IdeInternalSecurityGroupB0A5D76B
907-
- GroupId
908-
SubnetIds:
909-
Fn::Join:
910-
- ""
911-
- - Ref: VpcPublicSubnet1Subnet8E8DEDC0
912-
- ","
913-
- Ref: VpcPublicSubnet2SubnetA811849C
914-
VolumeSize: "50"
915-
IamInstanceProfileArn:
916-
Fn::GetAtt:
917-
- IdeInstanceProfile61B92038
918-
- Arn
919-
InstanceName: ide
920-
InstanceTypes: m7g.xlarge,m6g.xlarge,c7g.xlarge,t4g.xlarge
921921
UpdateReplacePolicy: Delete
922922
DeletionPolicy: Delete
923923
IdeEipAssociationDFF81215:

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

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -777,27 +777,6 @@ Resources:
777777
Fn::GetAtt:
778778
- IdeInstanceLauncherFunction803C5A2A
779779
- Arn
780-
IamInstanceProfileArn:
781-
Fn::GetAtt:
782-
- IdeInstanceProfile61B92038
783-
- Arn
784-
VolumeSize: "50"
785-
SubnetIds:
786-
Fn::Join:
787-
- ""
788-
- - Ref: VpcPublicSubnet1Subnet8E8DEDC0
789-
- ","
790-
- Ref: VpcPublicSubnet2SubnetA811849C
791-
SecurityGroupIds:
792-
Fn::Join:
793-
- ""
794-
- - Fn::GetAtt:
795-
- IdeSecurityGroup73B02454
796-
- GroupId
797-
- ","
798-
- Fn::GetAtt:
799-
- IdeInternalSecurityGroupB0A5D76B
800-
- GroupId
801780
ImageId:
802781
Ref: SsmParameterValueawsserviceamiamazonlinuxlatestal2023amikernel61arm64C96584B6F00A464EAD1953AFF4B05118Parameter
803782
UserData:
@@ -938,6 +917,27 @@ Resources:
938917
fi
939918
InstanceTypes: m7g.xlarge,m6g.xlarge,c7g.xlarge,t4g.xlarge
940919
InstanceName: ide
920+
IamInstanceProfileArn:
921+
Fn::GetAtt:
922+
- IdeInstanceProfile61B92038
923+
- Arn
924+
VolumeSize: "50"
925+
SubnetIds:
926+
Fn::Join:
927+
- ""
928+
- - Ref: VpcPublicSubnet1Subnet8E8DEDC0
929+
- ","
930+
- Ref: VpcPublicSubnet2SubnetA811849C
931+
SecurityGroupIds:
932+
Fn::Join:
933+
- ""
934+
- - Fn::GetAtt:
935+
- IdeSecurityGroup73B02454
936+
- GroupId
937+
- ","
938+
- Fn::GetAtt:
939+
- IdeInternalSecurityGroupB0A5D76B
940+
- GroupId
941941
UpdateReplacePolicy: Delete
942942
DeletionPolicy: Delete
943943
IdeEipAssociationDFF81215:
@@ -1316,12 +1316,12 @@ Resources:
13161316
Environment:
13171317
ComputeType: BUILD_GENERAL1_MEDIUM
13181318
EnvironmentVariables:
1319-
- Name: TEMPLATE_TYPE
1320-
Type: PLAINTEXT
1321-
Value: java-on-amazon-eks
13221319
- Name: GIT_BRANCH
13231320
Type: PLAINTEXT
13241321
Value: new-ws-infra
1322+
- Name: TEMPLATE_TYPE
1323+
Type: PLAINTEXT
1324+
Value: java-on-amazon-eks
13251325
Image: aws/codebuild/amazonlinux2-x86_64-standard:5.0
13261326
ImagePullCredentialsType: CODEBUILD
13271327
PrivilegedMode: false
@@ -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: "1766319416865"
15661567
ProjectName:
15671568
Ref: CodeBuildProjectA0FF5539
15681569
CodeBuildIamRoleArn:
15691570
Fn::GetAtt:
15701571
- CodeBuildRoleE9A44575
15711572
- Arn
1572-
ContentHash: "1766262434985"
15731573
DependsOn:
15741574
- CodeBuildCompleteRuleAllowEventRuleWorkshopStackCodeBuildReportLambdaFunctionD77C60919E0B0C89
15751575
- CodeBuildCompleteRuleEE9277E8
@@ -1921,7 +1921,7 @@ Resources:
19211921
- Ref: AWS::AccountId
19221922
- "-"
19231923
- Ref: AWS::Region
1924-
- "-20251220212715"
1924+
- "-20251221131657"
19251925
PublicAccessBlockConfiguration:
19261926
BlockPublicAcls: true
19271927
BlockPublicPolicy: true
@@ -2157,15 +2157,15 @@ Resources:
21572157
}
21582158
Environment:
21592159
Variables:
2160-
EKS_CLUSTER_NAME:
2161-
Ref: EksClusterB2BDED5B
21622160
S3_BUCKET_NAME:
21632161
Ref: WorkshopBucketFD5BC43F
21642162
SECRET_NAME: workshop-ide-password
21652163
KUBERNETES_AUTH_TYPE: aws
21662164
APP_LABEL: unicorn-store-spring
21672165
K8S_NAMESPACE: unicorn-store-spring
21682166
S3_THREAD_DUMPS_PREFIX: thread-dumps/
2167+
EKS_CLUSTER_NAME:
2168+
Ref: EksClusterB2BDED5B
21692169
FunctionName: workshop-thread-dump-lambda
21702170
Handler: index.lambda_handler
21712171
MemorySize: 512
@@ -2939,24 +2939,27 @@ Resources:
29392939
print(f"Error emptying bucket {bucket_name}: {e}")
29402940
29412941
def wait_for_deletion(endpoint_ids, max_wait=300):
2942-
"""Poll until endpoints are deleted or timeout."""
2942+
"""Poll until endpoints are fully deleted (not just deleting) or timeout."""
29432943
start_time = time.time()
29442944
29452945
while time.time() - start_time < max_wait:
29462946
try:
29472947
response = ec2.describe_vpc_endpoints(VpcEndpointIds=endpoint_ids)
2948-
remaining = [ep for ep in response.get('VpcEndpoints', [])
2949-
if ep['State'] not in ['deleted', 'deleting']]
2948+
endpoints = response.get('VpcEndpoints', [])
2949+
2950+
# Wait for fully deleted state, not just deleting
2951+
remaining = [ep for ep in endpoints if ep['State'] != 'deleted']
29502952
29512953
if not remaining:
2952-
print("All endpoints deleted")
2954+
print("All endpoints fully deleted")
29532955
return
29542956
2955-
print(f"Waiting for {len(remaining)} endpoints to delete...")
2957+
states = {ep['VpcEndpointId']: ep['State'] for ep in remaining}
2958+
print(f"Waiting for endpoints: {states}")
29562959
time.sleep(10)
29572960
except ec2.exceptions.ClientError as e:
29582961
if 'InvalidVpcEndpointId.NotFound' in str(e):
2959-
print("All endpoints deleted")
2962+
print("All endpoints deleted (not found)")
29602963
return
29612964
raise
29622965

0 commit comments

Comments
 (0)