配置 Terraform S3 后端时出错

MKM*_*MKM 7 amazon-s3 amazon-web-services terraform

我正在通过 terraform for AWS 配置 S3 后端。

terraform {
  backend "s3" {}
}
Run Code Online (Sandbox Code Playgroud)

在运行“terraform init”命令时提供(S3 后端)存储桶名称、键和区域的值,出现以下错误

配置后端“s3 ”时出错:未找到适用于 AWS 提供商的有效凭证源。有关为 AWS 提供商提供凭证的更多信息,请参阅https://terraform.io/docs/providers/aws/index.html请更新Terraform 文件中的配置以修复此错误,然后再次运行此命令。

我已经在 providers.tf 中将访问和秘密密钥声明为变量。在运行“terraform init”命令时,它没有提示任何访问密钥或秘密密钥。

如何解决这个问题?

cod*_*aus 16

运行时,terraform init您必须-backend-config为您的凭据(aws 密钥)添加选项。所以你的命令应该是这样的:

terraform init -backend-config="access_key=<your access key>" -backend-config="secret_key=<your secret key>"


Яро*_*вич 10

在 AWS 凭证文件中重命名配置文件时,我遇到了类似的问题。删除.terraform文件夹并terraform init再次运行即可解决问题。


Pra*_*tta 8

我也遇到了同样的问题,解决这个问题的最简单和安全的方法是配置 AWS 配置文件。即使你在你的项目中正确地提到了 AWS_PROFILE,你也必须在你的 backend.tf 中再次提到它

我的问题是,我已经在项目中设置了 AWS 提供商,如下所示并且它工作正常。

provider "aws" {
region = "${var.AWS_REGION}"
profile = "${var.AWS_PROFILE}"
}
Run Code Online (Sandbox Code Playgroud)

但在项目结束时我试图配置 S3 后端配置文件。因此我运行了该命令terraform init并且我也收到了相同的错误消息。

Error: error configuring S3 Backend: no valid credential sources for S3 Backend found.
Run Code Online (Sandbox Code Playgroud)

请注意,这对于 terraform 后端配置来说还不够。您还必须在后端文件中提及AWS_PROFILE

  • 完整解决方案

我目前正在使用 terraform 最新版本。这是 v0.13.5。

请看 provider.tf

provider "aws" {
region = "${var.AWS_REGION}"
profile = "${var.AWS_PROFILE}" # lets say profile is my-profile
}
Run Code Online (Sandbox Code Playgroud)

例如,您的 AWS_PROFILE 是my-profile 那么您backend.tf应该如下所示。

terraform {
    backend "s3" {
    bucket = "my-terraform--bucket"
    encrypt = true
    key = "state.tfstate"
    region = "ap-southeast-2"
    profile = "my-profile" # you have to give the profile name here. not the variable("${var.AWS_PROFILE}")
  }
}
Run Code Online (Sandbox Code Playgroud)

然后运行 terraform init

  • 非常感谢,这对我来说是正确的答案 (2认同)
  • 这就是正确答案,非常感谢大佬! (2认同)

dee*_*gan 6

如果您已设置自定义 aws 配置文件,请使用以下选项。

terraform init -backend-config =“profile =您的配置文件名称”

如果没有自定义配置文件,请确保将 access_key 和 Secret_key 添加到默认配置文件并尝试。