【问题标题】:how to store tfstate remotely Terraform backend by s3 bucket如何通过 s3 存储桶远程存储 tfstate Terraform 后端
【发布时间】:2020-02-05 10:09:10
【问题描述】:

我的可用资源是

操作系统 - ubuntu 18.04 | Terraform cli - Terraform v0.12.9 |用户 - root 登录

我的问题是 - 我希望 tfstate 通过 s3 远程存储后端,所以我手动创建了一个

s3 存储桶 - selllist-infra

为此我创建了 backend.tf 文件和我的 terraform 脚本在下面,当我 terraform init 下面的错误时。 脚本有什么问题?

provider "aws" {
    acces_key  = "**************"
    secret_key = "**************"
}

terraform {
    backend "s3" {
        bucket                      = "sellist-infra"
        key                         = "terraform/sellist/do/prod/terraform.tfstate
        endpoint                    = "nyc3.digitaloceanspaces.com"
        region                      = "us-east-1"
        profile                     = "sellist-do"
        skip_credentials_validation = true
        skip_get_ec2_platforms      = true
        skip_requesting_account_id  = true
        skip_metadata_api_check     = true
    }
} 


***OUTPUT*** 

Initializing the backend...

Warning: "skip_requesting_account_id": [DEPRECATED] The S3 Backend no longer automatically looks up the AWS Account ID and this attribute is no longer used.



Warning: "skip_get_ec2_platforms": [DEPRECATED] The S3 Backend does not require EC2 functionality and this attribute is no longer used.



Error: Failed to get existing workspaces: NoCredentialProviders: no valid providers in chain. Deprecated.
        For verbose messaging see aws.Config.CredentialsChainVerboseErrors

【问题讨论】:

  • 在我看来,将凭据存储在文件本身中并不是一个好主意。我将它们存储在未提交或上传的 tfvars 文件中。

标签: amazon-web-services amazon-s3 terraform terraform-provider-aws minio


【解决方案1】:

您收到的通知是因为skip_requesting_account_idskip_get_ec2_platforms 这两个变量已被弃用,请参阅docs

对于凭证错误,将凭证移动到 ~/.aws/credentials 并保留在文件中:

provider "aws" {}

这在安全性方面也是一种更好的做法,因为您不会将凭据与代码一起存储。在您的原始代码中,您还有一个错字,是 access_key 而不是 acces_key

【讨论】:

    【解决方案2】:

    S3 状态存储后端和 AWS 提供商是两个独立的组件,需要单独配置。

    通常我们避免使用标准 AWS environment variablescredentials file 直接配置它们,AWS 提供商和 S3 后端都会自动读取和使用它们。

    对于需要为后端和提供商使用单独凭证的更复杂的场景,Terraform 文档部分Multi-account AWS Architecture 提供了一个参考架构,允许在让 AWS 提供商假设的同时仍然使用标准 AWS 凭证机制获取特定 AWS 账户所需访问权限的特殊角色。

    AWS 提供商和 S3 后端上的 access_keysecret_key 配置参数适用于不适合使用标准 AWS 凭证机制的非常不寻常的情况。应该很少使用这些,但是如果您确实选择使用它们,那么您需要在provider "aws" 块和backend "s3" 块中分别设置它们。我建议将这些论点视为最后的手段,并且仅在您尝试了所有the other options 并得出结论认为由于某种原因它们不适合您的特殊情况时才使用它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-30
      • 2021-09-01
      • 2018-05-29
      • 1970-01-01
      • 2019-02-22
      • 2021-06-08
      • 2019-05-02
      • 2019-08-09
      相关资源
      最近更新 更多