-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdeploy_chaplin_web.sh
More file actions
executable file
·357 lines (300 loc) · 10.8 KB
/
Copy pathdeploy_chaplin_web.sh
File metadata and controls
executable file
·357 lines (300 loc) · 10.8 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
set -e
echo "=========================================="
echo "Chaplin Deployment Script"
echo "=========================================="
echo ""
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
print_success() { echo -e "${GREEN}✓ $1${NC}"; }
print_error() { echo -e "${RED}✗ $1${NC}"; }
print_info() { echo -e "${YELLOW}ℹ $1${NC}"; }
# Check prerequisites
echo "Checking prerequisites..."
if ! command -v python3 &> /dev/null; then
print_error "Python 3 is not installed. Please install Python 3.8 or higher."
exit 1
fi
print_success "Python $(python3 --version | cut -d' ' -f2) found"
if ! command -v node &> /dev/null; then
print_error "Node.js is not installed. Please install Node.js 16.x or higher."
exit 1
fi
print_success "Node.js $(node --version) found"
if ! command -v npm &> /dev/null; then
print_error "npm is not installed."
exit 1
fi
print_success "npm $(npm --version) found"
if ! command -v aws &> /dev/null; then
print_error "AWS CLI is not installed."
exit 1
fi
print_success "AWS CLI found"
echo ""
echo "=========================================="
echo "Configuration"
echo "=========================================="
echo ""
read -p "Enter AWS Region (default: us-east-1): " AWS_REGION
AWS_REGION=${AWS_REGION:-us-east-1}
print_info "Using region: $AWS_REGION"
echo ""
read -p "Enter S3 bucket name for health event data (must already exist): " S3_BUCKET_NAME
if [ -z "$S3_BUCKET_NAME" ]; then
print_error "S3 bucket name is required"
exit 1
fi
# Verify bucket exists
if ! aws s3 ls "s3://$S3_BUCKET_NAME" --region $AWS_REGION &> /dev/null; then
print_error "S3 bucket '$S3_BUCKET_NAME' does not exist or is not accessible"
exit 1
fi
print_success "S3 bucket verified: $S3_BUCKET_NAME"
echo ""
read -p "Enter Cognito user email: " COGNITO_USER_EMAIL
if [ -z "$COGNITO_USER_EMAIL" ]; then
print_error "Email is required"
exit 1
fi
# Extract domain and ask about restriction
EMAIL_DOMAIN="${COGNITO_USER_EMAIL#*@}"
read -p "Restrict signups to @${EMAIL_DOMAIN} only? [Y/n]: " RESTRICT_DOMAIN
RESTRICT_DOMAIN=${RESTRICT_DOMAIN:-Y}
if [[ "$RESTRICT_DOMAIN" =~ ^[Yy] ]]; then
ALLOWED_EMAIL_DOMAIN="$EMAIL_DOMAIN"
print_success "Signups restricted to @${EMAIL_DOMAIN}"
else
ALLOWED_EMAIL_DOMAIN=""
print_info "Signups open to any email domain"
fi
# Infrastructure setup
echo ""
print_info "AWS Infrastructure Setup"
echo ""
echo "Do you want to:"
echo "1) Deploy new infrastructure (Cognito + DynamoDB) - recommended"
echo "2) Use existing infrastructure"
echo ""
read -p "Enter choice [1]: " INFRA_CHOICE
INFRA_CHOICE=${INFRA_CHOICE:-1}
if [ "$INFRA_CHOICE" == "1" ]; then
echo ""
print_info "Deploying Cognito User Pool and DynamoDB Table..."
STACK_NAME="chaplin-infrastructure-stack"
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
aws cloudformation deploy \
--template-file "$SCRIPT_DIR/chaplin-infrastructure.yaml" \
--stack-name $STACK_NAME \
--region $AWS_REGION \
--parameter-overrides HealthDataBucketName=$S3_BUCKET_NAME \
--capabilities CAPABILITY_NAMED_IAM \
--no-fail-on-empty-changeset
if [ $? -eq 0 ]; then
print_success "Infrastructure deployed"
COGNITO_USER_POOL_ID=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--region $AWS_REGION \
--query 'Stacks[0].Outputs[?OutputKey==`UserPoolId`].OutputValue' \
--output text)
COGNITO_CLIENT_ID=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--region $AWS_REGION \
--query 'Stacks[0].Outputs[?OutputKey==`UserPoolClientId`].OutputValue' \
--output text)
COGNITO_CLIENT_SECRET=$(aws cognito-idp describe-user-pool-client \
--user-pool-id $COGNITO_USER_POOL_ID \
--client-id $COGNITO_CLIENT_ID \
--region $AWS_REGION \
--query 'UserPoolClient.ClientSecret' \
--output text)
DYNAMODB_TABLE=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--region $AWS_REGION \
--query 'Stacks[0].Outputs[?OutputKey==`DynamoDBTableName`].OutputValue' \
--output text)
LAMBDA_ARN=$(aws cloudformation describe-stacks \
--stack-name $STACK_NAME \
--region $AWS_REGION \
--query 'Stacks[0].Outputs[?OutputKey==`S3ToDynamoDBLambdaArn`].OutputValue' \
--output text)
print_success "Configuration retrieved"
print_info "DynamoDB Table: $DYNAMODB_TABLE"
print_info "Lambda Function: $LAMBDA_ARN"
# Configure S3 event notification
print_info "Configuring S3 event notification..."
NOTIFICATION_CONFIG=$(cat <<EOF
{
"LambdaFunctionConfigurations": [
{
"LambdaFunctionArn": "$LAMBDA_ARN",
"Events": ["s3:ObjectCreated:*"],
"Filter": {
"Key": {
"FilterRules": [
{
"Name": "suffix",
"Value": ".json"
}
]
}
}
}
]
}
EOF
)
echo "$NOTIFICATION_CONFIG" > /tmp/s3-notification-config.json
if aws s3api put-bucket-notification-configuration \
--bucket $S3_BUCKET_NAME \
--notification-configuration file:///tmp/s3-notification-config.json \
--region $AWS_REGION; then
print_success "S3 event notification configured"
rm /tmp/s3-notification-config.json
else
print_error "Failed to configure S3 event notification"
print_info "You may need to configure it manually"
fi
else
print_error "Failed to deploy infrastructure"
exit 1
fi
else
read -p "Enter Cognito User Pool ID: " COGNITO_USER_POOL_ID
[ -z "$COGNITO_USER_POOL_ID" ] && print_error "User Pool ID required" && exit 1
read -p "Enter Cognito Client ID: " COGNITO_CLIENT_ID
[ -z "$COGNITO_CLIENT_ID" ] && print_error "Client ID required" && exit 1
read -p "Enter Cognito Client Secret: " COGNITO_CLIENT_SECRET
[ -z "$COGNITO_CLIENT_SECRET" ] && print_error "Client Secret required" && exit 1
fi
echo ""
echo "=========================================="
echo "Setting up Python environment"
echo "=========================================="
echo ""
if [ ! -d ".venv" ]; then
print_info "Creating Python virtual environment..."
python3 -m venv .venv
print_success "Virtual environment created"
else
print_info "Virtual environment already exists"
fi
source .venv/bin/activate
print_info "Installing Python dependencies..."
pip install --quiet --upgrade pip
pip install --quiet boto3 pandas numpy python-dotenv
if pip install --quiet strands-agents 2>/dev/null; then
print_success "Amazon Strands installed"
else
print_info "Amazon Strands not available (optional)"
fi
print_success "Python dependencies installed"
echo ""
echo "=========================================="
echo "Setting up Node.js dependencies"
echo "=========================================="
echo ""
print_info "Installing server dependencies..."
cd health-dashboard
npm install --silent
print_success "Server dependencies installed"
print_info "Installing client dependencies..."
cd client
npm install --silent
print_success "Client dependencies installed"
print_info "Building React application..."
npm run build --silent
print_success "React application built"
cd ../..
echo ""
echo "=========================================="
echo "Configuring environment"
echo "=========================================="
echo ""
ENV_FILE="health-dashboard/.env"
print_info "Creating environment configuration..."
cat > "$ENV_FILE" << EOF
# AWS Configuration
AWS_REGION=$AWS_REGION
# Bedrock Configuration
BEDROCK_MODEL_ID=global.anthropic.claude-sonnet-4-20250514-v1:0
# AWS Cognito Configuration
COGNITO_USER_POOL_ID=$COGNITO_USER_POOL_ID
COGNITO_CLIENT_ID=$COGNITO_CLIENT_ID
COGNITO_CLIENT_SECRET=$COGNITO_CLIENT_SECRET
# Application Configuration
PORT=3001
NODE_ENV=development
ALLOWED_ORIGINS=http://localhost:3000,http://localhost:3001
# Email Domain Restriction
ALLOWED_EMAIL_DOMAIN=$ALLOWED_EMAIL_DOMAIN
EOF
print_success "Environment configuration created"
# Create output directory
print_info "Creating output directory..."
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
mkdir -p "$SCRIPT_DIR/output"
print_success "Output directory created"
echo ""
echo "=========================================="
echo "Verifying AWS access"
echo "=========================================="
echo ""
if aws sts get-caller-identity --region $AWS_REGION &> /dev/null; then
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
print_success "AWS credentials verified (Account: $ACCOUNT_ID)"
else
print_error "Failed to verify AWS credentials"
exit 1
fi
print_info "Checking Bedrock model access..."
if aws bedrock list-foundation-models --region $AWS_REGION --query 'modelSummaries[?contains(modelId, `claude-3-5-sonnet`)]' &> /dev/null; then
print_success "Bedrock access verified"
else
print_error "Cannot access Bedrock. Ensure Claude 3.5 Sonnet access in $AWS_REGION"
exit 1
fi
echo ""
echo "=========================================="
echo "Deployment Complete!"
echo "=========================================="
echo ""
print_success "Chaplin has been successfully deployed"
echo ""
if [ "$INFRA_CHOICE" == "1" ]; then
echo "Infrastructure Details:"
echo " User Pool ID: $COGNITO_USER_POOL_ID"
echo " Client ID: $COGNITO_CLIENT_ID"
echo " DynamoDB Table: $DYNAMODB_TABLE"
echo ""
# Auto-create Cognito user
print_info "Creating Cognito user: $COGNITO_USER_EMAIL"
if aws cognito-idp admin-create-user \
--user-pool-id "$COGNITO_USER_POOL_ID" \
--username "$COGNITO_USER_EMAIL" \
--temporary-password "ChaplinTemp1!" \
--user-attributes Name=email,Value="$COGNITO_USER_EMAIL" Name=email_verified,Value=true \
--message-action SUPPRESS \
--region "$AWS_REGION" &>/dev/null; then
print_success "Cognito user created"
echo " Email: $COGNITO_USER_EMAIL"
echo " Password: ChaplinTemp1!"
echo " (You will be forced to change this on first login)"
else
print_error "Failed to create Cognito user"
print_info "Create manually: aws cognito-idp admin-create-user --user-pool-id $COGNITO_USER_POOL_ID --username $COGNITO_USER_EMAIL --message-action SUPPRESS --region $AWS_REGION"
fi
echo ""
fi
echo "Next steps:"
echo "1. Deploy the data pipeline: see data_pipeline/README.md"
echo ""
print_info "Starting Chaplin..."
echo ""
echo "Access the application at: http://localhost:3001"
echo "Press Ctrl+C to stop the server"
echo ""
cd health-dashboard && npm start