From 1fda922c593b5641c291c86af811b1dc91947276 Mon Sep 17 00:00:00 2001 From: wate <110754234+r0liveir@users.noreply.github.com> Date: Sat, 5 Sep 2026 22:10:00 -0300 Subject: [PATCH] feat(infra): add AWS deployment stack Co-authored-by: Codex --- .gitignore | 6 ++ backend/Dockerfile | 59 ++++++++++++++++-- infra/.terraform.lock.hcl | 68 +++++++++++++++++++++ infra/README.md | 81 +++++++++++++++++++++++++ infra/alb.tf | 67 ++++++++++++++++++++ infra/cache.tf | 44 ++++++++++++++ infra/ecs.tf | 81 +++++++++++++++++++++++++ infra/main.tf | 37 +++++++++++ infra/network.tf | 16 +++++ infra/output.tf | 48 +++++++++++++++ infra/s3_cloudfront.tf | 97 +++++++++++++++++++++++++++++ infra/secrets.tf | 16 +++++ infra/terraform.tfvars.example | 12 ++++ infra/variables.tf | 108 +++++++++++++++++++++++++++++++++ 14 files changed, 736 insertions(+), 4 deletions(-) create mode 100644 infra/.terraform.lock.hcl create mode 100644 infra/README.md create mode 100644 infra/alb.tf create mode 100644 infra/cache.tf create mode 100644 infra/ecs.tf create mode 100644 infra/main.tf create mode 100644 infra/network.tf create mode 100644 infra/output.tf create mode 100644 infra/s3_cloudfront.tf create mode 100644 infra/secrets.tf create mode 100644 infra/terraform.tfvars.example create mode 100644 infra/variables.tf diff --git a/.gitignore b/.gitignore index 407dd1a..c67c704 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,9 @@ __pycache__ .ruff_cache .coverage coverage.xml + +# Terraform local state and values +infra/.terraform/ +infra/*.tfstate +infra/*.tfstate.* +infra/terraform.tfvars diff --git a/backend/Dockerfile b/backend/Dockerfile index 7de6f80..0374b8e 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -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", "*"] diff --git a/infra/.terraform.lock.hcl b/infra/.terraform.lock.hcl new file mode 100644 index 0000000..778ffc0 --- /dev/null +++ b/infra/.terraform.lock.hcl @@ -0,0 +1,68 @@ +# This file is maintained automatically by "terraform init". +# Manual edits may be lost in future updates. + +provider "registry.terraform.io/hashicorp/aws" { + version = "6.63.0" + constraints = ">= 5.83.0, >= 5.93.0, >= 6.28.0, >= 6.41.0, >= 6.46.0, < 7.0.0" + hashes = [ + "h1:9cre7jh1lSs/9igpgAcENMUAUlYW3HCtkav3up4oit0=", + "zh:005d56736afd17d963998c405cee6f434dbc23a415109f9435ff1542879ae611", + "zh:026ef126321a86ad7080b5d858e2527f96f5289678cbcd8856296e229c43339d", + "zh:06e0b58b2d1eddb5137fc86bee7ad2d07953c0bc3f57cccfc5ae0d2456068a3a", + "zh:07221735d61ababed84734e5ffcfc5bd59d01f29f029166ba5f2175895dceed1", + "zh:1a72db00583112bdb8c19b213a78a3f5de754fffc08f07e061f4e326289fab7d", + "zh:32968e74a53b03e97a084dc7050c22ef661fb5b3ea8a44f5a63e47bc45ad0e7c", + "zh:4b357dfe4b820e3e4acd2881cff8288b2186491e63416751f0d12692ba478ceb", + "zh:81e30884d7de686265e7d87bb92527e802878c65a378470ede2a1e9f4e40ccc9", + "zh:82e137297f6a5a08b9ce2138f7aabea245ad99495d9d9eff502f752d6ca90dbd", + "zh:8eb83b67099f0ea9df238a979dff933ff50ce06a2e3ff05a48556a10f10dd204", + "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425", + "zh:d0ba30886cbe41850fee689f51ef9088578f323cfd21817bb409951d43c465eb", + "zh:dd48e7089784454bc03d713e9057f5ca0ea1613bd402125054a51894957b7925", + "zh:f250fa81e54cf60fcb0e9c0fc4ac043f1ecc2ac24967f628b3609364fcab3d04", + "zh:f38fc09fc25a8d2cf89a4d4cd6a5ef7cb1aad72798dbdcad58b8876b6a551a54", + "zh:f7c7380fdf126e1901f2084588dbfd724c76cb131ccfa795a541219111103c06", + ] +} + +provider "registry.terraform.io/hashicorp/random" { + version = "3.9.0" + constraints = ">= 3.0.0, < 4.0.0" + hashes = [ + "h1:UlBuNVuCGJ39tTv2c5gz2NRZnQbXfbIWbTzWcth5o74=", + "zh:161ad0bd9a75768c82f53fb6e7172a9d8be2d4889b012645a34795031aaf1bf1", + "zh:19dc9a5b17729725ccfc4f45b0500af0ee5bc6b6b160c7adb8f2bf617d2c80ea", + "zh:269eda8fe42daa7974d5a34d166c3ba9defe80cde86c01e4dadcfdf2e1f05e5f", + "zh:373f7c65566f8f2cc7f45d698654feb9d988996957e1266a69ca00c52d6d16d0", + "zh:5599d16804c41c83009ec621b6d6b6f74e102f5827678a4750f8809055546b61", + "zh:583be0440469a22bff70dcfa56593b01566860b29607437264adb51060cf46fc", + "zh:5f211d8ec3f2e1f414870d9584bfe26e6995560ef81c748f8447a48164767398", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:7b547fd16216761ef86efc3ed516ac5ac0c5c42b7c7eb24a08cef2d93f69ed5e", + "zh:7e7c0679daf2a382151d05068c8c3f0dae6b7b7dccf818827b73dd08638df2ef", + "zh:8089dec888a8038b9b4fb23b3df7e1057293dbc5b60b42cc47ff690d69d4b61b", + "zh:c51f15a031edfd6f23ce8ced3446ca7f8d8d647e2499890d7d5d10d5016d7257", + "zh:c94784f005708890dc6895afd53636ec00ec1e430b15d41e5aebfb1d4b39bd04", + ] +} + +provider "registry.terraform.io/hashicorp/time" { + version = "0.14.1" + constraints = ">= 0.13.0" + hashes = [ + "h1:GJig5pIwiKDsiF73KLs7vWvDs76/x6DeNSxKrfqlA40=", + "zh:0837ca5b057e5cff94dff7de2fcccafb4abaa33c45de193fe2853e684818a267", + "zh:15a122f72d9e0f34fc5384cc7ec089319641fee5c319748a3aa02fc42f459969", + "zh:342fb83093a280ea7ee0654feae1f5867c62eb8eebc1ab46f9a7ab0b4c878a62", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:99f169834d3370b8341381c6a9c7a8b01fb26027531faa38e6fb49cc23916f68", + "zh:9f482917c7a28cf2436578be7aa9f04f8c811aba8b5949e0223ea987a2757a91", + "zh:ac6b5b8732826f2d1129a8a4a038ac7a7a9ca7b77d2a4608e5703be1a1e2bff0", + "zh:c54782a27d58ce04f6696c6fc0b2cf1e2fba6bed239fb520521a7bce7d7193cb", + "zh:c8d0ddc8f575ecb44f025d54edbfe118e26397fe328a67be62325766f31eb6e7", + "zh:d043b96f204edd2353bf6b2a34e645ffdee2e9634d9bb747331320444810a538", + "zh:e32c288501ca9a6c9d22b52e839dd391fc7083d54ee6b8dc296ce0e6bd3e57ef", + "zh:e47fcc7bb4e9ab5cc522c3b06e4fa9c0bf94b84be8210bc6b1655c44acb2addc", + "zh:f61bf218322bcbe0bd2d56bba738e7fa485e9b54244e13aa12de741b37d450c0", + ] +} diff --git a/infra/README.md b/infra/README.md new file mode 100644 index 0000000..551b265 --- /dev/null +++ b/infra/README.md @@ -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. diff --git a/infra/alb.tf b/infra/alb.tf new file mode 100644 index 0000000..ee77073 --- /dev/null +++ b/infra/alb.tf @@ -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 +} diff --git a/infra/cache.tf b/infra/cache.tf new file mode 100644 index 0000000..0085a28 --- /dev/null +++ b/infra/cache.tf @@ -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 +} diff --git a/infra/ecs.tf b/infra/ecs.tf new file mode 100644 index 0000000..7274d1e --- /dev/null +++ b/infra/ecs.tf @@ -0,0 +1,81 @@ +module "ecs" { + source = "terraform-aws-modules/ecs/aws" + version = "~> 7.0" + + cluster_name = "${local.name}-cluster" + + services = { + backend = { + cpu = var.backend_cpu + memory = var.backend_memory + + container_definitions = { + backend = { + image = var.backend_image + essential = true + portMappings = [ + { + name = "http" + containerPort = 8000 + protocol = "tcp" + } + ] + + environment = [ + { name = "ENVIRONMENT", value = "production" }, + { name = "LOG_LEVEL", value = var.backend_log_level }, + { name = "CORS_ORIGINS", value = var.cors_origins }, + { name = "PORT", value = "8000" } + ] + + secrets = [ + { name = "SUPABASE_URL", valueFrom = data.aws_secretsmanager_secret.supabase_url.arn }, + { name = "DATABASE_URL", valueFrom = data.aws_secretsmanager_secret.database_url.arn }, + { name = "REDIS_URL", valueFrom = data.aws_secretsmanager_secret.redis_url.arn } + ] + + enable_cloudwatch_logging = true + create_cloudwatch_log_group = true + cloudwatch_log_group_retention_in_days = var.backend_log_retention_days + } + } + + assign_public_ip = true + subnet_ids = module.vpc.public_subnets + + load_balancer = { + service = { + target_group_arn = module.alb.target_groups["backend"].arn + container_name = "backend" + container_port = 8000 + } + } + + task_exec_secret_arns = [ + data.aws_secretsmanager_secret.supabase_url.arn, + data.aws_secretsmanager_secret.database_url.arn, + data.aws_secretsmanager_secret.redis_url.arn + ] + + security_group_ingress_rules = { + ingress_alb = { + from_port = 8000 + to_port = 8000 + ip_protocol = "tcp" + description = "Backend HTTP from the ALB only" + referenced_security_group_id = module.alb.security_group_id + } + } + + security_group_egress_rules = { + egress_all = { + ip_protocol = "-1" + description = "Backend outbound internet and VPC traffic" + cidr_ipv4 = "0.0.0.0/0" + } + } + } + } + + tags = local.tags +} diff --git a/infra/main.tf b/infra/main.tf new file mode 100644 index 0000000..0b3b034 --- /dev/null +++ b/infra/main.tf @@ -0,0 +1,37 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 6.41, < 7.0" + } + random = { + source = "hashicorp/random" + version = ">= 3.0, < 4.0" + } + } + + required_version = ">= 1.5.7" +} + +provider "aws" { + region = var.aws_region +} + +data "aws_availability_zones" "available" { + state = "available" +} + +locals { + name = "${var.project_name}-${var.environment}" + azs = slice(data.aws_availability_zones.available.names, 0, var.az_count) + api_uses_https = var.api_certificate_arn != null && var.api_certificate_arn != "" + api_listener_port = local.api_uses_https ? 443 : 80 + api_listener_protocol = local.api_uses_https ? "HTTPS" : "HTTP" + api_host = var.api_domain_name != null && var.api_domain_name != "" ? var.api_domain_name : module.alb.dns_name + + tags = { + Project = var.project_name + Environment = var.environment + Terraform = "true" + } +} diff --git a/infra/network.tf b/infra/network.tf new file mode 100644 index 0000000..5e7814e --- /dev/null +++ b/infra/network.tf @@ -0,0 +1,16 @@ +module "vpc" { + source = "terraform-aws-modules/vpc/aws" + version = "~> 6.0" + + name = local.name + cidr = var.vpc_cidr + + azs = local.azs + public_subnets = [for index, _ in local.azs : cidrsubnet(var.vpc_cidr, 8, index)] + + enable_dns_hostnames = true + enable_dns_support = true + enable_nat_gateway = false + + tags = local.tags +} diff --git a/infra/output.tf b/infra/output.tf new file mode 100644 index 0000000..895ba9f --- /dev/null +++ b/infra/output.tf @@ -0,0 +1,48 @@ +output "api_alb_dns_name" { + description = "Temporary API hostname. Point an API DNS record at this later." + value = module.alb.dns_name +} + +output "api_base_url" { + description = "Temporary API base URL for smoke tests." + value = "${lower(local.api_listener_protocol)}://${local.api_host}" +} + +output "api_ws_url" { + description = "Temporary WebSocket base URL for smoke tests." + value = "${local.api_uses_https ? "wss" : "ws"}://${local.api_host}/sessions" +} + +output "frontend_bucket_name" { + description = "S3 bucket where frontend/dist should be synced." + value = module.s3_bucket.s3_bucket_id +} + +output "cloudfront_distribution_id" { + description = "CloudFront distribution ID used for cache invalidations." + value = module.cloudfront.cloudfront_distribution_id +} + +output "cloudfront_domain_name" { + description = "Temporary frontend hostname." + value = module.cloudfront.cloudfront_distribution_domain_name +} + +output "cloudfront_url" { + description = "Temporary frontend URL." + value = "https://${module.cloudfront.cloudfront_distribution_domain_name}" +} + +output "valkey_primary_endpoint" { + description = "Valkey primary endpoint address." + value = module.valkey.replication_group_primary_endpoint_address +} + +output "secret_names" { + description = "Existing Secrets Manager secrets read by Terraform." + value = { + supabase_url = data.aws_secretsmanager_secret.supabase_url.name + database_url = data.aws_secretsmanager_secret.database_url.name + redis_url = data.aws_secretsmanager_secret.redis_url.name + } +} diff --git a/infra/s3_cloudfront.tf b/infra/s3_cloudfront.tf new file mode 100644 index 0000000..0398ae9 --- /dev/null +++ b/infra/s3_cloudfront.tf @@ -0,0 +1,97 @@ +# 1. S3 Bucket Module (Secure by default: public access blocked, encrypted) +module "s3_bucket" { + source = "terraform-aws-modules/s3-bucket/aws" + version = "~> 4.0" + + bucket = var.frontend_bucket_name != null ? var.frontend_bucket_name : "${local.name}-frontend-assets-${var.aws_region}" + force_destroy = var.frontend_bucket_force_destroy + + # Let CloudFront OAC read the bucket + control_object_ownership = true + object_ownership = "BucketOwnerEnforced" + + # Automatically injects the CloudFront distribution ARN into the bucket policy + attach_policy = true + attach_deny_insecure_transport_policy = true + attach_require_latest_tls_policy = true + policy = data.aws_iam_policy_document.s3_policy.json + + tags = local.tags +} + +# 2. CloudFront Module (Handles OAC, distribution, and SPA error routing) +module "cloudfront" { + source = "terraform-aws-modules/cloudfront/aws" + version = "~> 6.0" + + comment = "Frontend SPA distribution" + enabled = true + is_ipv6_enabled = true + default_root_object = "index.html" + price_class = var.cloudfront_price_class + viewer_certificate = { + cloudfront_default_certificate = true + } + + origin_access_control = { + s3_oac = { + description = "CloudFront access to S3 frontend bucket" + origin_type = "s3" + signing_behavior = "always" + signing_protocol = "sigv4" + } + } + + origin = { + s3_frontend = { + domain_name = module.s3_bucket.s3_bucket_bucket_regional_domain_name + origin_access_control_key = "s3_oac" + } + } + + default_cache_behavior = { + target_origin_id = "s3_frontend" + viewer_protocol_policy = "redirect-to-https" + allowed_methods = ["GET", "HEAD", "OPTIONS"] + cached_methods = ["GET", "HEAD"] + use_forwarded_values = false + + # AWS Managed CachingOptimized policy + cache_policy_id = "658327ea-f89d-4fab-a63d-7e88639e58f6" + } + + # SPA Routing (403 and 404 redirected to index.html with HTTP 200) + custom_error_response = [ + { + error_code = 403 + response_code = 200 + response_page_path = "/index.html" + }, + { + error_code = 404 + response_code = 200 + response_page_path = "/index.html" + } + ] + + tags = local.tags +} + +# 3. Grant CloudFront OAC permission to read S3 objects +data "aws_iam_policy_document" "s3_policy" { + statement { + actions = ["s3:GetObject"] + resources = ["${module.s3_bucket.s3_bucket_arn}/*"] + + principals { + type = "Service" + identifiers = ["cloudfront.amazonaws.com"] + } + + condition { + test = "StringEquals" + variable = "AWS:SourceArn" + values = [module.cloudfront.cloudfront_distribution_arn] + } + } +} diff --git a/infra/secrets.tf b/infra/secrets.tf new file mode 100644 index 0000000..36a349d --- /dev/null +++ b/infra/secrets.tf @@ -0,0 +1,16 @@ +data "aws_secretsmanager_secret" "supabase_url" { + name = "${local.name}/SUPABASE_URL" +} + +data "aws_secretsmanager_secret" "database_url" { + name = "${local.name}/DATABASE_URL" +} + +data "aws_secretsmanager_secret" "redis_url" { + name = "${local.name}/REDIS_URL" +} + +resource "aws_secretsmanager_secret_version" "redis_url" { + secret_id = data.aws_secretsmanager_secret.redis_url.id + secret_string = "rediss://:${random_password.valkey_auth_token.result}@${module.valkey.replication_group_primary_endpoint_address}:6379/0" +} diff --git a/infra/terraform.tfvars.example b/infra/terraform.tfvars.example new file mode 100644 index 0000000..d386b8f --- /dev/null +++ b/infra/terraform.tfvars.example @@ -0,0 +1,12 @@ +# Leave null for initial HTTP smoke tests. Set a real ACM certificate ARN later +# to switch the API load balancer listener to HTTPS. +api_certificate_arn = null +api_domain_name = null +cors_origins = "https://d111111abcdef8.cloudfront.net" + +# Optional overrides: +# project_name = "webmun" +# environment = "prod" +# backend_image = "123456789012.dkr.ecr.sa-east-1.amazonaws.com/webmun-backend:latest" +# frontend_bucket_name = "my-globally-unique-webmun-frontend-bucket" +# frontend_bucket_force_destroy = true diff --git a/infra/variables.tf b/infra/variables.tf new file mode 100644 index 0000000..b003645 --- /dev/null +++ b/infra/variables.tf @@ -0,0 +1,108 @@ +variable "project_name" { + description = "Short project name used in AWS resource names." + type = string + default = "webmun" +} + +variable "environment" { + description = "Deployment environment name." + type = string + default = "prod" +} + +variable "aws_region" { + description = "The AWS region to deploy resources in" + type = string + default = "sa-east-1" +} + +variable "az_count" { + description = "Number of availability zones/public subnets to create." + type = number + default = 2 +} + +variable "vpc_cidr" { + description = "IPv4 CIDR block for the VPC." + type = string + default = "10.0.0.0/16" +} + +variable "api_certificate_arn" { + description = "Optional ACM certificate ARN for the API ALB HTTPS listener. Leave null for HTTP smoke tests." + type = string + default = null +} + +variable "api_domain_name" { + description = "Optional custom API hostname, for example api.webmun.net. Used in outputs after DNS points to the ALB." + type = string + default = null +} + +variable "backend_image" { + description = "Container image used by the ECS backend service." + type = string + default = "r0liveir/webmun-backend-app:latest" +} + +variable "backend_cpu" { + description = "Fargate CPU units for the backend task." + type = number + default = 256 +} + +variable "backend_memory" { + description = "Fargate memory in MiB for the backend task." + type = number + default = 512 +} + +variable "backend_log_level" { + description = "Backend LOG_LEVEL value." + type = string + default = "INFO" +} + +variable "backend_log_retention_days" { + description = "CloudWatch log retention for backend container logs." + type = number + default = 7 +} + +variable "cors_origins" { + description = "Comma-separated frontend origins allowed by the backend." + type = string + + validation { + condition = alltrue([ + for origin in split(",", var.cors_origins) : + can(regex("^https?://", trimspace(origin))) + ]) + error_message = "Each CORS origin must include http:// or https://, for example https://d111111abcdef8.cloudfront.net." + } +} + +variable "valkey_node_type" { + description = "ElastiCache node size for Valkey." + type = string + default = "cache.t4g.micro" +} + +variable "frontend_bucket_name" { + description = "Optional exact bucket name for frontend build artifacts. S3 bucket names are globally unique." + type = string + default = null +} + +variable "frontend_bucket_force_destroy" { + description = "Allow Terraform to delete the frontend bucket even when it contains files." + type = bool + default = false +} + +variable "cloudfront_price_class" { + description = "CloudFront edge location price class." + type = string + default = "PriceClass_100" +}