Deployment with Terraform & Render
Relevant source files
The following files were used as context for generating this wiki page:
Polar utilizes Terraform as its primary Infrastructure as Code (IaC) tool to manage cloud resources across different environments, including production, sandbox, and test. The core backend infrastructure—comprising the API server, background workers, and cron jobs—is hosted on Render, while frontend assets and file storage leverage Vercel and AWS S3 respectively.
The deployment workflow is centralized through HCP Terraform Cloud, which manages state, secrets, and execution runs. This ensures that infrastructure changes are consistent, repeatable, and version-controlled. Sources: terraform/README.md:3-12, terraform/modules/render_service/main.tf:1-10
Infrastructure Architecture
The Polar deployment architecture is a multi-cloud setup involving Render for compute, AWS for storage and CDN, and Vercel for the frontend.
Component Overview
- Render: Hosts the Python-based FastAPI server (API), Dramatiq background workers, and scheduled cron jobs.
- AWS: Provides S3 buckets for file storage (invoices, receipts, public uploads) and CloudFront for global content delivery.
- Vercel: Hosts the Next.js web application.
- PostgreSQL & Redis: Managed databases utilized by the backend services.
Sources: terraform/README.md:14-18, terraform/modules/render_service/main.tf:1-10, DEVELOPMENT.md:5-10
Deployment Flow Diagram
The following diagram illustrates the relationship between the IaC management layer and the target cloud providers.
Render Service Configuration
The render_service module is the core component for backend deployment. It manages environment groups, web services, and cron jobs.
Environment Groups
Render environment groups are used to share secrets and configuration across multiple services. Terraform manages several specific groups:
| Group Name | Purpose | Key Variables |
|---|---|---|
backend | Core application settings | POLAR_ENV, POLAR_BASE_URL, POLAR_JWKS |
aws_s3 | Storage credentials | POLAR_AWS_ACCESS_KEY_ID, POLAR_S3_FILES_BUCKET_NAME |
stripe | Payment integration | POLAR_STRIPE_SECRET_KEY, POLAR_STRIPE_WEBHOOK_SECRET |
github | OAuth and App integration | POLAR_GITHUB_CLIENT_ID, POLAR_GITHUB_CLIENT_SECRET |
pydantic_ai_gateway | AI Proxy configuration | POLAR_PYDANTIC_AI_GATEWAY_API_KEY |
Sources: terraform/modules/render_service/main.tf:13-118, terraform/README.md:31-43
Service Definitions
Terraform defines three primary types of compute resources on Render:
- API Web Service (
render_web_service.api): The main FastAPI entry point. It includes health check paths (/healthz) and autoscaling configurations (min 2, max 4 for production). Sources: terraform/modules/render_service/main.tf:318-373 - Workers (
render_web_service.worker): Instances dedicated to processing background jobs using Dramatiq. These are deployed as web services with custom start commands. Sources: terraform/modules/render_service/main.tf:375-408 - Cron Jobs (
render_cron_job.cron): Scheduled tasks that run on a specific interval (e.g., daily cleanup or reporting). Sources: terraform/modules/render_service/main.tf:410-440
Runtime and Scaling
Render services are configured to deploy from Docker images. In Terraform, these are set to the latest tag initially, while CI/CD processes handle specific image digest deployments out-of-band.
runtime_source = {
image = {
image_url = split("@", var.api_service_config.image_url)[0]
registry_credential_id = var.registry_credential_id
tag = "latest"
}
}
lifecycle {
ignore_changes = [runtime_source.image]
}
Sources: terraform/modules/render_service/main.tf:328-338, terraform/modules/render_service/main.tf:387-397
AWS Integration
AWS is utilized primarily for persistent storage and high-availability content delivery. The aws.tf configuration manages S3 buckets and CloudFront distributions.
S3 Buckets and Application Access
Polar segments data into several specialized S3 buckets. Application access is controlled through the application_access module, which creates IAM users with scoped policies for the production environment.
| Bucket Capability | Description |
|---|---|
customer_invoices | Storage for generated PDF invoices |
public_files | Public uploads such as product media |
files | Private files for downloadable benefits |
logs | Long-term backup of OpenTelemetry spans |
Sources: terraform/production/aws.tf:261-271, terraform/production/aws.tf:181-185
Content Delivery (CloudFront)
CloudFront distributions are configured for uploads.polar.sh and cdn.polar.sh. These distributions are integrated with S3 buckets and, in the case of public files, a Lambda@Edge function for real-time image resizing. Sources: terraform/production/aws.tf:205-231
Preview Environments
Outside of the primary Render/Vercel production flow, Polar uses a specialized script-based deployment for PR previews on virtual machines.
Preview Setup and Deployment
setup-vm.sh: Initializes a VM with Caddy (using Tailscale for SSL), Redis, Node.js, and Python (uv). It configures a token-gated API proxy for Vercel SSR. Sources: infra/preview/setup-vm.sh:10-90deploy.sh: Orchestrates the actual deployment of a specific PR branch. It handles git checkouts, backend dependency syncing, database migrations, and Caddy configuration regeneration. Sources: infra/preview/deploy.sh:40-150
Environment Variable Management
The source of truth for all environment variables and secrets is Terraform.
- Backend Variables: Defined in
render.tf(or modules) and attached torender_env_groupresources. Values are set as Terraform Variables in HCP Terraform Cloud. Sources: terraform/README.md:33-43 - Frontend Variables: Managed in Vercel-specific Terraform files or global configuration. Sources: terraform/README.md:55-60
[!WARNING]
Environment variables should never be created directly in the Render dashboard; Terraform must remain the sole source of truth to maintain infrastructure consistency. Sources: terraform/README.md:51-53
Summary
The Polar deployment system leverages Terraform to orchestrate a sophisticated multi-cloud environment. By combining Render's compute capabilities for the backend, Vercel's edge network for the frontend, and AWS's storage primitives, the project achieves a scalable and maintainable infrastructure. The use of HCP Terraform Cloud ensures that all secrets and state are managed securely and transparently across the engineering team. Sources: terraform/README.md:3-18, terraform/modules/render_service/main.tf:1-10