Module 5: AWS Fargate & CI/CD

Serverless Container Deployment.

The Stateless Web

Deploying WordPress to containers requires a shift in mindset. The filesystem is ephemeral.

Key Concepts

1. The Container Philosophy

Containers are ephemeral. Any file written to disk (like uploads) will be lost on restart. We must offload state.

2. Dockerfile for WordPress

We need a multi-stage build. Stage 1 installs Composer dependencies. Stage 2 copies them to the final PHP-FPM image.

FROM composer:2 as builder
COPY . /app
RUN composer install --no-dev

FROM php:8.2-fpm-alpine
COPY --from=builder /app /var/www/html

3. Nginx Configuration

Run Nginx in a separate container (sidecar) or use a single image with Supervisor. We prefer the sidecar pattern for Fargate.

4. S3 Offload

Install `humanmade/s3-uploads` via Composer to automatically push media library uploads to an AWS S3 bucket.

5. Environment Secrets

Never bake secrets into the image. Inject them at runtime using AWS Secrets Manager or ECS Environment Variables.

6. AWS Fargate Setup

Define a Task Definition with two containers: `php` and `nginx`. Map port 80 to the Nginx container.

7. Database Connection

Connect to AWS RDS (PostgreSQL) using the endpoint provided in the environment variables.

8. Redis for Sessions

Configure PHP to store sessions in Redis, not on the filesystem, to allow horizontal scaling.

session.save_handler = redis
session.save_path = "tcp://redis:6379"

9. GitLab CI/CD Pipeline

Build the Docker image, push to ECR, and update the ECS service on every merge to `main`.

10. Health Checks

Implement a `/healthz` endpoint in Nginx/PHP to let the Load Balancer know if the container is ready to accept traffic.