【问题标题】:Can't use S3 backend with Terraform - missing credentials无法将 S3 后端与 Terraform 一起使用 - 缺少凭据
【发布时间】:2019-01-21 16:16:42
【问题描述】:

我有一个 Terraform 样本中最普通的:

#  Configure AWS provider
provider "aws" {
    region     = "us-east-1"
    access_key = "xxxxxxxxx"
    secret_key = "yyyyyyyyyyy"
}

#  Terraform configuration
terraform {
  backend "s3" {
    bucket = "terraform.example.com"
    key    = "85/182/terraform.tfstate"
    region = "us-east-1"
  }
}

当我运行 terraform init 时,我收到以下(跟踪的)响应:

2018/08/14 14:19:13 [INFO] Terraform version: 0.11.7  41e50bd32a8825a84535e353c3674af8ce799161
2018/08/14 14:19:13 [INFO] Go runtime version: go1.10.1
2018/08/14 14:19:13 [INFO] CLI args: []string{"C:\\cygwin64\\usr\\local\\bin\\terraform.exe", "init"}
2018/08/14 14:19:13 [DEBUG] Attempting to open CLI config file: C:\Users\judall\AppData\Roaming\terraform.rc
2018/08/14 14:19:13 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2018/08/14 14:19:13 [INFO] CLI command args: []string{"init"}
2018/08/14 14:19:13 [DEBUG] command: loading backend config file: C:\cygwin64\home\judall\t2

2018/08/14 14:19:13 [DEBUG] command: no data state file found for backend config
Initializing the backend...
2018/08/14 14:19:13 [DEBUG] New state was assigned lineage "5113646b-318f-9612-5057-bc4803292c3a"
2018/08/14 14:19:13 [INFO] Building AWS region structure
2018/08/14 14:19:13 [INFO] Building AWS auth structure
2018/08/14 14:19:13 [INFO] Setting AWS metadata API timeout to 100ms
2018/08/14 14:19:13 [INFO] Ignoring AWS metadata API endpoint at default location as it doesn't return any instance-id

2018/08/14 14:19:13 [DEBUG] plugin: waiting for all plugin processes to complete...
Error configuring the backend "s3": No valid credential sources found for AWS Provider.
  Please see https://terraform.io/docs/providers/aws/index.html for more information on
  providing credentials for the AWS Provider

Please update the configuration in your Terraform files to fix this error
then run this command again.

我已经在谷歌上搜索了几个小时。我尝试使用“profile”属性——它产生的跟踪日志略有不同,但最终结果相同。我尝试设置 AWS_ 环境变量 - 结果相同。

我正在运行 terraform 版本 0.11.7。有什么建议吗?

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案1】:

    provider 配置独立于您的 backend 配置。

    您在 provider 块中配置的凭证用于创建您的 AWS 相关资源。要访问 S3 存储桶作为远程状态的存储,您还需要提供凭据。这可以与您的 provider 的配置中的相同,也可以完全不同(出于安全原因,仅具有此特定存储桶的权限)。

    您可以通过在 backend 块中添加凭据来修复它:

    #  Terraform configuration
    terraform {
      backend "s3" {
        bucket     = "terraform.example.com"
        key        = "85/182/terraform.tfstate"
        region     = "us-east-1"
        access_key = "xxxxxxxxx"
        secret_key = "yyyyyyyyyyy"
      }
    }
    

    或者您可以在您的主目录 (Docs) 中创建一个 AWS(默认)配置文件,并在您的 terraform 代码中删除您的凭证(首选选项,当您将配置存储在版本控制系统中时)。

    【讨论】:

    • 非常感谢!我确实有点猜到了这一点。但是,由于添加了该信息,我不得不将 -reconfigure 选项添加到我的 init 命令中。对人们来说只是一个进一步的仅供参考。再次感谢您的回复
    【解决方案2】:

    正如@JimUdall 在评论中指出的那样,如果您在更新的后端配置上重新运行init,您需要使用-reconfigure 更新配置以应用更改的配置。

    terraform init -reconfigure
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-12
      • 2022-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      • 2021-12-07
      • 2019-02-24
      相关资源
      最近更新 更多