Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions .github/workflows/deploy-backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Deploy to Elastic Beanstalk

on:
push:
branches:
- main
paths:
- 'server/**'
workflow_dispatch: # Allow manual trigger

jobs:
deploy:
runs-on: ubuntu-latest
name: Deploy Backend to AWS Elastic Beanstalk

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'corretto'

- name: Cache Maven packages
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2

- name: Build Spring Boot application
run: |
cd server
./mvnw clean package -DskipTests

- name: Verify build output
run: |
cd server
ls -la target/
if [ ! -f "target/relational-data-access-complete-0.0.1-SNAPSHOT.jar" ]; then
echo "Build failed - JAR file not found!"
exit 1
fi

- name: Prepare deployment package
run: |
cd server
mkdir -p deploy-package
cp target/relational-data-access-complete-0.0.1-SNAPSHOT.jar deploy-package/
cp deploy/Procfile deploy-package/
cp -r deploy/.ebextensions deploy-package/

- name: Create deployment zip
run: |
cd server/deploy-package
zip -r ../deploy-${{ github.sha }}.zip .

- name: Deploy to Elastic Beanstalk
uses: einaregilsson/beanstalk-deploy@v22
with:
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
application_name: customer-management-api # Replace with your EB app name
environment_name: customer-management-api-env # Replace with your EB env name
region: eu-north-1 # Replace with your AWS region
version_label: ${{ github.sha }}
deployment_package: server/deploy-${{ github.sha }}.zip

- name: Upload deployment artifact
uses: actions/upload-artifact@v4
if: always()
with:
name: deployment-package-${{ github.sha }}
path: server/deploy-${{ github.sha }}.zip
198 changes: 198 additions & 0 deletions server/DEPLOYMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
# 🚀 Deployment Guide - Customer Management API

This guide covers multiple deployment methods for the Spring Boot backend to AWS Elastic Beanstalk.

## 📋 Prerequisites

### Required Tools
- **Java 17+**: `java -version`
- **Maven**: Already included via `./mvnw`
- **AWS CLI** (for CLI deployment): [Install Guide](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)
- **AWS Account** with Elastic Beanstalk access

### AWS Setup
1. **Create Elastic Beanstalk Application**:
```bash
# Via AWS Console or CLI
aws elasticbeanstalk create-application \
--application-name customer-management-api \
--description "Customer Management REST API"
```

2. **Create Environment**:
```bash
aws elasticbeanstalk create-environment \
--application-name customer-management-api \
--environment-name customer-management-api-env \
--platform-arn "arn:aws:elasticbeanstalk:eu-north-1::platform/Java 17 running on 64bit Amazon Linux 2/3.x.x"
```

## 🎯 Deployment Methods

### Method 1: Manual Build & Deploy (Beginner Friendly)

**Step 1**: Build the deployment package
```bash
cd server
./build-and-deploy.sh
```

