Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make targets to configure a layer should not return errors if they have already been run #712

Open
rocketnova opened this issue Jul 27, 2024 · 2 comments
Labels
scope: scripts Shell scripts and makefiles type: devex Developer experience type: tech debt Improvements to code quality that don't necessarily have noticeable functional or behavior changes workflow: needs refinement Further information is requested

Comments

@rocketnova
Copy link
Contributor

Issue

All of the following make targets call /bin/create-tfbackend:

  • infra-configure-network
  • infra-configure-app-build-repository
  • infra-configure-app-database
  • infra-configure-app-service

If you run any of these commands again accidentally after the layer has already been configured, you get a confusing terraform message without clear instructions about what's wrong.

For example, if you have already previously run make infra-configure-app-build-repository and you run it again, you will receive the following error:

./bin/create-tfbackend "infra/app-rails/build-repository" shared
╷
│ Error: Backend initialization required, please run "terraform init"
│ 
│ Reason: Initial configuration of the requested backend "s3"
│ 
│ The "backend" is the interface that Terraform uses to store state,
│ perform operations, etc. If this message is showing up, it means that the
│ Terraform configuration you're using is using a custom configuration for
│ the Terraform backend.

│ Changes to backend configurations require reinitialization. This allows
│ Terraform to set up the new configuration, copy existing state, etc. Please run
│ "terraform init" with either the "-reconfigure" or "-migrate-state" flags to
│ use the current configuration.

│ If the change reason above is incorrect, please verify your configuration
│ hasn't changed and try again. At this point, no changes to your existing
│ configuration or state have been made.
╵
make: *** [infra-configure-app-build-repository] Error 1

What's actually happening is that this line is causing an error:

TF_STATE_BUCKET_NAME=$(terraform -chdir=infra/accounts output -raw tf_state_bucket_name)

The message is confusing and doesn't provide helpful advice to the user. Running terraform init in the root dir or in the build-repository dir, and then trying to re-run make infra-configure-app-build-repository is incorrect, even if it seems intuitive based on the error message.

The correct next step for the user is actually to move on to run make infra-update-app-build-repository OR to run something like terraform -chdir=infra/accounts init -backend-config=$(./bin/current-account-config-name).s3.tfbackend to re-initialize the account before re-running make infra-configure-app-build-repository.

Resolution Ideas

Perhaps we can:

  1. add a check in /bin/create-tfbackend to check to check to see if the account needs to be re-initialized
  2. automatically re-initialize
@rocketnova rocketnova added workflow: needs refinement Further information is requested type: tech debt Improvements to code quality that don't necessarily have noticeable functional or behavior changes type: devex Developer experience scope: scripts Shell scripts and makefiles labels Jul 27, 2024
@rocketnova
Copy link
Contributor Author

@lorenyu @doshitan Thoughts on approach?

@lorenyu
Copy link
Contributor

lorenyu commented Jul 29, 2024

If we come up with a solution to this sentence from this ticket:

it would be nice if the script checked .terraform/terraform.tfstate | jq .backend.config to see if it is already the correct backend config and skip initialization if so to speed up terraform commands.

then that might work here too

unfortunately I'm not sure if the jq .backend.config solution I proposed works. may need to dig into it more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: scripts Shell scripts and makefiles type: devex Developer experience type: tech debt Improvements to code quality that don't necessarily have noticeable functional or behavior changes workflow: needs refinement Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants