-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·376 lines (322 loc) · 12.7 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·376 lines (322 loc) · 12.7 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/bin/bash
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
NC='\033[0m' # No Color
# Function to print colored output
print_color() {
local color=$1
local message=$2
echo -e "${color}${message}${NC}"
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Print usage
usage() {
echo "Usage: $0 [-a <account>] [-r <region>] [-e <environment>] [-p <profile>] [-y]"
echo ""
echo "Parameters (auto-detected in AWS CloudShell):"
echo " -a, --account AWS Account ID (auto-detected from AWS credentials)"
echo " -r, --region AWS Region (auto-detected from AWS_REGION)"
echo ""
echo "Optional parameters:"
echo " -e, --environment Environment (default: dev)"
echo " -p, --profile AWS Profile (default: uses IAM role or environment credentials)"
echo " -y, --yes Auto-approve (skip confirmation prompts)"
echo " -q, --quiet Quiet mode (less verbose output)"
echo ""
echo "Examples:"
echo " $0 # In CloudShell (auto-detect)"
echo " $0 -y # Auto-approve deployment"
echo " $0 -a 123456789012 -r us-west-1"
echo " $0 -a 123456789012 -r us-west-1 -e staging -p my-profile"
exit 1
}
# Parse command line arguments
ENVIRONMENT="dev"
PROFILE=""
AUTO_APPROVE=0
QUIET_MODE=0
while [[ $# -gt 0 ]]; do
case $1 in
-a|--account)
ACCOUNT="$2"
shift 2
;;
-r|--region)
REGION="$2"
shift 2
;;
-e|--environment)
ENVIRONMENT="$2"
shift 2
;;
-p|--profile)
PROFILE="$2"
shift 2
;;
-y|--yes|--auto-approve)
AUTO_APPROVE=1
shift
;;
-q|--quiet)
QUIET_MODE=1
shift
;;
-h|--help)
usage
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done
# Auto-detect account and region if not provided (useful for CloudShell)
if [[ -z "$ACCOUNT" ]]; then
print_color $YELLOW "Account not specified, attempting to auto-detect..."
ACCOUNT=$(aws sts get-caller-identity --query Account --output text 2>/dev/null || echo "")
if [[ -n "$ACCOUNT" ]]; then
print_color $GREEN "✓ Detected AWS Account: $ACCOUNT"
fi
fi
if [[ -z "$REGION" ]]; then
print_color $YELLOW "Region not specified, attempting to auto-detect..."
REGION="${AWS_REGION:-${AWS_DEFAULT_REGION:-}}"
if [[ -n "$REGION" ]]; then
print_color $GREEN "✓ Detected AWS Region: $REGION"
fi
fi
# Validate required parameters
if [[ -z "$ACCOUNT" || -z "$REGION" ]]; then
print_color $RED "Error: Account and Region are required parameters"
print_color $YELLOW "Tip: In AWS CloudShell, these are auto-detected. Otherwise, provide them explicitly."
usage
fi
print_color $GREEN "=== AS2 Application Deployment Script ==="
print_color $YELLOW "Account: $ACCOUNT"
print_color $YELLOW "Region: $REGION"
print_color $YELLOW "Environment: $ENVIRONMENT"
print_color $YELLOW "Profile: ${PROFILE:-<using IAM role>}"
echo ""
# Confirmation prompt (unless auto-approve)
if [[ "$AUTO_APPROVE" != "1" ]]; then
echo -e -n "${CYAN}Proceed with deployment? [Y/n]:${NC} "
read -r response
case "$response" in
[nN][oO]|[nN])
print_color $YELLOW "Deployment cancelled"
exit 0
;;
esac
fi
# Track deployment progress
TOTAL_STEPS=4
CURRENT_STEP=0
step_start() {
CURRENT_STEP=$((CURRENT_STEP + 1))
print_color $BLUE "\n[$CURRENT_STEP/$TOTAL_STEPS] $1"
}
step_done() {
print_color $GREEN " ✓ $1"
}
step_fail() {
print_color $RED " ✗ $1"
}
# Check prerequisites
step_start "Checking prerequisites..."
if ! command_exists aws; then
print_color $RED "AWS CLI is not installed or not in PATH"
exit 1
fi
if ! command_exists cdk; then
print_color $RED "AWS CDK is not installed or not in PATH"
exit 1
fi
if ! command_exists mvn; then
print_color $YELLOW "Maven is not installed. Attempting to install..."
# Detect OS and install Maven
if [[ -f /etc/os-release ]]; then
. /etc/os-release
case "$ID" in
amzn|rhel|centos|fedora)
# Amazon Linux, RHEL, CentOS, Fedora
if command_exists dnf; then
sudo dnf install -y maven
elif command_exists yum; then
sudo yum install -y maven
else
print_color $RED "Package manager not found for $ID"
exit 1
fi
;;
ubuntu|debian)
sudo apt-get update
sudo apt-get install -y maven
;;
*)
print_color $RED "Unsupported OS: $ID. Please install Maven manually."
exit 1
;;
esac
elif [[ "$(uname)" == "Darwin" ]]; then
# macOS
if command_exists brew; then
brew install maven
else
print_color $RED "Homebrew not found. Please install Maven manually or install Homebrew first."
exit 1
fi
else
print_color $RED "Unable to detect OS. Please install Maven manually."
exit 1
fi
# Verify installation
if command_exists mvn; then
print_color $GREEN "✓ Maven installed successfully"
else
print_color $RED "Maven installation failed. Please install manually."
exit 1
fi
fi
if ! command_exists npm; then
print_color $RED "Node.js/npm is not installed or not in PATH"
exit 1
fi
step_done "All prerequisites installed"
# Set AWS profile only if specified
if [[ -n "$PROFILE" ]]; then
export AWS_PROFILE=$PROFILE
PROFILE_ARG="--profile $PROFILE"
else
PROFILE_ARG=""
fi
# Verify AWS credentials
print_color $BLUE "Verifying AWS credentials..."
if ! aws sts get-caller-identity $PROFILE_ARG --output text >/dev/null 2>&1; then
print_color $RED "Failed to verify AWS credentials. Please configure AWS CLI or ensure IAM role is attached."
exit 1
fi
CALLER_IDENTITY=$(aws sts get-caller-identity $PROFILE_ARG --output json)
CURRENT_ACCOUNT=$(echo $CALLER_IDENTITY | jq -r '.Account')
print_color $GREEN "✓ AWS credentials verified for account: $CURRENT_ACCOUNT"
# Navigate to project root
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_ROOT"
print_color $YELLOW "Project root: $PROJECT_ROOT"
# Step 2: Build backend
step_start "Building backend..."
cd backend
if mvn clean package -DskipTests=true -q; then
step_done "Backend JAR built"
else
step_fail "Backend build failed"
exit 1
fi
cd "$PROJECT_ROOT"
# Step 3: Deploy backend infrastructure
step_start "Deploying backend infrastructure..."
cd cdk
if mvn compile -q; then
step_done "CDK dependencies installed"
else
step_fail "CDK dependencies installation failed"
exit 1
fi
if cdk bootstrap aws://$ACCOUNT/$REGION $PROFILE_ARG 2>/dev/null; then
step_done "CDK bootstrapped"
else
print_color $YELLOW " ⚠ CDK bootstrap may have failed, continuing..."
fi
# Stack names with env + region suffix for multi-instance support
STACK_SUFFIX="-$ENVIRONMENT-$REGION"
S3_EVENT_STACK="S3EventLambdaStack$STACK_SUFFIX"
SQS_STACK="SQSLambdaStack$STACK_SUFFIX"
AS2_RECEIVER_STACK="AS2ReceiverLambdaStack$STACK_SUFFIX"
MANAGE_STACK="ManageLambdaStack$STACK_SUFFIX"
FRONTEND_STACK="FrontendDeploymentStack$STACK_SUFFIX"
# Resource names are defined in ResourceNames.java
print_color $BLUE "Resource names (defined in ResourceNames.java):"
print_color $WHITE " S3 Buckets: as2-{send|receive|frontend}-bucket-$ENVIRONMENT-$REGION-$ACCOUNT"
print_color $WHITE " SQS Queue: as2-sqs-queue-$ENVIRONMENT-$REGION"
print_color $WHITE " DynamoDB Tables: as2-table-{name}-$ENVIRONMENT-$REGION"
print_color $WHITE " Lambda Functions: as2-lambda-{name}-$ENVIRONMENT-$REGION"
echo ""
# Deploy S3EventLambdaStack and SQSLambdaStack first
if cdk deploy "$S3_EVENT_STACK" "$SQS_STACK" --require-approval never $PROFILE_ARG --context account=$ACCOUNT --context region=$REGION --context env=$ENVIRONMENT; then
step_done "S3 and SQS stacks deployed"
else
step_fail "Backend infrastructure deployment failed"
exit 1
fi
# Deploy AS2ReceiverLambdaStack
if cdk deploy "$AS2_RECEIVER_STACK" --require-approval never $PROFILE_ARG --context account=$ACCOUNT --context region=$REGION --context env=$ENVIRONMENT; then
step_done "AS2 Receiver stack deployed"
else
step_fail "AS2ReceiverLambdaStack deployment failed"
exit 1
fi
# Deploy ManageLambdaStack
if cdk deploy "$MANAGE_STACK" --require-approval never $PROFILE_ARG --context account=$ACCOUNT --context region=$REGION --context env=$ENVIRONMENT; then
step_done "Management API stack deployed"
else
step_fail "ManageLambdaStack deployment failed"
exit 1
fi
cd "$PROJECT_ROOT"
# Step 4: Deploy frontend
step_start "Deploying frontend..."
cd cdk
# Deploy FrontendDeploymentStack - it will automatically fetch backend outputs and configure frontend
if cdk deploy "$FRONTEND_STACK" --require-approval never $PROFILE_ARG --context account=$ACCOUNT --context region=$REGION --context env=$ENVIRONMENT; then
step_done "Frontend and CloudFront deployed"
else
step_fail "Frontend deployment failed"
exit 1
fi
cd "$PROJECT_ROOT"
# Display deployment information
echo ""
print_color $GREEN "════════════════════════════════════════════════════════════"
print_color $GREEN " DEPLOYMENT COMPLETED SUCCESSFULLY"
print_color $GREEN "════════════════════════════════════════════════════════════"
echo ""
if command_exists jq; then
# Get stack outputs
MANAGE_OUTPUTS=$(aws cloudformation describe-stacks --stack-name "$MANAGE_STACK" --region $REGION $PROFILE_ARG --query "Stacks[0].Outputs" --output json 2>/dev/null || echo "[]")
FRONTEND_OUTPUTS=$(aws cloudformation describe-stacks --stack-name "$FRONTEND_STACK" --region $REGION $PROFILE_ARG --query "Stacks[0].Outputs" --output json 2>/dev/null || echo "[]")
print_color $CYAN "\n📋 Deployment Information:"
print_color $CYAN "═══════════════════════════"
# Parse backend outputs
API_URL=$(echo $MANAGE_OUTPUTS | jq -r '.[] | select(.OutputKey=="ManageAPIOutput") | .OutputValue' 2>/dev/null || echo "")
USER_POOL_ID=$(echo $MANAGE_OUTPUTS | jq -r '.[] | select(.OutputKey=="CognitoUserPoolIdOutput") | .OutputValue' 2>/dev/null || echo "")
CLIENT_ID=$(echo $MANAGE_OUTPUTS | jq -r '.[] | select(.OutputKey=="CognitoUserPoolClientIdOutput") | .OutputValue' 2>/dev/null || echo "")
# Parse frontend outputs
WEBSITE_URL=$(echo $FRONTEND_OUTPUTS | jq -r '.[] | select(.OutputKey=="WebsiteURL") | .OutputValue' 2>/dev/null || echo "")
CLOUDFRONT_URL=$(echo $FRONTEND_OUTPUTS | jq -r '.[] | select(.OutputKey=="CloudFrontURL") | .OutputValue' 2>/dev/null || echo "")
# Parse AS2 Receiver outputs
AS2_RECEIVER_OUTPUTS=$(aws cloudformation describe-stacks --stack-name "$AS2_RECEIVER_STACK" --region $REGION $PROFILE_ARG --query "Stacks[0].Outputs" --output json 2>/dev/null || echo "[]")
AS2_RECEIVER_URL=$(echo $AS2_RECEIVER_OUTPUTS | jq -r '.[] | select(.OutputKey=="AS2ReceiverApiUrl") | .OutputValue' 2>/dev/null || echo "")
[[ -n "$API_URL" ]] && print_color $WHITE "🔗 Backend API URL: $API_URL"
[[ -n "$USER_POOL_ID" ]] && print_color $WHITE "🔐 Cognito User Pool ID: $USER_POOL_ID"
[[ -n "$CLIENT_ID" ]] && print_color $WHITE "🔑 Cognito Client ID: $CLIENT_ID"
[[ -n "$WEBSITE_URL" ]] && print_color $WHITE "🌐 Frontend Website URL: $WEBSITE_URL"
[[ -n "$CLOUDFRONT_URL" ]] && print_color $WHITE "⚡ CloudFront Distribution URL: $CLOUDFRONT_URL"
[[ -n "$AS2_RECEIVER_URL" ]] && print_color $WHITE "📨 AS2 Receiver API URL: $AS2_RECEIVER_URL"
print_color $GREEN "\n✅ Deployment completed successfully!"
print_color $GREEN "Your AS2 application is now ready to use."
else
print_color $YELLOW "⚠ jq not found, cannot parse deployment outputs"
print_color $GREEN "✅ Deployment completed!"
fi
print_color $CYAN "\n📝 Next Steps:"
print_color $WHITE "1. Access your application at: $CLOUDFRONT_URL"
print_color $WHITE "2. Create a user account by clicking 'Sign Up' on the login page"
print_color $WHITE "3. Configure your AS2 partners and certificates"