name: Environment Matrix: Dev/Stg/Prod
description: Environment standards for development, staging, and production: parity principles, data and access policies, configuration boundaries, and deployment controls
Environment Matrix: Dev/Stg/Prod
Overview
มาตรฐานการจัดการ environments (development, staging, production) รวมถึง configs, data policies, access controls ที่แตกต่างกันแต่ละ environment
Why This Matters
- Parity: Environments ใกล้เคียงกัน ลด "works on my machine"
- Safety: Production protected, staging for testing
- Clarity: รู้ว่า environment ไหนทำอะไรได้
- Compliance: Proper access controls
Core Concepts
1. Parity (เหมือนกันเท่าที่จำเป็น)
- staging ควร “production-like” ในเรื่อง topology, config shape, observability, and deployment flow
- ต่างเฉพาะสิ่งที่จำเป็นต่อความปลอดภัย/ต้นทุน (scale, data, access)
2. Data Policy
- dev: seed/mock ได้ แต่ต้องป้องกันการใช้ข้อมูลจริงโดยไม่ตั้งใจ
- staging: sanitized copy หรือ synthetic data (ห้าม PII ถ้าไม่จำเป็น)
- prod: real data + audit + retention policy
3. Access & Change Control
- prod ต้องมี least privilege, approvals, และ audit trail
- staging ใช้สำหรับ validation และ rehearsal ของ deployments/rollbacks
Environment Matrix
| Aspect |
Development |
Staging |
Production |
| Purpose |
Local dev, experiments |
Pre-prod testing |
Live users |
| Data |
Seed/mock |
Sanitized copy |
Real |
| Scale |
Minimal |
Production-like |
Full |
| Access |
All devs |
Restricted |
Very restricted |
| Logging |
Verbose |
Standard |
Standard + audit |
| Feature flags |
All enabled |
Selective |
Controlled rollout |
| Secrets |
Local vault |
Staging vault |
Prod vault |
| Domain |
localhost |
staging.example.com |
example.com |
Quick Start
- ตั้ง
APP_ENV=development|staging|production|test เป็น source-of-truth
- สร้าง “config matrix” ที่ keys เหมือนกันทุก env (ต่างเฉพาะค่า)
- แยก secrets ออกจาก config และใช้ secret manager ต่อ env
- ทำ access control matrix และกำหนดว่า “ใครทำอะไรได้” ต่อ env
Config Per Environment
// config/environments.ts
export const environments = {
development: {
apiUrl: 'http://localhost:3000',
database: 'dev_db',
logLevel: 'debug',
features: { all: true },
},
staging: {
apiUrl: 'https://staging-api.example.com',
database: 'staging_db',
logLevel: 'info',
features: { experimental: true },
},
production: {
apiUrl: 'https://api.example.com',
database: 'prod_db',
logLevel: 'warn',
features: { stable: true },
},
};
Access Control Matrix
| Role |
Dev |
Staging |
Production |
| Developer |
Full |
Read + Deploy |
Read logs only |
| Tech Lead |
Full |
Full |
Read + Deploy |
| SRE/DevOps |
Full |
Full |
Full |
| QA |
Read |
Full |
Read only |
Production Checklist
Anti-patterns
- Staging != Prod: staging ใช้งานจริงไม่ได้ ทำให้ bug หลุด prod
- Sharing secrets: ใช้ secret เดียวกันทุก env (risk สูงมาก)
- Real data in dev/staging: PII leak และ compliance risk
- No ownership: ไม่มีคนรับผิดชอบ access/policies ของแต่ละ env
Integration Points
- CI/CD environments (GitHub Environments, approvals, secrets)
- Secret managers and IAM
- Observability (separate tenants/projects per env)
- Data pipelines (masking/sanitization jobs)
Further Reading
1---2name: env-matrix-dev-stg-prod3description: มาตรฐานการจัดการ environments (development, staging, production) รวมถึง configs, data policies, access controls ที่แตกต่างกันแต่ละ environment4---5
6---
7name: Environment Matrix: Dev/Stg/Prod
8description: Environment standards for development, staging, and production: parity principles, data and access policies, configuration boundaries, and deployment controls
9---
10
11# Environment Matrix: Dev/Stg/Prod
12
13## Overview
14
15มาตรฐานการจัดการ environments (development, staging, production) รวมถึง configs, data policies, access controls ที่แตกต่างกันแต่ละ environment
16
17## Why This Matters
18
19- **Parity**: Environments ใกล้เคียงกัน ลด "works on my machine"
20- **Safety**: Production protected, staging for testing
21- **Clarity**: รู้ว่า environment ไหนทำอะไรได้
22- **Compliance**: Proper access controls
23
24---
25
26## Core Concepts
27
28### 1. Parity (เหมือนกันเท่าที่จำเป็น)
29
30- staging ควร “production-like” ในเรื่อง topology, config shape, observability, and deployment flow
31- ต่างเฉพาะสิ่งที่จำเป็นต่อความปลอดภัย/ต้นทุน (scale, data, access)
32
33### 2. Data Policy
34
35- dev: seed/mock ได้ แต่ต้องป้องกันการใช้ข้อมูลจริงโดยไม่ตั้งใจ
36- staging: sanitized copy หรือ synthetic data (ห้าม PII ถ้าไม่จำเป็น)
37- prod: real data + audit + retention policy
38
39### 3. Access & Change Control
40
41- prod ต้องมี least privilege, approvals, และ audit trail
42- staging ใช้สำหรับ validation และ rehearsal ของ deployments/rollbacks
43
44## Environment Matrix
45
46| Aspect | Development | Staging | Production |
47|--------|-------------|---------|------------|
48| Purpose | Local dev, experiments | Pre-prod testing | Live users |
49| Data | Seed/mock | Sanitized copy | Real |
50| Scale | Minimal | Production-like | Full |
51| Access | All devs | Restricted | Very restricted |
52| Logging | Verbose | Standard | Standard + audit |
53| Feature flags | All enabled | Selective | Controlled rollout |
54| Secrets | Local vault | Staging vault | Prod vault |
55| Domain | localhost | staging.example.com | example.com |
56
57## Quick Start
58
591. ตั้ง `APP_ENV=development|staging|production|test` เป็น source-of-truth
602. สร้าง “config matrix” ที่ keys เหมือนกันทุก env (ต่างเฉพาะค่า)
613. แยก secrets ออกจาก config และใช้ secret manager ต่อ env
624. ทำ access control matrix และกำหนดว่า “ใครทำอะไรได้” ต่อ env
63
64## Config Per Environment
65
66```typescript
67// config/environments.ts
68export const environments = {
69 development: {
70 apiUrl: 'http://localhost:3000',
71 database: 'dev_db',
72 logLevel: 'debug',
73 features: { all: true },
74 },
75 staging: {
76 apiUrl: 'https://staging-api.example.com',
77 database: 'staging_db',
78 logLevel: 'info',
79 features: { experimental: true },
80 },
81 production: {
82 apiUrl: 'https://api.example.com',
83 database: 'prod_db',
84 logLevel: 'warn',
85 features: { stable: true },
86 },
87};
88```
89
90## Access Control Matrix
91
92| Role | Dev | Staging | Production |
93|------|-----|---------|------------|
94| Developer | Full | Read + Deploy | Read logs only |
95| Tech Lead | Full | Full | Read + Deploy |
96| SRE/DevOps | Full | Full | Full |
97| QA | Read | Full | Read only |
98
99## Production Checklist
100
101- [ ] staging deploy flow เหมือน production (ต่างเฉพาะ scale/data/access)
102- [ ] data policy ชัดเจน (sanitized, retention, PII handling)
103- [ ] secrets แยกตาม env (ไม่มี reuse ข้าม env)
104- [ ] prod มี approvals + audit logs สำหรับ deploy/config changes
105- [ ] feature flags rollout rules ต่างกันชัดเจนระหว่าง envs
106
107## Anti-patterns
108
1091. **Staging != Prod**: staging ใช้งานจริงไม่ได้ ทำให้ bug หลุด prod
1102. **Sharing secrets**: ใช้ secret เดียวกันทุก env (risk สูงมาก)
1113. **Real data in dev/staging**: PII leak และ compliance risk
1124. **No ownership**: ไม่มีคนรับผิดชอบ access/policies ของแต่ละ env
113
114## Integration Points
115
116- CI/CD environments (GitHub Environments, approvals, secrets)
117- Secret managers and IAM
118- Observability (separate tenants/projects per env)
119- Data pipelines (masking/sanitization jobs)
120
121## Further Reading
122
123- [12-Factor App: Dev/Prod Parity](https://12factor.net/dev-prod-parity)
124- [Environment Management](https://docs.github.com/en/actions/deployment/targeting-different-environments)