【问题标题】:update aws_iam_policy with terraform in GitLab-CI fails with EntityAlreadyExists error在 GitLab-CI 中使用 terraform 更新 aws_iam_policy 失败并出现 EntityAlreadyExists 错误
【发布时间】:2021-07-24 20:27:15
【问题描述】:

我正在尝试更新 IAM 角色,它通过 GitLab-CI 与 Terraform 附加了策略。我的 terraform 代码如下所示:-

data "aws_iam_policy_document" "billing-roles" {
  statement {
      effect = "Allow"
      principals {
          type = "Federated"
          identifiers = ["${var.samlprovider_arn}"]
      }
      actions = ["sts:AssumeRoleWithSAML"]
      condition {
        test = "StringEquals"
        variable ="SAML:aud"
        values = ["https://signin.aws.amazon.com/saml"]
       }
    }
}

resource "aws_iam_role" "billing_role" {
  name               = "billing-role"
  permissions_boundary = "${var.permissions_boundary_arn}"
  assume_role_policy = "${data.aws_iam_policy_document.billing-roles.json}"
  tags =  {
      Applicatio_ID = "${var.app_id}"
      Environment = "${var.environment}"
      Name = "billing-role"
      Owner = "Terraform"
  }
}


resource "aws_iam_policy" "billing_policy" {
  name  = "billing-policy"
  policy= "${file("${path.module}/policies/billing-role-policy.json")}"
}

resource "aws_iam_role_policy_attachment" "billing_attachment" {
  role  = aws_iam_role.billing_role.name
  policy_arn = aws_iam_policy.billing_policy.arn
}

我正在通过 GitLab-CI 运行 terraform 的各个阶段(初始化、计划、应用)。这第一次有效,但因 EntityAlreadyExists 错误而失败。 .gitlab-ci.yml 看起来像这样:-

include:
  - project: 'infrastructure/infrastructure-code-cicd-files'
    ref: master
    file: '.for_terraform_iam.yml'

stages:
  - init
  - plan
  - apply

tf_init:
  extends: .tf_init
  tags:
    - integration
  stage: init
  variables:
    ACCOUNT: "ACCOUNT_ID"
    ASSUME_ROLE: "arn:aws:iam::ACCOUNT_ID:role/devops-cross-account"
    backend_bucket_name: "iam-role-backend-${ACCOUNT}"
    tfstate_file: "iam-role/terraform.tfstate"

tf_plan:
  extends: .tf_plan
  variables:
    ASSUME_ROLE: "arn:aws:iam::ACCOUNT_ID:role/devops-cross-account"
  tags:
    - integration
  stage: plan


tf_apply:
  extends: .tf_apply
  variables:
    ASSUME_ROLE: "arn:aws:iam::ACCOUNT_ID:role/devops-cross-account"
  tags:
    - integration
  stage: apply

这个 gitlab-ci 配置包括一个实用文件,其中包含 Init、Plan 和 Apply 的所有 terraform 逻辑。

我正在 Terraform 0.12.13 上运行设置。 Terraform 导入虽然成功导入资源,但在这里没有帮助,因为 terraform 抱怨“EntityAlreadyExists” 由于我在这里使用的 terraform 版本中的错误,Terraform 污点不起作用。

我想要一个工作流,其中 IAM 角色一旦创建,其附加的内联策略可以由 Ops 工程师更新,并且批准者将批准合并请求,这样 IAM 角色将根据 Ops 工程师的需要添加服务。

我们是否可以在此处更新 IAM 政策。我了解更新 IAM 角色需要先分离策略,然后将新策略附加到该角色。

请帮忙

【问题讨论】:

  • 你为什么要一直重新创建角色billing-role
  • billing_role 就是一个例子。还有其他角色需要更新,例如,我们为角色提供了允许“ec2:createinstances”的策略,但将来我们希望使用允许“ec2:deleteinstances”的策略更新此角色。这可能吗?
  • 你的 TF 状态文件保存在哪里?我猜它是远程的?
  • 是的,它保存在 s3 后端。谢谢

标签: amazon-web-services terraform gitlab-ci terraform-provider-aws terraform0.12+


【解决方案1】:

问题在于将 terraform.tfstate 文件传递​​到我错过的计划阶段。我们运行“aws s3 cp s3://backend-bucket/keys”。获取状态文件,这已经解决了问题。

【讨论】:

    猜你喜欢
    • 2016-11-16
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多