|
| 1 | +# AWS App Runner Service Role Configuration |
| 2 | + |
| 3 | +## 🔐 Service Role Requirements |
| 4 | + |
| 5 | +AWS App Runner membutuhkan IAM service role untuk mengakses ECR dan layanan AWS lainnya. |
| 6 | + |
| 7 | +### Option 1: Auto-create Service Role (Recommended) |
| 8 | +Saat membuat App Runner service, pilih **"Create new service role"** - AWS akan otomatis membuat role dengan permissions yang tepat. |
| 9 | + |
| 10 | +### Option 2: Manual Service Role Creation |
| 11 | + |
| 12 | +#### 1. Create IAM Role |
| 13 | +```json |
| 14 | +{ |
| 15 | + "Version": "2012-10-17", |
| 16 | + "Statement": [ |
| 17 | + { |
| 18 | + "Effect": "Allow", |
| 19 | + "Principal": { |
| 20 | + "Service": "build.apprunner.amazonaws.com" |
| 21 | + }, |
| 22 | + "Action": "sts:AssumeRole" |
| 23 | + } |
| 24 | + ] |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +#### 2. Attach Policy |
| 29 | +Role name: `AppRunnerECRAccessRole` |
| 30 | + |
| 31 | +**AWS Managed Policy:** |
| 32 | +- `AWSAppRunnerServicePolicyForECRAccess` |
| 33 | + |
| 34 | +**Or Custom Policy:** |
| 35 | +```json |
| 36 | +{ |
| 37 | + "Version": "2012-10-17", |
| 38 | + "Statement": [ |
| 39 | + { |
| 40 | + "Effect": "Allow", |
| 41 | + "Action": [ |
| 42 | + "ecr:GetAuthorizationToken" |
| 43 | + ], |
| 44 | + "Resource": "*" |
| 45 | + }, |
| 46 | + { |
| 47 | + "Effect": "Allow", |
| 48 | + "Action": [ |
| 49 | + "ecr:BatchCheckLayerAvailability", |
| 50 | + "ecr:GetDownloadUrlForLayer", |
| 51 | + "ecr:BatchGetImage", |
| 52 | + "ecr:DescribeRepositories", |
| 53 | + "ecr:DescribeImages" |
| 54 | + ], |
| 55 | + "Resource": "arn:aws:ecr:ap-southeast-2:147845229604:repository/permit-api" |
| 56 | + } |
| 57 | + ] |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## 📋 App Runner Service Configuration |
| 62 | + |
| 63 | +### Container Configuration |
| 64 | +```yaml |
| 65 | +Source: Container registry |
| 66 | +Provider: Amazon ECR |
| 67 | +Container image URI: 147845229604.dkr.ecr.ap-southeast-2.amazonaws.com/permit-api:latest |
| 68 | +Deployment trigger: Manual |
| 69 | +``` |
| 70 | +
|
| 71 | +### Build Configuration |
| 72 | +```yaml |
| 73 | +Service role: AppRunnerECRAccessRole (or auto-created) |
| 74 | +``` |
| 75 | +
|
| 76 | +### Service Configuration |
| 77 | +```yaml |
| 78 | +Service name: permit-api-service |
| 79 | +Virtual CPU: 1 vCPU |
| 80 | +Virtual memory: 2 GB |
| 81 | +Port: 8000 |
| 82 | +Environment variables: |
| 83 | + FLASK_ENV: production |
| 84 | + FLASK_DEBUG: 0 |
| 85 | + PORT: 8000 |
| 86 | + API_KEYS: demo_basic_key:DemoBasic:basic,demo_premium_key:DemoPremium:premium |
| 87 | + MASTER_API_KEY: demo_master_key_12345 |
| 88 | + LOG_LEVEL: INFO |
| 89 | +``` |
| 90 | +
|
| 91 | +### Auto-scaling Configuration |
| 92 | +```yaml |
| 93 | +Minimum size: 1 |
| 94 | +Maximum size: 3 |
| 95 | +Max concurrency: 100 per instance |
| 96 | +``` |
| 97 | +
|
| 98 | +### Health Check Configuration |
| 99 | +```yaml |
| 100 | +Health check path: /health |
| 101 | +Health check interval: 20 seconds |
| 102 | +Health check timeout: 5 seconds |
| 103 | +Healthy threshold: 1 |
| 104 | +Unhealthy threshold: 5 |
| 105 | +``` |
| 106 | +
|
| 107 | +## 🚀 Step-by-Step Creation |
| 108 | +
|
| 109 | +1. **AWS App Runner Console**: https://ap-southeast-2.console.aws.amazon.com/apprunner/home |
| 110 | +2. **Create service** → Container registry |
| 111 | +3. **ECR Image**: `147845229604.dkr.ecr.ap-southeast-2.amazonaws.com/permit-api:latest` |
| 112 | +4. **Service role**: Choose "Create new service role" or select existing `AppRunnerECRAccessRole` |
| 113 | +5. **Configure** service settings as above |
| 114 | +6. **Review and create** |
| 115 | + |
| 116 | +## ✅ Verification Steps |
| 117 | + |
| 118 | +After deployment: |
| 119 | +1. **Check service status**: Should show "Running" |
| 120 | +2. **Test health endpoint**: `https://your-app-url.awsapprunner.com/health` |
| 121 | +3. **Test API with key**: |
| 122 | + ```bash |
| 123 | + curl -H "Authorization: Bearer demo_basic_key" \ |
| 124 | + "https://your-app-url.awsapprunner.com/global/emissions?limit=5" |
| 125 | + ``` |
| 126 | + |
| 127 | +## 🔧 Troubleshooting |
| 128 | + |
| 129 | +**Common Issues:** |
| 130 | +- **Service role permissions**: Ensure ECR access is granted |
| 131 | +- **Container port**: Must match Dockerfile EXPOSE 8000 |
| 132 | +- **Health check**: Verify `/health` endpoint responds |
| 133 | +- **Environment variables**: Check all required vars are set |
0 commit comments