-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.ps1
More file actions
330 lines (275 loc) · 11.4 KB
/
Copy pathdeploy.ps1
File metadata and controls
330 lines (275 loc) · 11.4 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
#!/usr/bin/env pwsh
param(
[Parameter(Mandatory=$true)]
[string]$Account,
[Parameter(Mandatory=$true)]
[string]$Region,
[string]$Environment = "dev",
[string]$Profile = "default",
[string]$ResourcePrefix = "as2",
[Alias("y")]
[switch]$AutoApprove,
[Alias("q")]
[switch]$Quiet
)
# Set error action preference
$ErrorActionPreference = "Stop"
# Function to check if command exists
function Test-Command {
param($Command)
$null = Get-Command $Command -ErrorAction SilentlyContinue
return $?
}
# Function to write output (respects quiet mode)
function Write-Step {
param($Message, $Color = "White")
if (-not $Quiet) {
Write-Host $Message -ForegroundColor $Color
}
}
Write-Host "=== AS2 Application Deployment Script ===" -ForegroundColor Green
Write-Host "Account: $Account" -ForegroundColor Yellow
Write-Host "Region: $Region" -ForegroundColor Yellow
Write-Host "Environment: $Environment" -ForegroundColor Yellow
Write-Host "Profile: $Profile" -ForegroundColor Yellow
Write-Host "Resource Prefix: $ResourcePrefix" -ForegroundColor Yellow
Write-Host ""
# Confirmation prompt (unless auto-approve)
if (-not $AutoApprove) {
$response = Read-Host "Proceed with deployment? [Y/n]"
if ($response -eq "n" -or $response -eq "N" -or $response -eq "no" -or $response -eq "No") {
Write-Host "Deployment cancelled" -ForegroundColor Yellow
exit 0
}
}
# Track deployment progress
$TotalSteps = 4
$CurrentStep = 0
function Step-Start {
param($Message)
$script:CurrentStep++
Write-Host "`n[$script:CurrentStep/$TotalSteps] $Message" -ForegroundColor Blue
}
function Step-Done {
param($Message)
Write-Host " ✓ $Message" -ForegroundColor Green
}
function Step-Fail {
param($Message)
Write-Host " ✗ $Message" -ForegroundColor Red
}
# Check prerequisites
Step-Start "Checking prerequisites..."
if (-not (Test-Command "aws")) {
Write-Error "AWS CLI is not installed or not in PATH"
exit 1
}
if (-not (Test-Command "cdk")) {
Write-Error "AWS CDK is not installed or not in PATH"
exit 1
}
if (-not (Test-Command "mvn")) {
Write-Error "Maven is not installed or not in PATH"
exit 1
}
if (-not (Test-Command "npm")) {
Write-Error "Node.js/npm is not installed or not in PATH"
exit 1
}
Step-Done "All prerequisites installed"
# Set AWS profile
$env:AWS_PROFILE = $Profile
# Verify AWS credentials
Write-Step "Verifying AWS credentials..." "Blue"
try {
$identity = aws sts get-caller-identity --output json | ConvertFrom-Json
Step-Done "AWS credentials verified for account: $($identity.Account)"
} catch {
Write-Error "Failed to verify AWS credentials. Please configure AWS CLI with 'aws configure'"
exit 1
}
# Navigate to project root
$projectRoot = $PSScriptRoot
Set-Location $projectRoot
Write-Step "Project root: $projectRoot" "Yellow"
# Generate date suffix for unique bucket names
$DateSuffix = Get-Date -Format "yyyyMMdd"
# Generate base resource names
$FrontendBucket = "$ResourcePrefix-frontend-bucket-$DateSuffix"
$SendBucket = "$ResourcePrefix-send-bucket-$DateSuffix"
$ReceiveBucket = "$ResourcePrefix-receive-bucket-$DateSuffix"
$SqsQueue = "$ResourcePrefix-sqs-queue-$DateSuffix"
# Display actual bucket names (with suffix for non-prod)
if ($Environment -eq "prod") {
$DisplayFrontendBucket = $FrontendBucket
$DisplaySendBucket = $SendBucket
$DisplayReceiveBucket = $ReceiveBucket
$DisplaySqsQueue = $SqsQueue
} else {
$DisplayFrontendBucket = "$FrontendBucket-$Environment-$Region"
$DisplaySendBucket = "$SendBucket-$Environment-$Region"
$DisplayReceiveBucket = "$ReceiveBucket-$Environment-$Region"
$DisplaySqsQueue = "$SqsQueue-$Environment-$Region"
}
# Stack names with env + region suffix for multi-instance support
$StackSuffix = "-$Environment-$Region"
$S3EventStack = "S3EventLambdaStack$StackSuffix"
$SqsStack = "SQSLambdaStack$StackSuffix"
$AS2ReceiverStack = "AS2ReceiverLambdaStack$StackSuffix"
$ManageStack = "ManageLambdaStack$StackSuffix"
$FrontendStack = "FrontendDeploymentStack$StackSuffix"
Write-Host "Using resource names:" -ForegroundColor Blue
Write-Host " Frontend Bucket: $DisplayFrontendBucket" -ForegroundColor White
Write-Host " Send Bucket: $DisplaySendBucket" -ForegroundColor White
Write-Host " Receive Bucket: $DisplayReceiveBucket" -ForegroundColor White
Write-Host " SQS Queue: $DisplaySqsQueue" -ForegroundColor White
Write-Host ""
# Step 2: Build backend
Step-Start "Building backend..."
Set-Location "backend"
try {
if ($Quiet) {
mvn clean package -DskipTests=true -q
} else {
mvn clean package -DskipTests=true
}
if ($LASTEXITCODE -ne 0) {
throw "Maven build failed"
}
Step-Done "Backend JAR built"
} catch {
Step-Fail "Backend build failed"
exit 1
}
Set-Location $projectRoot
# Step 3: Deploy backend infrastructure
Step-Start "Deploying backend infrastructure..."
Set-Location "cdk"
try {
if ($Quiet) {
mvn compile -q
} else {
mvn compile
}
if ($LASTEXITCODE -ne 0) {
throw "CDK dependencies installation failed"
}
Step-Done "CDK dependencies installed"
} catch {
Step-Fail "CDK dependencies installation failed"
exit 1
}
Write-Step "Bootstrapping CDK (if needed)..." "Blue"
try {
cdk bootstrap aws://$Account/$Region --profile $Profile 2>$null
Step-Done "CDK bootstrapped"
} catch {
Write-Host " ⚠ CDK bootstrap may have failed, continuing..." -ForegroundColor Yellow
}
# Deploy S3EventLambdaStack and SQSLambdaStack first
try {
cdk deploy $S3EventStack $SqsStack --require-approval never --profile $Profile --context account=$Account --context region=$Region --context env=$Environment --context frontendBucketName="$FrontendBucket" --context sendBucketName="$SendBucket" --context receiveBucketName="$ReceiveBucket" --context sqsQueueName="$SqsQueue" if ($LASTEXITCODE -ne 0) {
throw "S3 and SQS stacks deployment failed"
}
Step-Done "S3 and SQS stacks deployed"
} catch {
Step-Fail "Backend infrastructure deployment failed"
exit 1
}
# Deploy AS2ReceiverLambdaStack
try {
cdk deploy $AS2ReceiverStack --require-approval never --profile $Profile --context account=$Account --context region=$Region --context env=$Environment --context frontendBucketName="$FrontendBucket" --context sendBucketName="$SendBucket" --context receiveBucketName="$ReceiveBucket" --context sqsQueueName="$SqsQueue" if ($LASTEXITCODE -ne 0) {
throw "AS2ReceiverLambdaStack deployment failed"
}
Step-Done "AS2 Receiver stack deployed"
} catch {
Step-Fail "AS2ReceiverLambdaStack deployment failed"
exit 1
}
# Deploy ManageLambdaStack
try {
cdk deploy $ManageStack --require-approval never --profile $Profile --context account=$Account --context region=$Region --context env=$Environment --context frontendBucketName="$FrontendBucket" --context sendBucketName="$SendBucket" --context receiveBucketName="$ReceiveBucket" --context sqsQueueName="$SqsQueue" if ($LASTEXITCODE -ne 0) {
throw "ManageLambdaStack deployment failed"
}
Step-Done "Management API stack deployed"
} catch {
Step-Fail "ManageLambdaStack deployment failed"
exit 1
}
Set-Location $projectRoot
# Step 4: Deploy frontend
Step-Start "Deploying frontend..."
Set-Location "cdk"
try {
cdk deploy $FrontendStack --require-approval never --profile $Profile --context account=$Account --context region=$Region --context env=$Environment --context frontendBucketName="$FrontendBucket" --context sendBucketName="$SendBucket" --context receiveBucketName="$ReceiveBucket" --context sqsQueueName="$SqsQueue" if ($LASTEXITCODE -ne 0) {
throw "Frontend deployment failed"
}
Step-Done "Frontend and CloudFront deployed"
} catch {
Step-Fail "Frontend deployment failed"
exit 1
}
Set-Location $projectRoot
# Display deployment information
Write-Host ""
Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Green
Write-Host " DEPLOYMENT COMPLETED SUCCESSFULLY" -ForegroundColor Green
Write-Host "════════════════════════════════════════════════════════════" -ForegroundColor Green
Write-Host ""
try {
$manageLambdaOutputs = aws cloudformation describe-stacks --stack-name $ManageStack --region $Region --profile $Profile --query "Stacks[0].Outputs" --output json | ConvertFrom-Json
$frontendOutputs = aws cloudformation describe-stacks --stack-name $FrontendStack --region $Region --profile $Profile --query "Stacks[0].Outputs" --output json | ConvertFrom-Json
Write-Host "`n📋 Deployment Information:" -ForegroundColor Cyan
Write-Host "═══════════════════════════" -ForegroundColor Cyan
$ApiUrl = ""
$UserPoolId = ""
$ClientId = ""
$WebsiteUrl = ""
$CloudFrontUrl = ""
$AS2ReceiverUrl = ""
foreach ($output in $manageLambdaOutputs) {
if ($output.OutputKey -eq "ManageAPIOutput") {
$ApiUrl = $output.OutputValue
Write-Host "🔗 Backend API URL: $ApiUrl" -ForegroundColor White
}
if ($output.OutputKey -eq "CognitoUserPoolIdOutput") {
$UserPoolId = $output.OutputValue
Write-Host "🔐 Cognito User Pool ID: $UserPoolId" -ForegroundColor White
}
if ($output.OutputKey -eq "CognitoUserPoolClientIdOutput") {
$ClientId = $output.OutputValue
Write-Host "🔑 Cognito Client ID: $ClientId" -ForegroundColor White
}
}
foreach ($output in $frontendOutputs) {
if ($output.OutputKey -eq "WebsiteURL") {
$WebsiteUrl = $output.OutputValue
Write-Host "🌐 Frontend Website URL: $WebsiteUrl" -ForegroundColor White
}
if ($output.OutputKey -eq "CloudFrontURL") {
$CloudFrontUrl = $output.OutputValue
Write-Host "⚡ CloudFront Distribution URL: $CloudFrontUrl" -ForegroundColor White
}
}
# Get AS2 Receiver URL
try {
$as2ReceiverOutputs = aws cloudformation describe-stacks --stack-name $AS2ReceiverStack --region $Region --profile $Profile --query "Stacks[0].Outputs" --output json | ConvertFrom-Json
foreach ($output in $as2ReceiverOutputs) {
if ($output.OutputKey -eq "AS2ReceiverApiUrl") {
$AS2ReceiverUrl = $output.OutputValue
Write-Host "📨 AS2 Receiver API URL: $AS2ReceiverUrl" -ForegroundColor White
}
}
} catch {
# AS2 Receiver stack output retrieval failed, continue
}
Write-Host "`n✅ Deployment completed successfully!" -ForegroundColor Green
Write-Host "Your AS2 application is now ready to use." -ForegroundColor Green
Write-Host "`n📝 Next Steps:" -ForegroundColor Cyan
Write-Host "1. Access your application at: $CloudFrontUrl" -ForegroundColor White
Write-Host "2. Create a user account by clicking 'Sign Up' on the login page" -ForegroundColor White
Write-Host "3. Configure your AS2 partners and certificates" -ForegroundColor White
} catch {
Write-Warning "Could not retrieve deployment outputs, but deployment appears successful"
Write-Host "✅ Deployment completed!" -ForegroundColor Green
}