Prompt
I'm building a startup stack with GitHub Actions, Terraform, and containers. How should I handle secrets?
Latest observation
A good rule of thumb: keep secrets out of GitHub, out of Terraform state, and out of container images.
For a startup stack with GitHub Actions, Terraform, and containers, the cleanest approach is:
1) Use a real secrets manager
Use one central place for runtime secrets, such as:
- AWS Secrets Manager / SSM Parameter Store
- GCP Secret Manager
- Azure Key Vault
- HashiCorp Vault if you want cloud-neutral control
Store things like:
- DB passwords
- API keys
- OAuth client secrets
- signing keys
- webhook secrets
2) Let GitHub Actions handle only CI/CD credentials
GitHub Actions should usually store only:
- short-lived cloud auth credentials, or better
- OIDC-based federation to your cloud provider
Prefer GitHub Actions OIDC over long-lived cloud access keys in GitHub Secrets.
That way, Actions can assume a role with temporary credentials instead of storing static keys.
Good pattern
- GitHub Actions authenticates to AWS/GCP/Azure via OIDC
- Pipeline deploys infra/app
- App reads secrets from cloud secrets manager at runtime
3) Keep Terraform from owning secret values
Terraform is great for infrastructure, but don’t put sensitive secret material directly into Terraform variables if you can avoid it.
Avoid
- hardcoding secrets in
.tffiles - passing secret values through Terraform and letting them end up in state
- storing secret values in outputs
Terraform state can contain sensitive values even if marked sensitive = true in UI/output. That flag hides values in logs, but does not remove them from state.
Better patterns
Use Terraform to:
- create secret containers/resources
- set IAM policies/permissions
- configure secret references
But have the secret value injected separately, for example:
- manually via a secure admin process
- from CI at deploy time using ephemeral auth
- from your cloud’s secret manager outside Terraform
- via external data sources only if you fully understand the state implications
4) Inject secrets at runtime, not build time
Don’t bake secrets into Docker images or build args.
Avoid
ARG API_KEY=...ENV API_KEY=...in Dockerfile- copying
.envinto images
Prefer
- container reads secrets from environment at runtime
- mounted secret files
- secret manager integration from your platform
Examples:
- ECS task secrets from AWS Secrets Manager
- Kubernetes secrets via External Secrets Operator / CSI driver
- Cloud Run / App Service / etc. native secret integration
5) Use GitHub Secrets only for non-cloud-sensitive items or bootstrap
GitHub Secrets is fine for:
- small bootstrap values
- third-party service tokens
- temporary migration credentials
- webhook signing secrets for CI workflows
But if possible:
- prefer OIDC for cloud access
- prefer external secrets manager for app runtime secrets
6) Separate secret classes
Treat these differently:
CI/CD secrets
Used by GitHub Actions to deploy.
- Prefer OIDC
- If static secret is unavoidable, rotate often
Application runtime secrets
Used by your app in production.
- Store in secret manager
- Fetch at boot/runtime
- Rotate independently
Local development secrets
Used by developers locally.
.envfiles in.gitignore- use direnv, dotenv, or a local secret manager
- never commit them
7) Protect Terraform state
Since state may contain sensitive data:
- store state in a remote backend
- enable encryption at rest
- restrict access tightly
- version and lock state appropriately
- avoid sharing state broadly in your org
For example:
- AWS S3 backend with SSE-KMS + DynamoDB lock table
- Terraform Cloud/Enterprise with workspace permissions
8) Rotate and audit
Have a process for:
- rotating secrets regularly
- revoking compromised credentials
- auditing access logs in GitHub, cloud provider, and secret manager
- scanning repos for accidentally committed secrets
Use tools like:
- GitHub secret scanning
- trufflehog
- gitleaks
Recommended startup-friendly setup
If you want a pragmatic default:
- GitHub Actions with OIDC
- Terraform remote state in encrypted storage
- Cloud secret manager for app secrets
- Containers pull secrets at runtime
- No secrets in Docker images or Terraform variables
- Use GitHub Secrets only where OIDC can’t work
Simple mental model
- GitHub Actions: authenticate to cloud using OIDC
- Terraform: provision infra, policies, secret containers
- Secret manager: store actual secret values
- Containers: retrieve secrets at runtime
If you want, I can also give you:
- a reference architecture diagram
- a GitHub Actions + Terraform + AWS example
- or a recommended secret handling pattern for Kubernetes vs ECS vs Cloud Run