# Update Launch Template to pull from Git # Define the user data bash script $userData = @' #!/bin/bash cd /home/ec2-user # Clone repo if it doesn't exist, otherwise pull latest if [ ! -d "AWS_Portfolio_Manager" ]; then git clone https://huggingface.co/spaces/Multichem-PD/DFS_Portfolio_Manager AWS_Portfolio_Manager cd AWS_Portfolio_Manager python3 -m venv venv mkdir -p .streamlit else cd AWS_Portfolio_Manager git pull origin main fi # Install/update packages source venv/bin/activate pip install -r requirements.txt # Restart Streamlit sudo supervisorctl restart streamlit '@ # Encode to base64 for AWS $userDataBase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($userData)) Write-Host "Creating new launch template version..." -ForegroundColor Cyan # Create properly formatted JSON file with ASCII encoding (no BOM) $jsonContent = "{`"UserData`": `"$userDataBase64`"}" [System.IO.File]::WriteAllText("$PWD\launch-template-data.json", $jsonContent, [System.Text.Encoding]::ASCII) # Create new launch template version aws ec2 create-launch-template-version ` --launch-template-name portfolio-manager-template ` --source-version '$Latest' ` --launch-template-data file://launch-template-data.json if ($LASTEXITCODE -ne 0) { Write-Host "Failed to create launch template version" -ForegroundColor Red Remove-Item "launch-template-data.json" -ErrorAction SilentlyContinue exit 1 } # Get the latest version number $latestVersion = aws ec2 describe-launch-template-versions ` --launch-template-name portfolio-manager-template ` --query 'LaunchTemplateVersions[0].VersionNumber' ` --output text Write-Host "Setting version $latestVersion as default..." -ForegroundColor Cyan # Set the new version as default aws ec2 modify-launch-template ` --launch-template-name portfolio-manager-template ` --default-version $latestVersion # Clean up temp file Remove-Item "launch-template-data.json" -ErrorAction SilentlyContinue if ($LASTEXITCODE -eq 0) { Write-Host "Success! Launch template updated to version $latestVersion" -ForegroundColor Green Write-Host "New instances will automatically pull code from Git" -ForegroundColor Green Write-Host "Don't forget to add secrets.toml to new instances manually!" -ForegroundColor Yellow } else { Write-Host "Failed to set default version" -ForegroundColor Red exit 1 }