This repository was archived by the owner on Mar 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·122 lines (97 loc) · 3.97 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·122 lines (97 loc) · 3.97 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
set -e
PROJECT_NAME="autobiography-api"
REGION="ap-northeast-2"
STACK_NAME="${PROJECT_NAME}-ultra-minimal"
echo "🚀 Starting ultra-minimal deployment for ${PROJECT_NAME}"
echo "📍 Region: ${REGION}"
# Get AWS Account ID
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
echo "🏢 Account ID: ${ACCOUNT_ID}"
# Check if key pair exists, create if not
KEY_PAIR_NAME="autobiography-api-key"
if ! aws ec2 describe-key-pairs --key-names ${KEY_PAIR_NAME} --region ${REGION} >/dev/null 2>&1; then
echo "🔑 Creating EC2 Key Pair..."
aws ec2 create-key-pair --key-name ${KEY_PAIR_NAME} --region ${REGION} --query 'KeyMaterial' --output text > ${KEY_PAIR_NAME}.pem
chmod 400 ${KEY_PAIR_NAME}.pem
echo "✅ Key pair created and saved as ${KEY_PAIR_NAME}.pem"
else
echo "✅ Key pair ${KEY_PAIR_NAME} already exists"
fi
echo "📦 Step 1: Deploying infrastructure..."
aws cloudformation deploy \
--template-file aws-infrastructure-ultra-minimal.yaml \
--stack-name ${STACK_NAME} \
--parameter-overrides \
ProjectName=${PROJECT_NAME} \
KeyPairName=${KEY_PAIR_NAME} \
--capabilities CAPABILITY_IAM \
--region ${REGION}
if [ $? -ne 0 ]; then
echo "❌ Infrastructure deployment failed"
exit 1
fi
echo "✅ Infrastructure deployed successfully"
# Get stack outputs
echo "📋 Getting stack outputs..."
EC2_IP=$(aws cloudformation describe-stacks --stack-name ${STACK_NAME} --region ${REGION} --query 'Stacks[0].Outputs[?OutputKey==`EC2PublicIP`].OutputValue' --output text)
ECR_URI=$(aws cloudformation describe-stacks --stack-name ${STACK_NAME} --region ${REGION} --query 'Stacks[0].Outputs[?OutputKey==`ECRRepositoryURI`].OutputValue' --output text)
echo "🖥️ EC2 Public IP: ${EC2_IP}"
echo "📦 ECR Repository: ${ECR_URI}"
echo "📦 Step 2: Building and pushing Docker image..."
# Build Docker image
docker build -t ${PROJECT_NAME} .
# Login to ECR
aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${ECR_URI}
# Tag and push image
docker tag ${PROJECT_NAME}:latest ${ECR_URI}:latest
docker push ${ECR_URI}:latest
echo "✅ Docker image pushed to ECR"
echo "📦 Step 3: Deploying to EC2..."
# Wait for EC2 to be ready
echo "⏳ Waiting for EC2 instance to be ready..."
sleep 60
# Create deployment script
cat > deploy-to-ec2.sh << EOF
#!/bin/bash
set -e
echo "🔄 Pulling latest image from ECR..."
aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${ECR_URI}
docker pull ${ECR_URI}:latest
echo "🛑 Stopping existing container..."
docker stop ${PROJECT_NAME} 2>/dev/null || true
docker rm ${PROJECT_NAME} 2>/dev/null || true
echo "🚀 Starting new container..."
docker run -d \\
--name ${PROJECT_NAME} \\
--restart unless-stopped \\
-p 3000:3000 \\
-p 80:3000 \\
--env-file /home/ec2-user/app/.env \\
-v /home/ec2-user/app:/app/data \\
${ECR_URI}:latest
echo "✅ Application deployed successfully"
echo "🌐 API available at: http://${EC2_IP}:3000"
echo "🌐 Health check: http://${EC2_IP}:3000/health"
EOF
# Copy deployment script to EC2 and execute
scp -i ${KEY_PAIR_NAME}.pem -o StrictHostKeyChecking=no deploy-to-ec2.sh ec2-user@${EC2_IP}:/home/ec2-user/
ssh -i ${KEY_PAIR_NAME}.pem -o StrictHostKeyChecking=no ec2-user@${EC2_IP} 'chmod +x deploy-to-ec2.sh && ./deploy-to-ec2.sh'
# Clean up
rm deploy-to-ec2.sh
echo ""
echo "🎉 Deployment completed successfully!"
echo ""
echo "📋 Summary:"
echo " 🖥️ EC2 Instance: ${EC2_IP}"
echo " 🌐 API Endpoint: http://${EC2_IP}:3000"
echo " 🏥 Health Check: http://${EC2_IP}:3000/health"
echo " 📚 API Docs: http://${EC2_IP}:3000/api/v2/docs"
echo " 🔑 SSH Access: ssh -i ${KEY_PAIR_NAME}.pem ec2-user@${EC2_IP}"
echo ""
echo "🧪 Test the API:"
echo " curl http://${EC2_IP}:3000/health"
echo ""
echo "💰 Estimated Monthly Cost: ~\$8-10 (EC2 t3.micro + S3)"
echo "📝 Note: Using SQLite database (stored on EC2) instead of RDS for simplicity"
echo ""