【问题标题】:Terraform error refreshing state access deniedTerraform 错误刷新状态访问被拒绝
【发布时间】:2018-09-23 18:33:04
【问题描述】:

我将 gitbucket 用于我的存储库和管道。 我有一个配置了远程状态的 terraform 配置文件,它在我的本地机器上运行良好,但是在 gitbucket 中运行时失败。我不断收到拒绝访问错误。 这是 main.tf:

terraform {
backend "s3" {
    bucket = "zego-terraform-test"
    key    = "test/terraform.tfstate"
    region = "eu-west-1"
  }
}

data "terraform_remote_state" "remote_state" {
  backend = "s3"

  config {
    bucket = "zego-terraform-test"
    key    = "test/terraform.tfstate"
    region = "eu-west-1"
  }
}

variable "region" {}

provider "aws" {
  region     = "${var.region}"
  access_key = {}
  secret_key = {}
  token      = {}
}

module "vpc" {
  source = "./modules/vpc"
}

这是我的 gitbucket-pipelines.yml:

image: python:3.5.1
pipelines:
  default:
    - step:
        caches:
          - pip
        script: # Modify the commands below to build your repository.
          - apt-get update
          - apt-get install unzip
          - wget https://releases.hashicorp.com/terraform/0.11.7/terraform_0.11.7_linux_amd64.zip
          - unzip terraform_0.11.7_linux_amd64.zip
          - rm terraform_0.11.7_linux_amd64.zip
          - export PATH="$PATH:${BITBUCKET_CLONE_DIR}"
          - terraform init
            -backend-config "access_key=$AWS_ACCESS_KEY"
            -backend-config "secret_key=$AWS_SECRET_KEY"
            -backend-config "token=$TOKEN"

当我在此管道中运行 .tf 文件时,我收到此错误:

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.
Error refreshing state: AccessDenied: Access Denied
    status code: 403

当我删除远程状态配置时,它运行良好。 为什么即使我在本地计算机和 gitbucket 环境中使用相同的凭据,我也会收到拒绝访问错误?

【问题讨论】:

  • 您确定您的凭据被正确读取了吗?如果您让管道从 AWS CLI 运行 aws sts get-caller-identity 命令会发生什么?
  • 对我来说,正确设置 AWS_PROFILE 解决了这个问题。

标签: amazon-s3 terraform bitbucket-pipelines gitbucket


【解决方案1】:

乍一看似乎是合理的。您是否尝试过将 terraform init 和 -backend-config 全部放在一条线上?不知道开头的-是不是弄乱了yml格式?

【讨论】:

    【解决方案2】:

    遇到同样的错误。对于我们的用例,我们必须手动删除.terraform/ 目录下的terraform.tfstate 文件并再次运行init

    【讨论】:

    • 我正在使用 Windows。我不小心删除了 terraform.tfstate 文件中的内容。之后,当我尝试删除 terraform.tfstate 文件时。它会自动重新创建。我在云中手动删除了资源。现在我无法在本地 Windows 中删除这个 .tfstate 文件。
    【解决方案3】:

    如果尚未找到此问题的解决方案,您可以在 terraform_remote_state 节的配置部分中使用“profile=”或“role_arn=”。 AWS Provider 和后端配置也是如此。

    我今天整天都在追这个问题,没有意识到 role_arn 可用于 terraform_backend_state 数据源。

    【讨论】:

      猜你喜欢
      • 2012-04-15
      • 2020-05-19
      • 2021-01-31
      • 1970-01-01
      • 2015-12-03
      • 2022-08-10
      • 2021-12-13
      • 1970-01-01
      • 2020-11-11
      相关资源
      最近更新 更多