【问题标题】:terraform_remote_state: Error: error configuring S3 Backend: no valid credential sources for S3 Backend foundterraform_remote_state:错误:配置 S3 后端时出错:找不到 S3 后端的有效凭证源
【发布时间】:2021-09-04 23:56:54
【问题描述】:

场景:我正在尝试读取存储在 AWS S3 存储桶中的远程 terraform 状态。我已经使用 aws configure cli 配置了 aws 凭证,并使用了我能够读取 AWS S3 存储桶和 tf 状态对象的凭证。

terraform 初始化工作正常。但是当我运行 terraform plan 时,出现以下错误:

Error: error configuring S3 Backend: no valid credential sources for S3 Backend found.
│
│ Please see https://www.terraform.io/docs/language/settings/backends/s3.html
│ for more information about providing credentials.
│
│ Error: NoCredentialProviders: no valid providers in chain. Deprecated.
│       For verbose messaging see aws.Config.CredentialsChainVerboseErrors

provider "aws" {
  region = "ap-southeast-1"
  profile = "dev-token"
  shared_credentials_file = "~/.aws/credentials"
}

data "terraform_remote_state" "ekscluster_state" {
  backend = "s3"
  config = {
    bucket = "bucket"
    region = "ap-southeast-1"
    key = "remote.tfstate"
  }
}

data "aws_eks_cluster" "db_subnet_ids" {
  name = data.terraform_remote_state.ekscluster_state.outputs.db_subnet_ids
}

resource "aws_db_subnet_group" "aurora_subnet_group" {
  name       = "name"
  subnet_ids = data.aws_eks_cluster.db_subnet_ids

  tags = {
    Name = format("%s", "name")
  }
}

远程状态包含所有内容。注意:在 S3 上存储状态可以使用相同的凭据正常工作。

期待听到一些提示。

【问题讨论】:

  • 您是否考虑过使用aws_subnet_ids data source 而不是使用 Terraform 的远程状态?在可能的情况下使用适当的提供商特定数据源几乎总是更好的解决方案,并且会更容易工作。
  • 如果您确实想使用远程状态数据源,那么您还需要将其配置与远程状态后端的配置保持一致,因为它们是单独处理的。您尚未显示远程状态后端配置,但如果它与提供程序配置匹配,您还需要将 profile 键添加到 config 块。

标签: amazon-s3 terraform terraform-provider-aws


【解决方案1】:

根据terraform_remote_stateargument-reference docs

配置对象可以使用在等效 terraform { backend "" { ... } } 块中有效的任何参数

因此,您可能需要将配置文件指定为 terraform_remote_stateconfig 元素的一部分,例如:

data "terraform_remote_state" "ekscluster_state" {
  backend = "s3"
  config = {
    bucket = "bucket"
    region = "ap-southeast-1"
    key = "remote.tfstate"
    profile = "dev-token"
  }
}

FWIW 为避免这种情况,我通常在运行 TF 脚本时尝试设置 AWS_PROFILE 环境变量。我管理着 lot 个具有不同配置文件的不同 AWS 账户,因此我在调用 TF 时指定内联。

【讨论】:

  • 您如何使用 AWS_PROFILE ?作为环境变量?
  • @Judi - 是的,只使用环境变量 AWSPROFILE=my-profile。我经常设置 inline 以避免使用错误的可能性。例如($env:AWS_PROFILE='dev-token';terraform plan)
  • 我可能会开始一个单独的线程来获得关于这个问题的更多想法。你的方法很好。但是,看起来您正在使用 PS - 对吗?这一直是我担心我可能会忘记更改我的 env 并且我的 TF 可能在 diff env 中运行。
  • 主要使用 PS,但有时使用 bash。我不担心忘记更改太多,因为不同的配置文件通常无法访问彼此的状态文件。此外,当我运行计划时,我可以看到会发生什么变化。
猜你喜欢
  • 2021-12-23
  • 2022-08-16
  • 2019-08-22
  • 2021-09-26
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 2018-11-21
  • 2017-10-05
相关资源
最近更新 更多