terraform apply 在 terraform init 和 terraform plan 成功后报告 S3 后端配置错误

4

我正在Linux实例上运行terraform,但是遇到以下错误信息。

+ /usr/local/bin/terraform workspace new test
enter code here[0m[0m[1m[33mBackend reinitialization required. Please run "terraform init".[0m

[33mReason: 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 setup the new configuration, copy existing state, etc. This is
only done during "terraform init". Please run that command now then try again.

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.
[0m
[31mFailed to load backend: Initialization required. Please see the error message above.

这是 Terraform 配置文件。
provider "aws" {
  # don't touch below here
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
  region     = "us-west-2"
}

# Configure Terraform to store this in S3
terraform {
  backend "s3" {
    bucket = "nom-terraform"
    key    = "apps/onboarding/terraform.tfstate"
    region = "us-west-2"
  }
}

在运行terraform apply之前,我成功地运行了terraform plan。

尝试删除“.terraform”文件夹并重新运行terraform init? - keety
@keety 我正在Linux环境下运行它,你能告诉我确切的位置在哪里找到那个 .terraform 文件夹吗? - Cool Eagle
它在你当前工作目录中的任何文件夹里。 - Brandon Miller
3个回答

7

看起来你已经添加了新的S3作为后端。因此,terraform需要重新初始化。 只需运行terraform init即可将S3添加为后端,并请求许可将本地状态文件转移至S3。


2
我尝试过了,但还是一样。terraform init可以工作,但apply仍然会抛出相同的错误。:( - Ankur Bhatia
同样的问题在这里。 - Pedro Bezanilla
请问您能否分享@PedroBezanilla的错误信息? - deepak
@deepak "需要重新初始化后端。请运行“terraform init”。 原因:请求的后端“s3”的初始配置自我使用后端配置以来一直显示为:terraform init \ -backend-config=backend.tfvars - Pedro Bezanilla

1

我会先删除任何状态文件和 .terraform 文件夹,因为有时错误是由于损坏造成的。

然后我会运行 init 命令并且应该可以成功运行。

我不认为添加后端是问题所在,因为它应该尝试在状态之间进行合并。


0
您需要先初始化提供程序,然后使用最新版本下载提供程序,输出会类似于以下内容:
# terraform init

Initializing provider plugins...
- Checking for available provider plugins on https://releases.hashicorp.com...
- Downloading plugin for provider "spotinst" (1.1.1)...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.aws: version = "~> 1.2"
* provider.cloudflare: version = "~> 0.1"
* provider.spotinst: version = "~> 1.1"

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.`

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接