Infrastructure as Code for otaku.lt using Terraform and Cloudflare Workers.
This project manages the complete infrastructure for otaku.lt, a Next.js website deployed on Cloudflare Workers. The infrastructure uses Terraform for infrastructure management and Wrangler for deployment automation.
- π Cloudflare Workers deployment
- ποΈ Terraform infrastructure management
- π Automated CI/CD with GitHub Actions
- π¦ Wrangler deployment automation
- π Secure authentication via environment variables
- π Custom domain with SSL/TLS
providers.tf- Terraform, GitHub, and Cloudflare provider configurationvariables.tf- Input variables for configurationworkers.tf- Cloudflare Workers configurationcloudflare.tf- Legacy configuration (deprecated)github.tf- GitHub repository resourcesoutputs.tf- Output valuesMakefile- Project automation and deployment commands.env.example- Environment variables template
-
Install required tools:
# On macOS using Homebrew brew install terraform brew install gh brew install cloudflare-wrangler -
Authenticate:
gh auth login # GitHub authentication # Get API token from: https://dash.cloudflare.com/profile/api-tokens
-
Setup environment:
make workers-setup
-
Deploy everything:
make workers-apply
That's it! Your site should be live at https://otaku.lt
# Authenticate with both services
gh auth login
# Set up Cloudflare API token (get from dashboard)
make cf-set-token TOKEN=your_api_token_here# Review infrastructure changes
make plan
# Apply infrastructure (Workers, DNS, etc.)
make apply# Build and deploy to Workers
make workers-deploy
# OR deploy to preview/staging
make workers-deploy-previewmake setup- Setup environment and check dependenciesmake plan- Run terraform planmake apply- Run terraform applymake fmt- Format terraform filesmake validate- Validate configurationmake clean- Clean terraform cache
make workers-build- Build Next.js applicationmake workers-deploy- Deploy to production Workersmake workers-deploy-preview- Deploy to preview/stagingmake workers-test- Test Workers locallymake workers-setup- Complete Workers setupmake workers-apply- Apply infrastructure + deploymake workers-status- Check deployment status
make cf-set-token TOKEN=xyz- Set Cloudflare API token from dashboardmake cf-set-token TOKEN=xyz- Set custom API tokenmake help- Show all available commands
π Secure Authentication via Environment Variables
The Makefile automatically handles authentication:
# Authenticate with GitHub
gh auth login
# Set up API token (get from Cloudflare dashboard)
make cf-set-token TOKEN=your_api_token_here# Create custom API token at: https://dash.cloudflare.com/profile/api-tokens
# Required permissions: Zone:Zone:Read, Zone:DNS:Edit, Account:Cloudflare Workers:Edit
make cf-set-token TOKEN=your_custom_api_tokenThe project uses .env file for configuration (set up with make cf-set-token):
# Example .env file (auto-generated)
CLOUDFLARE_API_TOKEN=your_api_token
CLOUDFLARE_ZONE_ID=your_zone_id
CLOUDFLARE_ACCOUNT_ID=your_account_id
DOMAIN_NAME=otaku.lt
PAGES_PROJECT_NAME=otaku-ltThis Terraform configuration manages:
- otaku.lt: Main website repository (Next.js application)
- otaku.lt-sdk: Infrastructure as Code repository (this repository)
- Worker Script: Main application handler with static asset serving
- Custom Domains: DNS configuration for otaku.lt and www.otaku.lt
- Worker Routes: Traffic routing and WWW redirect
- SSL/TLS: Security and performance settings
- DNS Records: A records pointing to Workers
- Code Push β GitHub repository (otaku.lt)
- GitHub Actions β Builds Next.js app and deploys to Workers
- Workers β Serves static assets and handles routing
- Live Site β https://otaku.lt
- Build β
make workers-build - Deploy β
make workers-deploy - Live Site β https://otaku.lt
The project uses these environment variables (managed in .env):
# Cloudflare (set using cf-set-token)
CLOUDFLARE_API_TOKEN=your_api_token
CLOUDFLARE_ZONE_ID=your_zone_id
CLOUDFLARE_ACCOUNT_ID=your_account_id
# Project configuration
DOMAIN_NAME=otaku.lt
PAGES_PROJECT_NAME=otaku-lt
# GitHub (handled automatically by Makefile)
GITHUB_TOKEN=$(gh auth token)
GITHUB_OWNER=otaku-ltThe Next.js application includes:
name = "otaku-lt"
main = "src/index.js"
compatibility_date = "2024-01-15"
[assets]
directory = "out"
serve_single_page_app = true{
"scripts": {
"deploy": "npm run build && wrangler deploy",
"deploy:preview": "npm run build && wrangler deploy --env preview",
"wrangler:dev": "wrangler dev"
}
}Automatic deployment is configured via .github/workflows/deploy.yml:
- Pull Requests: Deploy to preview environment
- Main Branch: Deploy to production
The required GitHub Actions secrets are automatically managed by Terraform:
# Setup GitHub Actions secrets for CI/CD
make github-secretsThis creates the following repository secrets:
CLOUDFLARE_API_TOKEN- Your Cloudflare API tokenCLOUDFLARE_ACCOUNT_ID- Your Cloudflare account ID
# Full automated setup
gh auth login # Authenticate with GitHub
gh auth login # Authenticate with GitHub
# Get API token from: https://dash.cloudflare.com/profile/api-tokens
make cf-set-token TOKEN=xyz # Set up credentials
make workers-setup # Setup infrastructure
make github-secrets # Configure CI/CD secrets
make workers-apply # Deploy everythingAfter this setup, your CI/CD pipeline will automatically:
- Deploy previews for pull requests
- Deploy to production when merging to main
- Comment on PRs with preview URLs
- Account ID: Cloudflare Dashboard β Right sidebar
- Zone ID: Cloudflare Dashboard β Your domain β Right sidebar
- API Token: Use
make cf-set-token TOKEN=xyzor create at API Tokens
For custom tokens, required permissions:
Zone:Zone:ReadZone:DNS:EditAccount:Cloudflare Workers:Edit
otaku.lt-sdk/ # Infrastructure
βββ workers.tf # Workers configuration
βββ cloudflare.tf # Legacy configuration (deprecated)
βββ providers.tf # Provider configuration
βββ variables.tf # Variable definitions
βββ outputs.tf # Output definitions
βββ Makefile # Automation commands
βββ .env # Credentials (auto-generated)
βββ README.md # This file
otaku.lt/ # Frontend Application
βββ wrangler.toml # Workers configuration
βββ src/index.js # Worker script
βββ .github/workflows/ # CI/CD
βββ package.json # Updated with Wrangler
βββ next.config.js # Next.js configuration
βββ ... (Next.js app)
cd otaku.lt
npm run dev # Next.js dev server
# OR
make workers-test # Test Workers locallymake workers-deploy # Deploy to production
make workers-deploy-preview # Deploy to stagingmake plan # Review changes
make apply # Apply changes-
Authentication errors:
make cf-set-token TOKEN=your_api_token_here
-
Build failures:
cd otaku.lt npm install npm run build -
Deployment issues:
make workers-status # Check deployment status wrangler tail # View logs
-
DNS issues:
make plan # Check Terraform DNS config
- Never commit
.envor files containing sensitive data - The
.gitignorefile excludes sensitive files - Use remote state backend for production use
- Rotate API tokens regularly
When making changes:
- Test locally:
make workers-test - Review infrastructure:
make plan - Deploy preview:
make workers-deploy-preview - Apply to production:
make workers-apply - Commit changes to both
otaku.ltandotaku.lt-sdk