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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ __pycache__
.ruff_cache
.coverage
coverage.xml

# Terraform local state and values
infra/.terraform/
infra/*.tfstate
infra/*.tfstate.*
infra/terraform.tfvars
59 changes: 55 additions & 4 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,66 @@
FROM python:3.11-alpine
# ------------------
# Build stage
# ------------------

FROM python:3.11-slim AS base

COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

ENV UV_COMPILE_BYTECODE=1 \
UV_LINK_MODE=copy \
PYTHONUNBUFFERED=1

WORKDIR /app
COPY . .
COPY pyproject.toml uv.lock .

ENV UV_NO_DEV=1
# ------------------
# Development env
# ------------------
FROM base AS development

# By copying only the uv lock files, we dont recheck with uv sync everytime a line of python is changed
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-install-project

RUN uv sync --locked
# notice how we dont copy source code here: it is redundant, since we have bind mount

ENV PATH="/app/.venv/bin:$PATH"

CMD ["uv", "run", "fastapi", "dev", "--host", "0.0.0.0", "--port", "8000"]

# ------------------
# Prod stage
# ------------------
FROM base AS prod-builder

ENV UV_NO_DEV=1

RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-install-project

# here we do copy, since we wont use bind mount
COPY . .
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen

# ------------------
# Prod stage
# ------------------

FROM python:3.11-slim AS production

WORKDIR /app

RUN groupadd -r appuser && useradd -r -g appuser appuser

# Copy .venv with project dependencies and source code
COPY --from=prod-builder /app/.venv /app/.venv
COPY --from=prod-builder --chown=appuser:appuser /app /app

ENV PATH="/app/.venv/bin:$PATH" \
PYTHONUNBUFFERED=1

USER appuser
EXPOSE 8000

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--proxy-headers", "--forwarded-allow-ips", "*"]
68 changes: 68 additions & 0 deletions infra/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions infra/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Infrastructure folder

Prerequisites: Terraform, AWS CLI, Docker, and npm.

Terraform provisions the AWS infrastructure for the FastAPI backend and the
React frontend. Configure AWS credentials before running these commands.

## Setup

Create these plaintext secrets in AWS Secrets Manager before the first apply:

```text
webmun-prod/SUPABASE_URL
webmun-prod/DATABASE_URL
webmun-prod/REDIS_URL
```

Then initialize and apply the infrastructure:

```bash
cd infra
cp terraform.tfvars.example terraform.tfvars
terraform init
terraform plan
terraform apply
```

Keep `terraform.tfvars`, `.terraform/`, and Terraform state files local. Commit
`.terraform.lock.hcl` so provider versions remain reproducible.

## Deploy backend

Build and push the production image configured by `backend_image`:

```bash
docker build --target production -t r0liveir/webmun-backend-app:latest backend
docker push r0liveir/webmun-backend-app:latest
```

When reusing the `latest` tag, restart the ECS service so it pulls the new image:

```bash
aws ecs update-service \
--region sa-east-1 \
--cluster webmun-prod-cluster \
--service backend \
--force-new-deployment
```

Prefer immutable image tags for releases. Update `backend_image` in
`terraform.tfvars` and run `terraform apply` when changing the tag.

## Deploy frontend

Build the static frontend with the production endpoints:

```bash
cd frontend
VITE_API_URL="$(cd ../infra && terraform output -raw api_base_url)" \
VITE_WS_URL="$(cd ../infra && terraform output -raw api_ws_url)" \
VITE_SUPABASE_URL="https://your-project.supabase.co" \
VITE_SUPABASE_PUBLISHABLE_KEY="your-supabase-publishable-key" \
npm run build
```

Upload it to S3 and invalidate CloudFront:

```bash
aws s3 sync dist/ "s3://$(cd ../infra && terraform output -raw frontend_bucket_name)" --delete
aws cloudfront create-invalidation \
--distribution-id "$(cd ../infra && terraform output -raw cloudfront_distribution_id)" \
--paths "/*"
```

When the frontend origin changes, update `cors_origins` in `terraform.tfvars`
and run `terraform apply`.

## Update backend secrets

Update secret values directly in AWS Secrets Manager. ECS reads them when a
task starts, so force a new backend deployment after every secret change.
67 changes: 67 additions & 0 deletions infra/alb.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 10.0"

name = "${local.name}-api"
load_balancer_type = "application"

vpc_id = module.vpc.vpc_id
subnets = module.vpc.public_subnets

enable_deletion_protection = false

security_group_ingress_rules = {
api = {
from_port = local.api_listener_port
to_port = local.api_listener_port
ip_protocol = "tcp"
cidr_ipv4 = "0.0.0.0/0"
description = "${local.api_listener_protocol} from the internet"
}
}

security_group_egress_rules = {
all = {
ip_protocol = "-1"
cidr_ipv4 = "0.0.0.0/0"
description = "Allow ALB outbound traffic to ECS tasks"
}
}

listeners = {
api = {
port = local.api_listener_port
protocol = local.api_listener_protocol
certificate_arn = local.api_listener_protocol == "HTTPS" ? var.api_certificate_arn : null

forward = {
target_group_key = "backend"
}
}
}

target_groups = {
backend = {
name_prefix = "api-"
protocol = "HTTP"
port = 8000
target_type = "ip"
deregistration_delay = 30
create_attachment = false
load_balancing_cross_zone_enabled = true

health_check = {
enabled = true
interval = 30
path = "/docs"
healthy_threshold = 2
unhealthy_threshold = 3
timeout = 5
protocol = "HTTP"
matcher = "200-399"
}
}
}

tags = local.tags
}
44 changes: 44 additions & 0 deletions infra/cache.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
resource "random_password" "valkey_auth_token" {
length = 48
special = false
}

module "valkey" {
source = "terraform-aws-modules/elasticache/aws"
version = "~> 1.0"

replication_group_id = "${local.name}-cache"
description = "WebMUN Redis-compatible Valkey cache"

engine = "valkey"
engine_version = "7.2"
node_type = var.valkey_node_type

automatic_failover_enabled = false
multi_az_enabled = false
transit_encryption_enabled = true
auth_token = random_password.valkey_auth_token.result
apply_immediately = true

vpc_id = module.vpc.vpc_id
security_group_rules = {
ingress_from_backend = {
description = "Valkey from ECS backend tasks"
from_port = 6379
to_port = 6379
ip_protocol = "tcp"
referenced_security_group_id = module.ecs.services["backend"].security_group_id
}
}

subnet_group_name = "${local.name}-cache"
subnet_group_description = "Public subnet group for first-pass WebMUN Valkey"
subnet_ids = module.vpc.public_subnets

create_parameter_group = true
parameter_group_name = "${local.name}-cache"
parameter_group_family = "valkey7"
parameter_group_description = "WebMUN Valkey parameters"

tags = local.tags
}
Loading
Loading