Repository navigation
Expand file tree
/
Copy pathfast-ecr-deploy.sh
More file actions
executable file
Β·225 lines (191 loc) Β· 7.05 KB
/
Copy pathfast-ecr-deploy.sh
File metadata and controls
executable file
Β·225 lines (191 loc) Β· 7.05 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
# Fast Tabsur Deployment using ECR
set -e
echo "π Fast Tabsur Deployment using ECR"
# Load configuration
if [ -f "ec2-config.env" ]; then
source ec2-config.env
echo "π± Using instance: $EC2_INSTANCE_ID at $EC2_PUBLIC_IP"
else
echo "β Configuration file not found."
exit 1
fi
# Set AWS credentials
export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
export AWS_REGION=$AWS_REGION
echo "π AWS credentials configured"
# Create ECR repositories
echo "π¦ Creating ECR repositories..."
aws ecr create-repository --repository-name tabsur-server --region $AWS_REGION 2>/dev/null || echo "Repository tabsur-server already exists"
aws ecr create-repository --repository-name tabsur-client --region $AWS_REGION 2>/dev/null || echo "Repository tabsur-client already exists"
# Get ECR repository URIs
SERVER_ECR_URI=$(aws ecr describe-repositories --repository-names tabsur-server --query 'repositories[0].repositoryUri' --output text)
CLIENT_ECR_URI=$(aws ecr describe-repositories --repository-names tabsur-client --query 'repositories[0].repositoryUri' --output text)
echo "π¦ Server ECR: $SERVER_ECR_URI"
echo "π¦ Client ECR: $CLIENT_ECR_URI"
# Login to ECR
echo "π Logging into ECR..."
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $SERVER_ECR_URI
# Build and push images locally (much faster than on EC2)
echo "ποΈ Building Docker images locally..."
echo "Building server image..."
docker build -t tabsur-server -f Dockerfile.server.multistage .
docker tag tabsur-server:latest $SERVER_ECR_URI:latest
echo "Building client image..."
# Prefer API domain when it resolves; fallback to instance API
API_DOMAIN_HOST="https://api.bemyguest.dedyn.io"
if getent hosts api.bemyguest.dedyn.io >/dev/null 2>&1; then
CLIENT_API_BUILD_HOST="$API_DOMAIN_HOST"
else
CLIENT_API_BUILD_HOST="http://$EC2_PUBLIC_IP:5000"
fi
echo "Using client API host: $CLIENT_API_BUILD_HOST"
docker build \
--build-arg REACT_APP_SERVER_HOST="$CLIENT_API_BUILD_HOST" \
--build-arg REACT_APP_API_URL="$CLIENT_API_BUILD_HOST" \
-t tabsur-client \
-f Dockerfile.client.multistage .
docker tag tabsur-client:latest $CLIENT_ECR_URI:latest
echo "π€ Pushing images to ECR..."
docker push $SERVER_ECR_URI:latest
docker push $CLIENT_ECR_URI:latest
echo "β
Images pushed to ECR successfully!"
# Create fast deployment docker-compose.yml
echo "π Creating fast deployment configuration..."
# Resolve DB host from Terraform outputs if available
DB_HOST_FROM_TF=$(cd terraform 2>/dev/null && terraform output -raw postgres_public_ip 2>/dev/null || true)
DB_HOST_TO_USE=${DB_HOST_FROM_TF:-${DB_HOST:-3.249.94.227}}
# Secrets must come from the environment; never fall back to guessable defaults
DB_PASSWORD_VALUE=${DB_PASSWORD:?DB_PASSWORD must be set}
JWT_SECRET_VALUE=${JWT_SECRET:?JWT_SECRET must be set}
GOOGLE_MAPS_API_KEY_VALUE=${GOOGLE_MAPS_API_KEY:-}
# CORS origins: allow instance IP and planned domains (comma-separated)
CORS_ORIGIN_VALUE=${CORS_ORIGIN:-http://$EC2_PUBLIC_IP,https://bemyguest.dedyn.io,https://www.bemyguest.dedyn.io}
cat > docker-compose.ecr.yml << EOF
version: '3.8'
services:
server:
image: $SERVER_ECR_URI:latest
container_name: tabsur-server
restart: unless-stopped
environment:
- NODE_ENV=production
- PORT=5000
- DB_HOST=${DB_HOST_TO_USE}
- DB_PORT=5432
- DB_NAME=coolanu
- DB_USER=coolanu_user
- DB_PASSWORD=${DB_PASSWORD_VALUE}
- DB_SSL=false
- JWT_SECRET=${JWT_SECRET_VALUE}
- GOOGLE_MAPS_API_KEY=${GOOGLE_MAPS_API_KEY_VALUE}
- CORS_ORIGIN=${CORS_ORIGIN_VALUE}
ports:
- "5000:5000"
networks:
- tabsur-network
client:
image: $CLIENT_ECR_URI:latest
container_name: tabsur-client
restart: unless-stopped
environment:
- REACT_APP_SERVER_HOST=http://localhost:5000
- REACT_APP_GOOGLE_MAPS_API_KEY=${GOOGLE_MAPS_API_KEY}
ports:
- "80:80"
depends_on:
- server
networks:
- tabsur-network
loadbalancer:
image: nginx:alpine
container_name: tabsur-lb
restart: unless-stopped
ports:
- "8080:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- server
- client
networks:
- tabsur-network
networks:
tabsur-network:
driver: bridge
EOF
# Create nginx config
cat > nginx.conf << 'EOF'
events {
worker_connections 1024;
}
http {
upstream backend {
server server:5000;
}
upstream frontend {
server client:80;
}
server {
listen 80;
server_name _;
location /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
location /api/ {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location / {
proxy_pass http://frontend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
EOF
# Copy configuration to EC2 (home dir first to avoid permission issues)
echo "π€ Copying configuration to EC2..."
scp -i ~/.ssh/coolanu-postgres docker-compose.ecr.yml ubuntu@$EC2_PUBLIC_IP:~/
scp -i ~/.ssh/coolanu-postgres nginx.conf ubuntu@$EC2_PUBLIC_IP:~/
# Deploy on EC2 (much faster now - just pull and run!)
echo "π Deploying on EC2..."
ssh -i ~/.ssh/coolanu-postgres ubuntu@$EC2_PUBLIC_IP << EOF
set -e
sudo mkdir -p /opt/tabsur
sudo mv ~/docker-compose.ecr.yml ~/nginx.conf /opt/tabsur/
sudo chown root:root /opt/tabsur/docker-compose.ecr.yml /opt/tabsur/nginx.conf
cd /opt/tabsur
# Login to ECR (root docker)
aws ecr get-login-password --region $AWS_REGION | sudo docker login --username AWS --password-stdin $SERVER_ECR_URI
# Pull images (fast - no building!)
echo "π₯ Pulling images from ECR..."
sudo docker pull $SERVER_ECR_URI:latest
sudo docker pull $CLIENT_ECR_URI:latest
# Start services
echo "π Starting services..."
(sudo docker compose -f docker-compose.ecr.yml up -d) || (sudo docker-compose -f docker-compose.ecr.yml up -d)
echo "β
Deployment complete!"
(sudo docker compose -f docker-compose.ecr.yml ps) || (sudo docker-compose -f docker-compose.ecr.yml ps)
EOF
echo ""
echo "π Fast ECR-based deployment completed!"
echo "β±οΈ Total time: ~5-10 minutes (vs 15-30 minutes without ECR)"
echo "π Your app is now available at:"
echo " Main App: http://$EC2_PUBLIC_IP"
echo " API: http://$EC2_PUBLIC_IP:5000"
echo " Load Balancer: http://$EC2_PUBLIC_IP:8080"
echo ""
echo "π‘ Benefits of this approach:"
echo " β
Images built locally (faster, more reliable)"
echo " β
ECR provides versioning and rollback"
echo " β
No need to copy source code to EC2"
echo " β
Consistent deployments across environments"