Twelve factor

From vinnie357/claude-skills · View interactive version

Twelve factor is an AI agent skill from the GitHub repository vinnie357/claude-skills. Guide for 12-Factor cloud-native applications. Use when designing microservices, configuring containers, deploying to Kubernetes, or following cloud-native patterns.

How to use this skill

Skill file

12-Factor App Methodology

Guide for building scalable, maintainable, and portable cloud-native applications following the 12-Factor App principles and modern extensions.

Anti-fabrication

This skill follows core:anti-fabrication. The 12 factors themselves are the

methodology cited in sources.md (12factor.net) — a stable, versionless reference, so

naming a factor correctly is low-risk. The risk sits in the worked examples: the

Kubernetes manifests, runtime.exs snippets, and Docker Compose configs in

references/kubernetes.md and references/factor-examples.md assert real API fields

(e.g. livenessProbe, ConfigMap keys) that do change across Kubernetes versions —

verify a manifest field against current API docs before asserting it applies, rather

than assuming an older example still matches the current API.

The 12 Factors

I. Codebase

One codebase tracked in revision control, many deploys

Violations: multiple repositories for one application, per-environment codebases, copying code between repositories. Fix: consolidate to one repository and move the differences into environment config.

II. Dependencies

Explicitly declare and isolate dependencies

Worked Dockerfile and per-language manifest list: references/factor-examples.md.

III. Config

Store config in the environment

All configuration comes from environment variables — anything that varies between deploys: credentials, hostnames, ports, pool sizes, backing service URLs, model names.

# Elixir - config/runtime.exs
config :my_app, MyApp.Repo,
  hostname: System.get_env("DATABASE_HOST") || "localhost",
  password: System.fetch_env!("DATABASE_PASSWORD"),
  pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")

What makes a violation, and the smallest correct fix:

Litmus test from 12factor.net: the codebase could be made open source at any moment without compromising any credentials.

Kubernetes ConfigMap/Secret wiring: references/kubernetes.md.

IV. Backing Services

Treat backing services as attached resources

V. Build, Release, Run

Strictly separate build and run stages

  1. Build: convert code to executable bundle
  2. Release: combine build with config
  3. Run: execute in target environment

CI/CD pipeline example: references/factor-examples.md.

VI. Processes

Execute the app as one or more stateless processes

Session-store example: references/factor-examples.md. Deployment manifest: references/kubernetes.md.

VII. Port Binding

Export services via port binding

VIII. Concurrency

Scale out via the process model

IX. Disposability

Maximize robustness with fast startup and graceful shutdown

<!-- vale Flavored.Superlatives = NO -->

<!-- vale Flavored.Superlatives = YES -->

Graceful-shutdown example: references/factor-examples.md.

X. Dev/Prod Parity

Keep development, staging, and production as similar as possible

Docker Compose dev-environment example: references/factor-examples.md.

XI. Logs

Treat logs as event streams

XII. Admin Processes

Run admin/management tasks as one-off processes

Kubernetes Job/CronJob examples: references/kubernetes.md.

Modern Extensions (Beyond 12)

Three extensions round out contemporary cloud-native practice — full detail and examples in references/modern-extensions.md:

Common Patterns

Configuration Validation

Validate required configuration at startup — fail fast, naming the missing variables:

function validateConfig() {
  const required = ['DATABASE_URL', 'JWT_SECRET', 'REDIS_URL'];
  const missing = required.filter(key => !process.env[key]);

  if (missing.length > 0) {
    throw new Error(`Missing required environment variables: ${missing.join(', ')}`);
  }
}

// Call before starting server
validateConfig();

Health checks, graceful degradation, and the troubleshooting guide: references/operations.md.

Anti-Patterns to Avoid

❌ Environment-Specific Code Paths

// DON'T: branch on environment
if (process.env.NODE_ENV === 'production') { /* different behavior */ }

// DO: one code path, configured value
const timeout = parseInt(process.env.TIMEOUT || '5000');

❌ Local File Storage

// DON'T: write persistent data to local filesystem
fs.writeFile('/tmp/uploads/' + filename, data);

// DO: use object storage
await s3.putObject({ Bucket: process.env.S3_BUCKET, Key: filename, Body: data });

❌ In-Memory State

// DON'T: store state in memory
const sessions = new Map();

// DO: use external store
const session = await redis.get(`session:${sessionId}`);

❌ Hardcoded Dependencies

// DON'T: hardcode service locations
const db = connect('localhost:5432');

// DO: use environment variables
const db = connect(process.env.DATABASE_URL);

Best Practices Summary

  1. Environment variables for all configuration
  2. Stateless processes that can scale horizontally
  3. Structured logging to stdout
  4. Containers for development parity
  5. Automated CI/CD pipelines
  6. Health checks for orchestration
  7. Graceful shutdown handling
  8. Fast startup times (< 10s)
  9. Immutable releases with unique IDs
  10. Monitoring and telemetry via metrics and tracing

Resources

Key Insights

"The twelve-factor methodology can be applied to apps written in any programming language, and which use any combination of backing services (database, queue, memory cache, etc)."

"The twelve-factor app never assumes that anything cached in memory or on disk will be available on a future request or job – with many processes of each type running, chances are high that a future request will be served by a different process." — Factor VI, Processes

Design applications from day one to be cloud-native, scalable, and maintainable.

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.