**Step 2**: Upload via AWS Console
1. Go to [AWS Elastic Beanstalk Console](https://console.aws.amazon.com/elasticbeanstalk/)
2. Select your application → Environment
3. Click "Upload and Deploy"
4. Upload the generated zip file from `deploy/deploy-YYYYMMDD-HHMMSS.zip`
5. Click "Deploy"

### Method 2: AWS CLI Deployment (Recommended for Development)

**Prerequisites**:
```bash
# Install AWS CLI
aws --version

# Configure credentials
aws configure
```

**Deploy**:
```bash
cd server
./deploy-aws-cli.sh [environment-name]

# Example
./deploy-aws-cli.sh customer-management-api-env
```

### Method 3: GitHub Actions CI/CD (Recommended for Production)

**Step 1**: Set up GitHub Secrets
Go to your repository → Settings → Secrets and variables → Actions

Add these secrets:
- `AWS_ACCESS_KEY_ID`: Your AWS access key
- `AWS_SECRET_ACCESS_KEY`: Your AWS secret key

**Step 2**: Update workflow configuration
Edit `.github/workflows/deploy-backend.yml`:
```yaml
application_name: your-eb-application-name
environment_name: your-eb-environment-name
region: your-aws-region
```

**Step 3**: Deploy
Push changes to `main` branch:
```bash
git add .
git commit -m "Deploy: Update backend"
git push origin main
```

## 🔧 Configuration

### Environment Variables (Set in AWS EB Console)

**Database Configuration**:
```
DB_HOST=your-rds-endpoint.amazonaws.com
DB_PORT=5432
DB_NAME=customerdb
DB_USERNAME=your-username
DB_PASSWORD=your-secure-password
```

**CORS Configuration**:
```
CORS_ALLOWED_ORIGINS=https://main.d123456789.amplifyapp.com,https://your-frontend-domain.com
```

**Application Settings**:
```
SPRING_PROFILES_ACTIVE=production
JPA_DDL_AUTO=validate
LOG_LEVEL=INFO
```

## 🏥 Health Checks

Elastic Beanstalk will use these endpoints:
- **Health Check**: `/api/health`
- **Ping Test**: `/api/ping`

## 📊 Monitoring

### CloudWatch Logs
Logs are automatically streamed to CloudWatch:
```bash
# View logs via CLI
aws logs describe-log-groups --log-group-name-prefix "/aws/elasticbeanstalk"
```

### Application Metrics
- **Health Dashboard**: AWS EB Console → Environment Health
- **Application Logs**: AWS EB Console → Logs
- **Monitoring**: CloudWatch metrics for requests, latency, errors

## 🚨 Troubleshooting

### Common Issues

**1. Health Check Failures**
```bash
# Check if health endpoint responds
curl https://your-app-url.elasticbeanstalk.com/api/health
```

**2. Database Connection Issues**
- Verify RDS security groups allow EB connections
- Check environment variables are set correctly
- Ensure database exists and credentials are valid

**3. CORS Issues**
- Update `CORS_ALLOWED_ORIGINS` with your frontend URL
- Verify frontend is using correct backend URL

### Debugging Commands
```bash
# Check environment status
aws elasticbeanstalk describe-environments \
--application-name customer-management-api

# Download logs
aws elasticbeanstalk request-environment-info \
--environment-name customer-management-api-env \
--info-type tail

# View recent events
aws elasticbeanstalk describe-events \
--environment-name customer-management-api-env
```

## 🔄 Rollback

If deployment fails:
```bash
# List previous versions
aws elasticbeanstalk describe-application-versions \
--application-name customer-management-api

# Rollback to previous version
aws elasticbeanstalk update-environment \
--environment-name customer-management-api-env \
--version-label previous-version-label
```

## 🎯 Best Practices

1. **Test Locally First**: Always test with `./mvnw spring-boot:run`
2. **Database Migrations**: Use `validate` in production, `update` only in development
3. **Environment Variables**: Never commit secrets to git
4. **Monitoring**: Set up CloudWatch alarms for error rates
5. **Blue/Green Deployments**: Use EB's deployment policies for zero-downtime deploys

## 🔗 Useful Links

- [AWS EB Java Guide](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-platform.html)
- [Spring Boot EB Guide](https://spring.io/guides/gs/spring-boot-docker/)
- [EB CLI Documentation](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3.html)
55 changes: 55 additions & 0 deletions server/build-and-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

# Build and Deploy Script for Elastic Beanstalk
# Usage: ./build-and-deploy.sh

set -e # Exit on any error

echo "🏗️ Building Spring Boot Application..."

# Clean and build the application
echo "Cleaning previous builds..."
./mvnw clean

echo "Building JAR file..."
./mvnw package -DskipTests

# Check if build was successful
if [ ! -f "target/relational-data-access-complete-0.0.1-SNAPSHOT.jar" ]; then
echo "❌ Build failed - JAR file not found!"
exit 1
fi

echo "✅ Build successful!"

# Prepare deployment directory
echo "📦 Preparing deployment package..."

# Clean deploy directory
rm -rf deploy/temp
mkdir -p deploy/temp

# Copy JAR file to deploy directory
cp target/relational-data-access-complete-0.0.1-SNAPSHOT.jar deploy/temp/

# Copy Procfile and .ebextensions
cp deploy/Procfile deploy/temp/
cp -r deploy/.ebextensions deploy/temp/

# Create deployment zip
echo "Creating deployment zip..."
cd deploy/temp
zip -r ../deploy-$(date +%Y%m%d-%H%M%S).zip .
cd ../..

# Clean up temp directory
rm -rf deploy/temp

echo "✅ Deployment package created: deploy/deploy-$(date +%Y%m%d-%H%M%S).zip"
echo ""
echo "📋 Next steps:"
echo "1. Go to AWS Elastic Beanstalk Console"
echo "2. Select your application environment"
echo "3. Click 'Upload and Deploy'"
echo "4. Upload the zip file: deploy/deploy-$(date +%Y%m%d-%H%M%S).zip"
echo "5. Deploy!"
Loading