fix(infra): stop Terraform clobbering app runtime secret on every apply#269
Open
arielr-lt wants to merge 1 commit into
Open
fix(infra): stop Terraform clobbering app runtime secret on every apply#269arielr-lt wants to merge 1 commit into
arielr-lt wants to merge 1 commit into
Conversation
The app runtime secret (ctdl-xtra/<env>/app) is managed out-of-band in
Secrets Manager via the "Update app env vars" workflow, but
aws_secretsmanager_secret_version.app rewrote the whole blob from
local.app_secret_values on every apply. That regenerated
random_password.encryption_key, changing ENCRYPTION_KEY and orphaning all
data already encrypted under the previous key (caused the prod DB-secret
decryption failures).
Add lifecycle { ignore_changes = [secret_string] } to all three envs so
Terraform seeds the secret on first creation only and never overwrites
runtime values afterward.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The app runtime secret (
ctdl-xtra/<env>/app) is managed out-of-band in AWS Secrets Manager — by theUpdate app env varsworkflow and by manual ops — butaws_secretsmanager_secret_version.apprewrote the entire JSON blob fromlocal.app_secret_valueson everyterraform apply.Because
ENCRYPTION_KEYis sourced fromrandom_password.encryption_key, any apply that recreated/refreshed that resource silently rotated the key. The app uses it for AES-256-CBC, so all DB rows encrypted under the previous key became undecryptable.This is exactly what bit production: the live key had drifted to a random value and encrypted DB secrets could no longer be decrypted (restored manually by setting the correct
ENCRYPTION_KEYback into Secrets Manager + rolling api/worker).Fix
Add
lifecycle { ignore_changes = [secret_string] }toaws_secretsmanager_secret_version.appin test, sandbox, and production. Terraform now seeds the secret on first creation only and never overwrites runtime values afterward — Secrets Manager + theUpdate app env varsworkflow are the single source of truth.Trade-off / follow-up
secret_stringis now fully ignored, so infra-derived values (DATABASE_URL,REDIS_URL) are no longer auto-propagated by Terraform. If the RDS endpoint ordb_password/redis_passwordchange, the secret must be updated out-of-band (via the workflow) to match. This matches the existing operational model where that workflow already owns runtime env. Worth a short runbook note.Verification
terraform fmtclean on changed files.terraform planper env and confirm the only secret-related change is the in-placelifecycleupdate (nosecret_stringreplacement).🤖 Generated with Claude Code