From f9fb645db2b0f03d0191298a810c44b9e12b752f Mon Sep 17 00:00:00 2001 From: Lefteris Karageorgiou Date: Thu, 22 Aug 2024 14:26:55 +0300 Subject: [PATCH 1/2] delete vpc peering in pre destroy hook --- labs/unicorn-store/delete-vpc-peering.sh | 0 .../com/unicorn/core/InfrastructureStack.java | 28 ++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 labs/unicorn-store/delete-vpc-peering.sh diff --git a/labs/unicorn-store/delete-vpc-peering.sh b/labs/unicorn-store/delete-vpc-peering.sh new file mode 100644 index 000000000..e69de29bb diff --git a/labs/unicorn-store/infrastructure/cdk/src/main/java/com/unicorn/core/InfrastructureStack.java b/labs/unicorn-store/infrastructure/cdk/src/main/java/com/unicorn/core/InfrastructureStack.java index 0845d5dfb..5c0944a9c 100644 --- a/labs/unicorn-store/infrastructure/cdk/src/main/java/com/unicorn/core/InfrastructureStack.java +++ b/labs/unicorn-store/infrastructure/cdk/src/main/java/com/unicorn/core/InfrastructureStack.java @@ -7,7 +7,11 @@ import software.amazon.awscdk.services.events.EventBus; import software.amazon.awscdk.services.rds.*; import software.constructs.Construct; +import software.amazon.awscdk.Aspects; +import software.amazon.awscdk.IAspect; +import software.constructs.IConstruct; +import java.io.IOException; import java.util.List; public class InfrastructureStack extends Stack { @@ -36,9 +40,10 @@ public InfrastructureStack(final Construct scope, final String id, final StackPr createEventBridgeVpcEndpoint(); createDynamoDBVpcEndpoint(); new DatabaseSetupConstruct(this, "UnicornDatabaseConstruct"); + + Aspects.of(this).add(new PreDestroyHook()); } - private EventBus createEventBus() { return EventBus.Builder.create(this, "UnicornEventBus") .eventBusName("unicorns") @@ -136,4 +141,25 @@ private IGatewayVpcEndpoint createDynamoDBVpcEndpoint() { .build(); } + private void runCustomScript(String scriptPath) { + try { + Process process = Runtime.getRuntime().exec(scriptPath); + int exitCode = process.waitFor(); + if (exitCode != 0) { + System.err.println("Error running custom script. Exit code: " + exitCode); + } + } catch (IOException | InterruptedException e) { + System.err.println("Error running custom script: " + e.getMessage()); + } + } + + class PreDestroyHook implements IAspect { + @Override + public void visit(IConstruct node) { + if (node instanceof software.amazon.awscdk.Stack) { + runCustomScript("/home/ec2-user/environment/aws-lambda-java-workshop/labs/unicorn-store/delete-vpc-peering.sh"); + } + } + } + } From 8877c2bd7620440d780fe56bb833d8621cc8b63f Mon Sep 17 00:00:00 2001 From: Lefteris Karageorgiou Date: Thu, 22 Aug 2024 15:04:13 +0300 Subject: [PATCH 2/2] added delete vpc peering script --- labs/unicorn-store/delete-vpc-peering.sh | 7 +++++++ 1 file changed, 7 insertions(+) mode change 100644 => 100755 labs/unicorn-store/delete-vpc-peering.sh diff --git a/labs/unicorn-store/delete-vpc-peering.sh b/labs/unicorn-store/delete-vpc-peering.sh old mode 100644 new mode 100755 index e69de29bb..5614abb3f --- a/labs/unicorn-store/delete-vpc-peering.sh +++ b/labs/unicorn-store/delete-vpc-peering.sh @@ -0,0 +1,7 @@ +#bin/sh + +UNICORN_VPC_ID=$(aws cloudformation describe-stacks --stack-name UnicornStoreInfrastructure --query 'Stacks[0].Outputs[?OutputKey==`UnicornStoreVpcId`].OutputValue' --output text) + +VPC_PEERING_CONNECTION_ID=$(aws ec2 describe-vpc-peering-connections --filters "Name=accepter-vpc-info.vpc-id,Values=$UNICORN_VPC_ID" --query 'VpcPeeringConnections[0].VpcPeeringConnectionId' --output text) + +aws ec2 delete-vpc-peering-connection --vpc-peering-connection-id $VPC_PEERING_CONNECTION_ID