如何解决错误加载状态:AccessDenied:Access Denied 状态代码:403 尝试将 s3 用于 terraform 后端时?

hel*_*per 9 amazon-web-services terraform terraform-provider-aws

我的简单 terraform 文件是:

provider "aws" {
  region = "region"
  access_key = "key" 
  secret_key = "secret_key"
}

terraform {
  backend "s3" {
    # Replace this with your bucket name!
    bucket         = "great-name-terraform-state-2"
    key            = "global/s3/terraform.tfstate"
    region         = "eu-central-1"
    # Replace this with your DynamoDB table name!
    dynamodb_table = "great-name-locks-2"
    encrypt        = true
  }
}

resource "aws_s3_bucket" "terraform_state" {
  bucket = "great-name-terraform-state-2"
  # Enable versioning so we can see the full revision history of our
  # state files
  versioning {
    enabled = true
  }
  server_side_encryption_configuration {
    rule {
      apply_server_side_encryption_by_default {
        sse_algorithm = "AES256"
      }
    }
  }
}

resource "aws_dynamodb_table" "terraform_locks" {
  name         = "great-name-locks-2"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "LockID"
  attribute {
    name = "LockID"
    type = "S"
    }
}
Run Code Online (Sandbox Code Playgroud)

我想要做的就是将我的后端从本地替换为存储在 S3 中。我正在做以下事情:

  1. terraform init (当 terrafrom{} 块是评论时)

  2. terrafrom apply - 我可以在我的 AWS 中看到存储桶已创建,以及 Dynmpo 表。

  3. 现在我不再评论 terrafrom 块,terraform init并且我收到以下错误:

Error loading state:
    AccessDenied: Access Denied
        status code: 403, request id: xxx, host id: xxxx
Run Code Online (Sandbox Code Playgroud)

我的 IAM 具有 我正在使用的管理访问权限Terraform v0.12.24 ,我直接在文件中写入我的 AWS 密钥和秘密

我究竟做错了什么?

我感谢任何帮助!

小智 10

我以前遇到过这个。以下是帮助您克服该错误的步骤-

  1. 删除 .terraform 目录
  2. 将 access_key 和 secret_key 放在后端块下。像下面给出的代码
  3. 运行 terraform init
  backend "s3" {
    bucket = "great-name-terraform-state-2"
    key    = "global/s3/terraform.tfstate"
    region = "eu-central-1"
    access_key = "<access-key>"
    secret_key = "<secret-key>"
  }
}
Run Code Online (Sandbox Code Playgroud)

错误应该消失了。

  • 您还可以设置 AWS 配置文件名称而不是访问密钥和密钥。 (2认同)
  • 最佳实践不建议您在 Terraform 文件中存储敏感材料,例如访问权限和密钥。如果您还使用像 Github 这样的代码存储库,则尤其如此。正如 @Juancho 指出的,您需要做的就是在后端添加一行,如下所示: `profile = your_profile_name_from_the_aws_credentials_file` 另外,删除 `.terraform` 目录是完全没有必要的。 (2认同)

Bla*_*gle 10

terraform init我知道,通过在为其 Terraform 后端共享相同 S3 存储桶的其他项目上运行,我的凭据没有问题。

对我有用的:

rm -rf .terraform/
Run Code Online (Sandbox Code Playgroud)

编辑

terraform init请务必在删除本地目录后再次运行.terraform,以确保安装了所需的软件包。


小智 5

我也面临同样的问题。然后我手动从本地系统中删除状态文件。您可以在.terraform/目录下找到terraform.tfstate文件并再次运行init。如果您在 aws cli 中配置了多个配置文件。在 aws 提供程序配置下不提及配置文件将使 terraform 使用默认配置文件。


Mek*_*ata 5

为了更好的安全性,您可以像这样使用shared_credentials_fileprofile ;

provider "aws" {
  region = "region"
  shared_credentials_file = "$HOME/.aws/credentials # default
  profile = "default" # you may change to desired profile
}

terraform {
  backend "s3" {
    profile = "default" # change to desired profile
    # Replace this with your bucket name!
    bucket         = "great-name-terraform-state-2"
    key            = "global/s3/terraform.tfstate"
    region         = "eu-central-1"
    # Replace this with your DynamoDB table name!
    dynamodb_table = "great-name-locks-2"
    encrypt        = true
  }
}
Run Code Online (Sandbox Code Playgroud)