forked from codecentric/event-driven-microservices-platform
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdestroy.sh
More file actions
88 lines (72 loc) · 4.06 KB
/
destroy.sh
File metadata and controls
88 lines (72 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env bash
# destroy.sh – tears down the EDMP infrastructure deployed by deploy.sh
# -----------------------------------------------------------------------
# • Reads backend configuration from .terraform-backend-info
# • Executes terraform destroy -auto-approve
# • Optionally destroys the S3 bucket and DynamoDB table
# • Leaves the local SSH key-pair in place (delete manually if desired)
#
# Usage:
# $ ./destroy.sh [--clean-backend] # --clean-backend removes S3/DynamoDB
# -----------------------------------------------------------------------
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
CLEAN_BACKEND=false
if [[ "${1:-}" == "--clean-backend" ]]; then
CLEAN_BACKEND=true
fi
# ──────────────────────────────────────────────────────────────
# 1️⃣ read backend configuration
# ──────────────────────────────────────────────────────────────
if [[ ! -f ".terraform-backend-info" ]]; then
echo "❌ Backend info file not found. Run deploy.sh first."
exit 1
fi
source .terraform-backend-info
echo "🔧 Using backend configuration:"
echo " S3 Bucket: $BUCKET_NAME"
echo " DynamoDB: $DYNAMODB_TABLE"
echo " Region: $REGION"
# ──────────────────────────────────────────────────────────────
# 2️⃣ destroy terraform resources
# ──────────────────────────────────────────────────────────────
cd terraform
echo "⚠️ Destroying Terraform-managed resources..."
terraform init -upgrade -input=false >/dev/null
terraform destroy -auto-approve
# ──────────────────────────────────────────────────────────────
# 3️⃣ optionally clean up backend resources
# ──────────────────────────────────────────────────────────────
if [[ "$CLEAN_BACKEND" == "true" ]]; then
echo "🧹 Cleaning up backend resources..."
# Empty and delete S3 bucket
echo "📦 Emptying S3 bucket: $BUCKET_NAME"
aws s3 rm s3://"$BUCKET_NAME" --recursive --region "$REGION" 2>/dev/null || true
echo "📦 Deleting S3 bucket: $BUCKET_NAME"
aws s3api delete-bucket --bucket "$BUCKET_NAME" --region "$REGION" 2>/dev/null || true
# Delete DynamoDB table
echo "📊 Deleting DynamoDB table: $DYNAMODB_TABLE"
aws dynamodb delete-table --table-name "$DYNAMODB_TABLE" --region "$REGION" 2>/dev/null || true
echo "✅ Backend resources cleaned up"
# Remove backend info file
cd ..
rm -f .terraform-backend-info
rm -f terraform/backend.tf
else
echo "ℹ️ Backend resources preserved. Use --clean-backend to remove them."
fi
# ──────────────────────────────────────────────────────────────
# 4️⃣ completion message
# ──────────────────────────────────────────────────────────────
echo ""
echo "✅ Terraform destroy complete."
echo "🔑 Local SSH key-pair left untouched:"
echo " edmp-key (private key)"
echo " edmp-key.pub (public key)"
echo " Delete them manually if you no longer need SSH access."
if [[ "$CLEAN_BACKEND" == "false" ]]; then
echo ""
echo "💡 To completely remove all resources including backend state:"
echo " ./destroy.sh --clean-backend"
fi