fix: auto-generate unique S3 state bucket name using AWS account ID#2
Closed
cstirry wants to merge 1 commit into
Closed
fix: auto-generate unique S3 state bucket name using AWS account ID#2cstirry wants to merge 1 commit into
cstirry wants to merge 1 commit into
Conversation
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
When a new deployer runs
uv run opencontext authenticate, all checks pass cleanly — Python, uv, AWS CLI, credentials, and Terraform. But then when they runuv run opencontext configureand complete the CLI wizard, it fails at the end with no useful explanation:The root cause is that the S3 bucket name for Terraform state was hardcoded to
opencontext-terraform-statein multiple places. S3 bucket names are globally unique across all AWS accounts — this name is already owned by another account, so any new deployer hits a 403 with no actionable error message.There were three separate issues discovered while fixing this:
Fixes
1. Auto-generate a unique S3 bucket name using the AWS account ID
Instead of a hardcoded bucket name, the CLI now calls STS to get the deployer's AWS account ID and generates a name in the format
opencontext-terraform-state-<account-id>(e.g.opencontext-terraform-state-716692883178). This is guaranteed unique per AWS account with no extra user input required. Users can still override it with--state-bucketif needed.Files changed:
cli/commands/configure.py,terraform/bootstrap/variables.tf,terraform/bootstrap/main.tf2. Handle 403 with a clear error message instead of crashing
Previously, if a 403 was encountered (bucket owned by another account), the raw AWS
ClientErrorbubbled up to Rich'sconsole.printwhich tried to parse the AWS error text as Rich markup. The error message contains[/:]which caused a secondaryMarkupErrorcrash, completely obscuring the real problem. The 403 is now caught explicitly and prints a clear, actionable message:File changed:
cli/commands/configure.py3. Always run
terraform init -reconfigureinstead of skipping if.terraformexistsThe original code skipped
terraform initif a.terraformdirectory already existed. This caused a second failure on re-runs after the bucket name changed — Terraform detected a backend config change and refused to proceed without reinitialization. Removing the existence check and adding-reconfigureensures the backend config is always in sync.File changed:
cli/commands/configure.pyHow to reproduce the original bug
uv run opencontext authenticate— all checks passuv run opencontext configureand complete the wizardHow to verify the fix
uv run opencontext configureand complete the wizardopencontext-terraform-state-<your-account-id>