File size: 2,309 Bytes
7f942a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
param(
    [Parameter(Mandatory=$true)]
    [string]$InstanceType
)

# Validate instance type format
if ($InstanceType -notmatch '^[a-z][0-9]+\.[a-z]+$') {
    Write-Error "Invalid instance type format. Use format like: m5.large, m5.xlarge, c5.2xlarge"
    exit 1
}

Write-Host "Scaling portfolio-manager instances to $InstanceType..." -ForegroundColor Green

# Step 1: Create new launch template version
Write-Host "Creating new launch template version..." -ForegroundColor Yellow
$templateResult = aws ec2 create-launch-template-version --launch-template-name portfolio-manager-template --source-version 1 --launch-template-data "{`"InstanceType`":`"$InstanceType`"}" --version-description "Scaled to $InstanceType" --output json | ConvertFrom-Json

if ($LASTEXITCODE -ne 0) {
    Write-Error "Failed to create launch template version"
    exit 1
}

$newVersion = $templateResult.LaunchTemplateVersion.VersionNumber
Write-Host "Created launch template version $newVersion" -ForegroundColor Green

# Step 2: Update Auto Scaling Group
Write-Host "Updating Auto Scaling Group..." -ForegroundColor Yellow
aws autoscaling update-auto-scaling-group --auto-scaling-group-name portfolio-manager-asg --launch-template LaunchTemplateName=portfolio-manager-template,Version='$Latest'

if ($LASTEXITCODE -ne 0) {
    Write-Error "Failed to update Auto Scaling Group"
    exit 1
}

Write-Host "Auto Scaling Group updated" -ForegroundColor Green

# Step 3: Start instance refresh
Write-Host "Starting instance refresh..." -ForegroundColor Yellow
$refreshResult = aws autoscaling start-instance-refresh --auto-scaling-group-name portfolio-manager-asg --preferences '{\"InstanceWarmup\":300,\"MinHealthyPercentage\":50}' --output json | ConvertFrom-Json

if ($LASTEXITCODE -ne 0) {
    Write-Error "Failed to start instance refresh"
    exit 1
}

$refreshId = $refreshResult.InstanceRefreshId
Write-Host "Instance refresh started with ID: $refreshId" -ForegroundColor Green

Write-Host "`nScaling process initiated successfully!" -ForegroundColor Green
Write-Host "Monitor progress with: aws autoscaling describe-instance-refreshes --auto-scaling-group-name portfolio-manager-asg --instance-refresh-ids $refreshId" -ForegroundColor Cyan
Write-Host "This process will take 5-10 minutes to complete." -ForegroundColor Yellow