如何将Terraform状态上传到s3存储桶?

时间:2020-04-24 10:49:35

标签: terraform

我有一个项目,其基础设施由terraform管理。我想将状态推向s3存储桶,以便其他团队可以使用该状态。下面是后端配置:

terraform {
  backend "s3" {
    bucket = "MY_BUCKET"
    key    = "tfstate"
    region  = "ap-southeast-2"
  }
}

运行terraform init时出现以下错误:

    AccessDenied: Access Denied
        status code: 403, request id: 107E6007C9C64805, host id: kWASxeq1msxvGPZIKdi+7htg3yncMFxW9PQuXdC8ouwsEHMhx8ZPu6dKGUGWzDtblC6WRg1P1ew=

Terraform failed to load the default state from the "s3" backend.
State migration cannot occur unless the state can be loaded. Backend
modification and state migration has been aborted. The state in both the
source and the destination remain unmodified. Please resolve the
above error and try again.

似乎terraform试图从s3存储桶加载状态,而不是推送到s3存储桶。如何配置terraform将状态推送到s3?

我已在tf文件上配置了AWS配置文件:

provider "aws" {
  region  = "ap-southeast-2"
  profile = "me"
}

当前用户的凭据对存储桶具有管理员权限。

2 个答案:

答案 0 :(得分:0)

您没有在后端块中提供 S3 凭据。您需要在那里设置它们(access_keysecret_key 参数)或通过环境变量(AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY)。

您还需要确保存储桶存在并且这些凭据确实可以访问它。

顺便说一下,您不需要 AWS 提供程序块。即使您不在 Terraform 配置中管理 AWS 资源,S3 后端也可以使用。

答案 1 :(得分:0)

我遇到了同样的问题,并且在我的 aws 控制台中没有创建 backend.tf 文件中提到的存储桶。因此,我使用 backend.tf 文件中提到的相同存储桶名称创建了存储桶,并且它对我有用。