|
| 1 | +# 🚀 Deployment Guide - Customer Management API |
| 2 | + |
| 3 | +This guide covers multiple deployment methods for the Spring Boot backend to AWS Elastic Beanstalk. |
| 4 | + |
| 5 | +## 📋 Prerequisites |
| 6 | + |
| 7 | +### Required Tools |
| 8 | +- **Java 17+**: `java -version` |
| 9 | +- **Maven**: Already included via `./mvnw` |
| 10 | +- **AWS CLI** (for CLI deployment): [Install Guide](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) |
| 11 | +- **AWS Account** with Elastic Beanstalk access |
| 12 | + |
| 13 | +### AWS Setup |
| 14 | +1. **Create Elastic Beanstalk Application**: |
| 15 | + ```bash |
| 16 | + # Via AWS Console or CLI |
| 17 | + aws elasticbeanstalk create-application \ |
| 18 | + --application-name customer-management-api \ |
| 19 | + --description "Customer Management REST API" |
| 20 | + ``` |
| 21 | + |
| 22 | +2. **Create Environment**: |
| 23 | + ```bash |
| 24 | + aws elasticbeanstalk create-environment \ |
| 25 | + --application-name customer-management-api \ |
| 26 | + --environment-name customer-management-api-env \ |
| 27 | + --platform-arn "arn:aws:elasticbeanstalk:eu-north-1::platform/Java 17 running on 64bit Amazon Linux 2/3.x.x" |
| 28 | + ``` |
| 29 | + |
| 30 | +## 🎯 Deployment Methods |
| 31 | + |
| 32 | +### Method 1: Manual Build & Deploy (Beginner Friendly) |
| 33 | + |
| 34 | +**Step 1**: Build the deployment package |
| 35 | +```bash |
| 36 | +cd server |
| 37 | +./build-and-deploy.sh |
| 38 | +``` |
| 39 | + |
| 40 | +**Step 2**: Upload via AWS Console |
| 41 | +1. Go to [AWS Elastic Beanstalk Console](https://console.aws.amazon.com/elasticbeanstalk/) |
| 42 | +2. Select your application → Environment |
| 43 | +3. Click "Upload and Deploy" |
| 44 | +4. Upload the generated zip file from `deploy/deploy-YYYYMMDD-HHMMSS.zip` |
| 45 | +5. Click "Deploy" |
| 46 | + |
| 47 | +### Method 2: AWS CLI Deployment (Recommended for Development) |
| 48 | + |
| 49 | +**Prerequisites**: |
| 50 | +```bash |
| 51 | +# Install AWS CLI |
| 52 | +aws --version |
| 53 | + |
| 54 | +# Configure credentials |
| 55 | +aws configure |
| 56 | +``` |
| 57 | + |
| 58 | +**Deploy**: |
| 59 | +```bash |
| 60 | +cd server |
| 61 | +./deploy-aws-cli.sh [environment-name] |
| 62 | + |
| 63 | +# Example |
| 64 | +./deploy-aws-cli.sh customer-management-api-env |
| 65 | +``` |
| 66 | + |
| 67 | +### Method 3: GitHub Actions CI/CD (Recommended for Production) |
| 68 | + |
| 69 | +**Step 1**: Set up GitHub Secrets |
| 70 | +Go to your repository → Settings → Secrets and variables → Actions |
| 71 | + |
| 72 | +Add these secrets: |
| 73 | +- `AWS_ACCESS_KEY_ID`: Your AWS access key |
| 74 | +- `AWS_SECRET_ACCESS_KEY`: Your AWS secret key |
| 75 | + |
| 76 | +**Step 2**: Update workflow configuration |
| 77 | +Edit `.github/workflows/deploy-backend.yml`: |
| 78 | +```yaml |
| 79 | +application_name: your-eb-application-name |
| 80 | +environment_name: your-eb-environment-name |
| 81 | +region: your-aws-region |
| 82 | +``` |
| 83 | +
|
| 84 | +**Step 3**: Deploy |
| 85 | +Push changes to `main` branch: |
| 86 | +```bash |
| 87 | +git add . |
| 88 | +git commit -m "Deploy: Update backend" |
| 89 | +git push origin main |
| 90 | +``` |
| 91 | + |
| 92 | +## 🔧 Configuration |
| 93 | + |
| 94 | +### Environment Variables (Set in AWS EB Console) |
| 95 | + |
| 96 | +**Database Configuration**: |
| 97 | +``` |
| 98 | +DB_HOST=your-rds-endpoint.amazonaws.com |
| 99 | +DB_PORT=5432 |
| 100 | +DB_NAME=customerdb |
| 101 | +DB_USERNAME=your-username |
| 102 | +DB_PASSWORD=your-secure-password |
| 103 | +``` |
| 104 | +
|
| 105 | +**CORS Configuration**: |
| 106 | +``` |
| 107 | +CORS_ALLOWED_ORIGINS=https://main.d123456789.amplifyapp.com,https://your-frontend-domain.com |
| 108 | +``` |
| 109 | +
|
| 110 | +**Application Settings**: |
| 111 | +``` |
| 112 | +SPRING_PROFILES_ACTIVE=production |
| 113 | +JPA_DDL_AUTO=validate |
| 114 | +LOG_LEVEL=INFO |
| 115 | +``` |
| 116 | +
|
| 117 | +## 🏥 Health Checks |
| 118 | +
|
| 119 | +Elastic Beanstalk will use these endpoints: |
| 120 | +- **Health Check**: `/api/health` |
| 121 | +- **Ping Test**: `/api/ping` |
| 122 | +
|
| 123 | +## 📊 Monitoring |
| 124 | +
|
| 125 | +### CloudWatch Logs |
| 126 | +Logs are automatically streamed to CloudWatch: |
| 127 | +```bash |
| 128 | +# View logs via CLI |
| 129 | +aws logs describe-log-groups --log-group-name-prefix "/aws/elasticbeanstalk" |
| 130 | +``` |
| 131 | + |
| 132 | +### Application Metrics |
| 133 | +- **Health Dashboard**: AWS EB Console → Environment Health |
| 134 | +- **Application Logs**: AWS EB Console → Logs |
| 135 | +- **Monitoring**: CloudWatch metrics for requests, latency, errors |
| 136 | + |
| 137 | +## 🚨 Troubleshooting |
| 138 | + |
| 139 | +### Common Issues |
| 140 | + |
| 141 | +**1. Health Check Failures** |
| 142 | +```bash |
| 143 | +# Check if health endpoint responds |
| 144 | +curl https://your-app-url.elasticbeanstalk.com/api/health |
| 145 | +``` |
| 146 | + |
| 147 | +**2. Database Connection Issues** |
| 148 | +- Verify RDS security groups allow EB connections |
| 149 | +- Check environment variables are set correctly |
| 150 | +- Ensure database exists and credentials are valid |
| 151 | + |
| 152 | +**3. CORS Issues** |
| 153 | +- Update `CORS_ALLOWED_ORIGINS` with your frontend URL |
| 154 | +- Verify frontend is using correct backend URL |
| 155 | + |
| 156 | +### Debugging Commands |
| 157 | +```bash |
| 158 | +# Check environment status |
| 159 | +aws elasticbeanstalk describe-environments \ |
| 160 | + --application-name customer-management-api |
| 161 | + |
| 162 | +# Download logs |
| 163 | +aws elasticbeanstalk request-environment-info \ |
| 164 | + --environment-name customer-management-api-env \ |
| 165 | + --info-type tail |
| 166 | + |
| 167 | +# View recent events |
| 168 | +aws elasticbeanstalk describe-events \ |
| 169 | + --environment-name customer-management-api-env |
| 170 | +``` |
| 171 | + |
| 172 | +## 🔄 Rollback |
| 173 | + |
| 174 | +If deployment fails: |
| 175 | +```bash |
| 176 | +# List previous versions |
| 177 | +aws elasticbeanstalk describe-application-versions \ |
| 178 | + --application-name customer-management-api |
| 179 | + |
| 180 | +# Rollback to previous version |
| 181 | +aws elasticbeanstalk update-environment \ |
| 182 | + --environment-name customer-management-api-env \ |
| 183 | + --version-label previous-version-label |
| 184 | +``` |
| 185 | + |
| 186 | +## 🎯 Best Practices |
| 187 | + |
| 188 | +1. **Test Locally First**: Always test with `./mvnw spring-boot:run` |
| 189 | +2. **Database Migrations**: Use `validate` in production, `update` only in development |
| 190 | +3. **Environment Variables**: Never commit secrets to git |
| 191 | +4. **Monitoring**: Set up CloudWatch alarms for error rates |
| 192 | +5. **Blue/Green Deployments**: Use EB's deployment policies for zero-downtime deploys |
| 193 | + |
| 194 | +## 🔗 Useful Links |
| 195 | + |
| 196 | +- [AWS EB Java Guide](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-platform.html) |
| 197 | +- [Spring Boot EB Guide](https://spring.io/guides/gs/spring-boot-docker/) |
| 198 | +- [EB CLI Documentation](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb-cli3.html) |
0 commit comments