【问题标题】:AWS Terraform IAM user with assume role for S3 back end担任 S3 后端角色的 AWS Terraform IAM 用户
【发布时间】:2022-02-09 22:06:44
【问题描述】:

为了将 Terraform 状态文件存储在 S3 存储桶中,我使用了管理员帐户凭据并进行了配置。现在我已经创建了 IAM 用户并尝试使用承担角色。假设角色附加了管理员策略。

我已经用aws sts assume-role --role-arn 命令测试了这个AIM 用户设置。我能够使用aws s3 ls 命令获取令牌并使用能够访问 S3 存储桶的令牌。

现在尝试将 IAM 用户访问/密钥与 terraform 的承担角色一起使用。当我运行terraform refresh 命令时,它会给出以下错误。

供应商代码:

provider "aws" {
  region  = "us-west-2"
  max_retries = 1
  assume_role {
    role_arn = var.assume_role_arn
  }

}

错误

 Error: Error acquiring the state lock
│
│ Error message: 2 errors occurred:
│       * AccessDeniedException: User: arn:aws:iam::xxxxxxxx:user/deploy is not authorized to perform: dynamodb:PutItem on resource:
│ arn:aws:dynamodb:us-west-2:xxxxxxxx:table/terraform-state-lock
│       status code: 400, request id: CV4J3JN1LD0KPDGHAOA9UL13ONVV4KQNSO5AEMVJF66Q9ASUAAJG
│       * AccessDeniedException: User: arn:aws:iam::xxxxxxxx:user/deploy is not authorized to perform: dynamodb:GetItem on resource:
│ arn:aws:dynamodb:us-west-2:xxxxxxxx:table/terraform-state-lock
│       status code: 400, request id: BISQQDU4659LLI3CEDP26T8RJ7VV4KQNSO5AEMVJF66Q9ASUAAJG
│
│
│
│ Terraform acquires a state lock to protect the state from being written
│ by multiple users at the same time. Please resolve the issue above and try
│ again. For most commands, you can disable locking with the "-lock=false"
│ flag, but this is not recommended.

为什么这在 Terraform 中不起作用如何修复此错误?

谢谢 SR

【问题讨论】:

  • 错误提示您没有使用dynamodb的权限。
  • @Marcin 我已将Administrator 策略附加到该角色,它应该可以访问 dynamodb 和 s3。另外,当我运行terraform refresh -lock=false 命令时收到此消息Error: error loading state: AccessDenied: Access Denied
  • 但错误说明它是关于用户,而不是角色:user/deploy
  • 我可以运行aws sts assume-role --role-arn 命令并导出AWS_SESSION_TOKEN 和访问/密钥terraform refresh 命令有效。这意味着我认为用户设置是正确的。只有使用 terraform 才能获得会话令牌。
  • @sfgroups 您在本地环境中使用它。它使用您的密钥而不是角色。顺便说一句,您可以在后端块中添加配置文件。

标签: amazon-s3 terraform amazon-iam terraform-provider-aws assume-role


【解决方案1】:

它似乎甚至没有承担这个角色,否则错误将显示为assumed-role/deploy identity

我建议您尝试在命令行中导出 AWS_SECRET_ACCESS_KEYAWS_ACCESS_KEY_ID 变量,然后再次运行 Terraform。

这几天我尝试使用 Terraform Cloud 和那里的 terraform 用户凭据,这不完全是你的情况,但它应该以相同的方式运行。

【讨论】:

    猜你喜欢
    • 2018-01-15
    • 2019-09-10
    • 2020-04-29
    • 2018-03-06
    • 2018-11-10
    • 2020-11-08
    • 2022-07-28
    • 1970-01-01
    • 2021-08-02
    相关资源
    最近更新 更多