gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
7.91k stars 966 forks source link

Backend initialization error when setting disable_init = true #2880

Open denis256 opened 8 months ago

denis256 commented 8 months ago

Describe the bug Noticed that apply of changes when is specified disable_init = true end in Reason: Initial configuration of the requested backend "s3" error:

$ terragrunt run-all apply --terragrunt-non-interactive
DEBU[0000] Generated file /projects/gruntwork/terragrunt-tests/s3-reconfigure/app/backend.tf.  prefix=[/projects/gruntwork/terragrunt-tests/s3-reconfigure/app] 
DEBU[0000] Running command: terraform init -backend=false  prefix=[/projects/gruntwork/terragrunt-tests/s3-reconfigure/app] 

Initializing provider plugins...

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
DEBU[0000] Running command: terraform apply -auto-approve -input=false  prefix=[/projects/gruntwork/terragrunt-tests/s3-reconfigure/app] 
╷
│ 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.
╵
ERRO[0000] terraform invocation failed in /projects/gruntwork/terragrunt-tests/s3-reconfigure/app  prefix=[/projects/gruntwork/terragrunt-tests/s3-reconfigure/app] 
ERRO[0000] Module /projects/gruntwork/terragrunt-tests/s3-reconfigure/app has finished with an error: 1 error occurred:
    * [/projects/gruntwork/terragrunt-tests/s3-reconfigure/app] exit status 1
  prefix=[/projects/gruntwork/terragrunt-tests/s3-reconfigure/app] 
DEBU[0000] 1 error occurred:
    * [/projects/gruntwork/terragrunt-tests/s3-reconfigure/app] exit status 1

ERRO[0000] 1 error occurred:
    * [/projects/gruntwork/terragrunt-tests/s3-reconfigure/app] exit status 1

To Reproduce

  1. Create S3 bucket
  2. Specify disable_init in remote_state configuration
remote_state {
  backend = "s3"
  disable_init = true
  generate = {
    path      = "backend.tf"
    if_exists = "overwrite"
  }
  config = {
    bucket         = "xxx-yyy"
    key            = "${path_relative_to_include()}/terraform.tfstate"
    region         = "us-east-2"
    encrypt        = true
  }
}

Expected behavior Terragrunt use existing S3 bucket and don't crash

Nice to have

Versions

Additional context N/A

dhoppe commented 6 months ago

I have the same problem running terragrunt run-all validate:

remote_state {
  backend = "s3"

  disable_init = tobool(get_env("TERRAGRUNT_DISABLE_INIT", "false"))

  generate = {
    path      = "backend.tf"
    if_exists = "overwrite_terragrunt"
  }

  config = {
    bucket         = "terraform-states-${local.aws_account_id}"
    key            = "${path_relative_to_include()}/terraform.tfstate"
    region         = "eu-west-1"
    encrypt        = true
    dynamodb_table = "terraform-locks-${local.aws_account_id}"
  }
}
github-actions[bot] commented 4 days ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for raising this issue.