Lesson 2Secrets

Secrets and Certificates

Credentials, API keys และ certificates ต้องมี lifecycle ชัดเจน: store, access, rotate, audit และ revoke โดยไม่ฝังไว้ใน Git หรือ AMI

Secrets Manager

AWS Secrets Manager เหมาะกับ secrets ที่ต้องการ rotation, versioning และ integration กับ services เช่น RDS ตัวอย่างเช่น database username/password, third-party API token หรือ application signing secret

aws secretsmanager create-secret \
  --name prod/app/db \
  --description "Database credential for prod app" \
  --secret-string '{"username":"appuser","password":"REPLACE_ME"}'

Systems Manager Parameter Store

Parameter Store เหมาะกับ configuration และ secret ที่เรียบง่ายกว่า เช่น feature flag, endpoint, AMI ID หรือ secure string ข้อดีคือใช้งานง่ายและผูกกับ IAM/KMS ได้ แต่ automatic rotation ไม่ใช่จุดแข็งหลักเท่า Secrets Manager

Secrets Manager vs Parameter Store

หัวข้อSecrets ManagerParameter Store
เหมาะกับCredentials ที่ต้อง rotateConfig และ secure parameters
Rotationรองรับ rotation workflowต้องออกแบบเองเป็นหลัก
Cost modelคิดค่าบริการต่อ secret/API ตามเงื่อนไขมี standard tier และ advanced tier
ตัวอย่างRDS password, API tokenApp endpoint, environment value, simple secure string

AWS Certificate Manager

AWS Certificate Manager ใช้ provision และ manage TLS certificates สำหรับ services เช่น ALB, CloudFront และ API Gateway สำหรับ public certificate ที่ออกโดย ACM และใช้กับ integrated services, ACM ช่วยต่ออายุ certificate อัตโนมัติเมื่อ validation ยังถูกต้อง

Best practices

  • ให้ application อ่าน secret ด้วย IAM role และ Least Privilege
  • อย่า print secret ลง log, error message หรือ build output
  • ตั้ง naming convention เช่น /prod/payments/db/password หรือ prod/payments/db
  • แยก secrets ระหว่าง dev, staging และ prod
  • ใช้ ACM certificate กับ HTTPS endpoint แทน self-managed certificate เมื่อเหมาะสม

Common mistakes

  • เก็บ secrets ใน Terraform state โดยไม่ระวัง เพราะ state อาจมี plaintext sensitive values
  • ใช้ secret เดียวกันทุก environment ทำให้ blast radius ใหญ่
  • ไม่มี rotation plan เมื่อมีคนออกจากทีม หรือ credential อาจรั่ว

Review questions

  1. Secrets Manager เหมาะกว่า Parameter Store เมื่อไร?
  2. ทำไมไม่ควรเก็บ secret ใน source code?
  3. ACM ช่วยลดงาน operation เรื่อง TLS อย่างไร?