To deploy an Angular and FastAPI stack to AWS, compile the Angular app into static files and package the FastAPI backend into a Docker image. The workflow below tests both apps on dev, deploys them to a staging environment on staging, and builds the final production release on main. Step-1: Prepare Your AWS and GitHub Secrets Before creating the workflow, set up credentials in GitHub under Settings > Environments (dev, staging, and production). AWS Authentication. Store AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION in each environment (or use AWS OIDC roles for keyless authentication). AWS S3 (Angular): Add AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY (Secrets), and AWS_REGION, S3_BUCKET_NAME, CLOUDFRONT_ID (Variables). AWS EC2 (FastAPI): Add EC2_HOST (public IP or DNS), EC2_USER (e.g. ubuntu or ec2-user), and EC2_SSH_KEY (.pem private key) as Secrets. Deployment Targets. Add environment-specific variables like S3_BUCKET_NAME for Angular and ECR_REPOSITORY or EC2_HOST for FastAPI. Step-2: Full-Stack GitHub Actions Workflow Save configuration as .github/workflows/aws-fullstack-pipeline.yml. It assumes the repository has /frontend (Angular) and /backend (FastAPI) folders. # aws-fullstack-pipeline.yml name: Angular + FastAPI AWS Pipeline on: push: branches: - dev - staging - main jobs: # DEV STAGE: Test Angular and FastAPI test-dev: runs-on: ubuntu-latest steps: - uses: actions/checkout@v5 # Test Angular - name: Set up Node.js uses: actions/setup-node@v5 with: node-version: '20' cache: 'npm' cache-dependency-path: frontend/package-lock.json - name: Install & Test Angular working-directory: ./frontend run: | npm ci npm run test -- --watch=false --browsers=ChromeHeadless # Test FastAPI - name: Set up Python uses: actions/setup-python@v5 with: python-version: '3.11' cache: 'pip' cache-dependency-path: backend/requirements.txt - name: Install & Test FastAPI working-directory: ./backend run: | pip install -r requirements.txt pytest # STAGING STAGE: Build & Deploy to AWS Staging deploy-staging: needs: test-dev if: github.ref == 'refs/heads/staging' || github.ref == 'refs/heads/main' runs-on: ubuntu-latest environment: staging steps: - uses: actions/checkout@v5 - name: Configure AWS Credentials uses: aws-actions/configure-aws-credentials@v4 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: ${{ vars.AWS_REGION }} # Build & Deploy Angular to Staging S3 - name: Build Angular (Staging) working-directory: ./frontend run: | npm ci npm run build -- --configuration=staging - name: Sync to Staging S3 run: aws s3 sync frontend/dist/ s3://${{ vars.S3_BUCKET_NAME }} --delete # Build & Push FastAPI Docker Image to Staging ECR - name: Login to Amazon ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v2 - name: Build & Push FastAPI Staging Image working-directory: ./backend env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }} IMAGE_TAG: staging-${{ github.sha }} run: | docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG # Add your ECS update-service or EC2 SSH restart command here # PRODUCTION BUILD STAGE: Final Release on Main production-build: needs: deploy-staging if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest environment: production steps: - uses: actions/checkout@v4 - name: Configure AWS Credentials uses: aws-actions/configure-aws-credentials@v4 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: ${{ vars.AWS_REGION }} # Final Production Build for Angular - name: Build Angular (Production) working-directory: ./frontend run: | npm ci npm run build -- --configuration=production - name: Deploy to Production S3 & Invalidate CloudFront run: | aws s3 sync frontend/dist/ s3://${{ vars.S3_BUCKET_NAME }} --delete aws cloudfront create-invalidation --distribution-id ${{ vars.CLOUDFRONT_ID }} --paths "/*" # Final Production Build for FastAPI - name: Login to Amazon ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v2 - name: Build & Push FastAPI Production Image working-directory: ./backend env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }} IMAGE_TAG: latest run: | docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG Step-3: Stack-Specific Tips Angular environment files: Ensure angular.json has build configurations defined for staging and production so the right backend API URLs get bundled during "npm run build". FastAPI containerization: Place a Dockerfile inside the /backend folder that runs Uvicorn or Gunicorn so AWS can easily pull and run the container. Branch promotion: Require Pull Requests to merge dev into staging, and staging into main, so code cannot skip the testing or staging gates. =============================================================================== With Angular and FastAPI in a single monorepo and FastAPI hosted on AWS EC2, optimize the pipeline using path filters to only rebuild and deploy the parts of the app that actually changed. 1. Required GitHub Environment Secrets & Variables: same as described earlier 2. Monorepo Workflow (aws-ec2-monorepo.yml) Save file to .github/workflows/aws-ec2-monorepo.yaml. It assumes the folders are named frontend/ and backend/, and that FastAPI runs on EC2 via a systemd service or Docker. # aws-ec2-monorepo.yml name: Monorepo Angular + FastAPI (EC2) on: push: branches: - dev - staging - main jobs: # DETECT CHANGES: Check if frontend or backend files were modified changes: runs-on: ubuntu-latest outputs: frontend: ${{ steps.filter.outputs.frontend }} backend: ${{ steps.filter.outputs.backend }} steps: - uses: actions/checkout@v4 - uses: dorny/paths-filter@v3 id: filter with: filters: | frontend: - 'frontend/**' backend: - 'backend/**' # DEV STAGE: Test modified components test-dev: needs: changes runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Test Angular if: needs.changes.outputs.frontend == 'true' working-directory: ./frontend run: | npm ci npm run test -- --watch=false --browsers=ChromeHeadless - name: Test FastAPI if: needs.changes.outputs.backend == 'true' working-directory: ./backend run: | python -m pip install --upgrade pip pip install -r requirements.txt pytest # STAGING STAGE: Deploy to Staging S3 & Staging EC2 deploy-staging: needs: [changes, test-dev] if: github.ref == 'refs/heads/staging' || github.ref == 'refs/heads/main' runs-on: ubuntu-latest environment: staging steps: - uses: actions/checkout@v4 # Deploy Angular to Staging S3 - name: Configure AWS Credentials if: needs.changes.outputs.frontend == 'true' uses: aws-actions/configure-aws-credentials@v4 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: ${{ vars.AWS_REGION }} - name: Build & Upload Angular (Staging) if: needs.changes.outputs.frontend == 'true' working-directory: ./frontend run: | npm ci npm run build -- --configuration=staging aws s3 sync dist/ s3://${{ vars.S3_BUCKET_NAME }} --delete # Deploy FastAPI to Staging EC2 via SSH - name: Deploy FastAPI to Staging EC2 if: needs.changes.outputs.backend == 'true' uses: appleboy/ssh-action@v1.0.3 with: host: ${{ secrets.EC2_HOST }} username: ${{ secrets.EC2_USER }} key: ${{ secrets.EC2_SSH_KEY }} script: | cd /var/www/app/backend git fetch origin staging git reset --hard origin/staging source venv/bin/activate pip install -r requirements.txt sudo systemctl restart fastapi # PRODUCTION BUILD STAGE: Final Release on Main production-build: needs: [changes, deploy-staging] if: github.ref == 'refs/heads/main' runs-on: ubuntu-latest environment: production steps: - uses: actions/checkout@v4 # Final Production Build for Angular - name: Configure AWS Credentials if: needs.changes.outputs.frontend == 'true' uses: aws-actions/configure-aws-credentials@v4 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: ${{ vars.AWS_REGION }} - name: Build & Deploy Angular (Production) if: needs.changes.outputs.frontend == 'true' working-directory: ./frontend run: | npm ci npm run build -- --configuration=production aws s3 sync dist/ s3://${{ vars.S3_BUCKET_NAME }} --delete aws cloudfront create-invalidation --distribution-id ${{ vars.CLOUDFRONT_ID }} --paths "/*" # Final Production Deploy for FastAPI on EC2 - name: Deploy FastAPI to Production EC2 if: needs.changes.outputs.backend == 'true' uses: appleboy/ssh-action@v1.0.3 with: host: ${{ secrets.EC2_HOST }} username: ${{ secrets.EC2_USER }} key: ${{ secrets.EC2_SSH_KEY }} script: | cd /var/www/app/backend git fetch origin main git reset --hard origin/main source venv/bin/activate pip install -r requirements.txt sudo systemctl restart fastapi 3. EC2 Server Setup Requirements For the SSH deployment commands above to work smoothly on your EC2 instances, make sure server is prepared. Sparse checkout (optional): To avoid pulling the Angular frontend/ folder onto the EC2 server, run git sparse-checkout set backend inside /var/www/app on the EC2 instance. Systemd service: Create a /etc/systemd/system/fastapi.service file on EC2 so Uvicorn/Gunicorn runs in the background and restarts automatically. Passwordless sudo for restart: Allow EC2 user to restart only the FastAPI service without a password prompt by adding ubuntu ALL=(ALL) NOPASSWD: /bin/systemctl restart fastapi via sudo visudo Angular build output path: Check angular.json outputPath (e.g. dist/app-name/browser) and update the aws s3 sync source path to match it.