|
| 1 | +# PowerShell script for ECR deployment (Windows) |
| 2 | +param( |
| 3 | + [string]$AWSRegion = "ap-southeast-2", |
| 4 | + [string]$ECRRepository = "permit-api", |
| 5 | + [string]$ImageTag = "latest" |
| 6 | +) |
| 7 | + |
| 8 | +Write-Host "🔐 AWS CLI ECR Deployment Script" -ForegroundColor Green |
| 9 | +Write-Host "Region: $AWSRegion" -ForegroundColor Yellow |
| 10 | + |
| 11 | +# Use AWS CLI from venv |
| 12 | +$AWS_CMD = ".\.venv\Scripts\python.exe -m awscli" |
| 13 | + |
| 14 | +# Test AWS credentials |
| 15 | +Write-Host "🔍 Testing AWS credentials..." -ForegroundColor Cyan |
| 16 | +try { |
| 17 | + $AccountOutput = & cmd /c "$AWS_CMD sts get-caller-identity 2>nul" |
| 18 | + if ($LASTEXITCODE -eq 0) { |
| 19 | + $AccountInfo = $AccountOutput | ConvertFrom-Json |
| 20 | + $AWSAccountID = $AccountInfo.Account |
| 21 | + Write-Host "✅ AWS Account: $AWSAccountID" -ForegroundColor Green |
| 22 | + } else { |
| 23 | + throw "AWS credentials test failed" |
| 24 | + } |
| 25 | +} |
| 26 | +catch { |
| 27 | + Write-Host "❌ AWS CLI not configured or credentials invalid" -ForegroundColor Red |
| 28 | + Write-Host "Please run: .\.venv\Scripts\python.exe -m awscli configure" -ForegroundColor Yellow |
| 29 | + Write-Host "Make sure to use fresh AWS Access Keys!" -ForegroundColor Yellow |
| 30 | + exit 1 |
| 31 | +} |
| 32 | + |
| 33 | +# Check if Docker is available |
| 34 | +Write-Host "🐳 Checking Docker..." -ForegroundColor Cyan |
| 35 | +try { |
| 36 | + docker --version | Out-Null |
| 37 | + if ($LASTEXITCODE -eq 0) { |
| 38 | + Write-Host "✅ Docker is available" -ForegroundColor Green |
| 39 | + } else { |
| 40 | + throw "Docker not found" |
| 41 | + } |
| 42 | +} |
| 43 | +catch { |
| 44 | + Write-Host "❌ Docker not installed" -ForegroundColor Red |
| 45 | + Write-Host "Please install Docker Desktop or use GitHub Actions deployment" -ForegroundColor Yellow |
| 46 | + Write-Host "GitHub Actions URL: https://github.com/hk-dev13/project-permit-api/actions" -ForegroundColor Cyan |
| 47 | + exit 1 |
| 48 | +} |
| 49 | + |
| 50 | +# Create ECR repository if needed |
| 51 | +Write-Host "📦 Setting up ECR repository..." -ForegroundColor Cyan |
| 52 | +$RepoCheck = & cmd /c "$AWS_CMD ecr describe-repositories --repository-names $ECRRepository --region $AWSRegion 2>nul" |
| 53 | +if ($LASTEXITCODE -ne 0) { |
| 54 | + Write-Host "Creating ECR repository: $ECRRepository" -ForegroundColor Yellow |
| 55 | + & cmd /c "$AWS_CMD ecr create-repository --repository-name $ECRRepository --region $AWSRegion" |
| 56 | +} |
| 57 | + |
| 58 | +# Login to ECR |
| 59 | +Write-Host "🔐 Logging into ECR..." -ForegroundColor Cyan |
| 60 | +$LoginPassword = & cmd /c "$AWS_CMD ecr get-login-password --region $AWSRegion" |
| 61 | +if ($LASTEXITCODE -eq 0) { |
| 62 | + echo $LoginPassword | docker login --username AWS --password-stdin "$AWSAccountID.dkr.ecr.$AWSRegion.amazonaws.com" |
| 63 | + if ($LASTEXITCODE -eq 0) { |
| 64 | + Write-Host "✅ ECR login successful" -ForegroundColor Green |
| 65 | + } else { |
| 66 | + Write-Host "❌ ECR login failed" -ForegroundColor Red |
| 67 | + exit 1 |
| 68 | + } |
| 69 | +} else { |
| 70 | + Write-Host "❌ Failed to get ECR login token" -ForegroundColor Red |
| 71 | + exit 1 |
| 72 | +} |
| 73 | + |
| 74 | +# Build Docker image |
| 75 | +Write-Host "🔨 Building Docker image..." -ForegroundColor Cyan |
| 76 | +docker build -f Dockerfile.apprunner -t "${ECRRepository}:${ImageTag}" . |
| 77 | +if ($LASTEXITCODE -ne 0) { |
| 78 | + Write-Host "❌ Docker build failed" -ForegroundColor Red |
| 79 | + exit 1 |
| 80 | +} |
| 81 | + |
| 82 | +# Tag and push image |
| 83 | +$ECRURI = "$AWSAccountID.dkr.ecr.$AWSRegion.amazonaws.com/${ECRRepository}:${ImageTag}" |
| 84 | +Write-Host "🏷️ Tagging image: $ECRURI" -ForegroundColor Cyan |
| 85 | +docker tag "${ECRRepository}:${ImageTag}" $ECRURI |
| 86 | + |
| 87 | +Write-Host "📤 Pushing to ECR..." -ForegroundColor Cyan |
| 88 | +docker push $ECRURI |
| 89 | + |
| 90 | +if ($LASTEXITCODE -eq 0) { |
| 91 | + Write-Host "" -ForegroundColor Green |
| 92 | + Write-Host "🎉 SUCCESS! Docker image pushed to ECR" -ForegroundColor Green |
| 93 | + Write-Host "" -ForegroundColor Green |
| 94 | + Write-Host "📋 Next Steps:" -ForegroundColor Yellow |
| 95 | + Write-Host "1. Go to AWS App Runner Console" -ForegroundColor White |
| 96 | + Write-Host "2. Create Service → Container Registry" -ForegroundColor White |
| 97 | + Write-Host "3. Use this ECR Image URI:" -ForegroundColor White |
| 98 | + Write-Host " $ECRURI" -ForegroundColor Cyan |
| 99 | + Write-Host "4. Set Port: 8000" -ForegroundColor White |
| 100 | + Write-Host "5. Add Environment Variables:" -ForegroundColor White |
| 101 | + Write-Host " FLASK_ENV=production" -ForegroundColor Gray |
| 102 | + Write-Host " FLASK_DEBUG=0" -ForegroundColor Gray |
| 103 | + Write-Host " PORT=8000" -ForegroundColor Gray |
| 104 | + Write-Host "" -ForegroundColor Green |
| 105 | + Write-Host "🌐 App Runner Console:" -ForegroundColor Yellow |
| 106 | + Write-Host " https://ap-southeast-2.console.aws.amazon.com/apprunner/home" -ForegroundColor Cyan |
| 107 | +} else { |
| 108 | + Write-Host "❌ Failed to push image to ECR" -ForegroundColor Red |
| 109 | + exit 1 |
| 110 | +} |
0 commit comments