Repository navigation
fix(postgres-migration): expose the instance id and region as outputs, make the seed re-runnable - #98
Merged
sishuo-yang merged 1 commit intoAug 19, 2026
Conversation
…puts The playbook recovered the generated DB instance identifier by filtering `aws rds describe-db-instances` on the endpoint address. That lookup has no error path: `| [0]` over an empty match is JSON null, which `--output text` renders as the literal string `None`, so `reboot-db-instance` then failed with `DBInstance not found: none` and pointed at RDS rather than at the lookup. Three unrelated conditions collapsed to that same `None` -- an empty RDS_HOST, an endpoint that does not match, and a region containing no instances. The last is the common one, because Terraform reads its region from terraform.tfvars and the AWS CLI reads its own from AWS_REGION, AWS_DEFAULT_REGION or the profile, and inherits nothing from Terraform. Aimed at the wrong region the CLI does not error; it reports an account with no such instance in it. `identifier_prefix` means the identifier cannot be written down in advance, but it is still a known attribute of the resource, so an output is exact and needs no API call. Add `rds_instance_id` and `region`, which lets the playbook export both from state and removes the failure class rather than guarding it. Also correct two file-header examples that invoked psql as `-h "$PGHOST" -U postgres`. Neither is right for this lab: PGHOST is never exported, and the master user is shopadmin, so a reader who pasted either got a connection to localhost or a role that does not exist. Co-Authored-By: Claude Opus 5 (1M context) <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.
Two lab-code fixes for the Postgres migration workshop, both found by running module 01 end to end on a live RDS stack. The playbook half ships in WorkshopHouse#60.
1. The RDS instance id and region as outputs
The playbook recovered the generated DB instance identifier by filtering
describe-db-instanceson the endpoint address:A learner hit this and got:
| [0]over an empty match is JSONnull, which--output textrenders as the literal stringNone.export X="$(...)"swallows the exit code, so the next two commands ran against garbage and the error named RDS instead of the lookup.Three unrelated conditions collapse to that same
None, confirmed by evaluating the expression directly:RDS_HOSTmatches an instanceRDS_HOSTemptyNoneRDS_HOSTcarries the:5432suffixNoneNoneThe last is the one that fires in practice. Terraform takes its region from
regioninterraform.tfvars; the AWS CLI takes its own fromAWS_REGION,AWS_DEFAULT_REGIONor the profile, and inherits nothing from Terraform. Aimed at the wrong region the CLI does not error -- it reports an account with no such instance in it. The reporting learner had a profile onus-east-1against a stack inus-east-2.identifier_prefixmeans the identifier cannot be written down in advance, but it is still a known attribute of the resource. The playbook's premise -- that generated therefore means unavailable -- was the actual defect. Two new outputs make it exact, with no API call and nothing to mismatch:rds_instance_id--aws_db_instance.shop.identifier, forreboot-db-instanceand module 03's CloudWatch dimensionregion--var.region, so the playbook can exportAWS_REGIONfrom the applied stack and every rawawscall in modules 01, 03 and 06 lands in the right region2. The seed is re-runnable
A seed that died partway could not be restarted: the second run re-inserted products, hit
sku's unique constraint and aborted, so recovering meant manual cleanup before the file would run at all -- and nothing in the file hinted at that. This is not hypothetical; it is what a killed seed leaves you with.Each
INSERTnow carries a count guard against its own target. Each table is loaded by one statement, so a table is either empty of seed rows or complete and there is no partially-seeded state for the guard to misjudge. Theordersandorder_itemsguards compare against the target rather than testing for emptiness, becauseapp/writer.pyinserts into both and would otherwise make them look finished while the seed rows are still missing.Verified in four states:
INSERT 0 0x4order_itemslost, rest intactorder_itemsAgainst the live already-seeded RDS instance, a re-run reports four
INSERT 0 0and takes 20 seconds.3. Two file-header examples
sql/01_schema.sqlandsql/02_seed.sqlboth showedpsql -h "$PGHOST" -U postgres -d shop. Neither is right for this lab:PGHOSTis never exported, and the master user isshopadmin, so a reader who pasted either got a connection to localhost or a role that does not exist.Note for reviewers
The outputs need a
terraform applyto take effect. Outputs are only written to state byapply, so an existing stack reportsOutput "region" not founduntil it runs. It is a no-op for infrastructure --0 added, 0 changed, 0 destroyed-- but it is not optional.Verification
terraform fmt -checkandterraform validatepassterraform applyagainst a live stack from this lab:No changes,0 added, 0 changed, 0 destroyed, both new outputs present.gitignore,README.mdand the privatescripts/manifest.sh🤖 Generated with Claude Code