簡體   English   中英

在 GitLab-CI 中使用 terraform 更新 aws_iam_policy 失敗並出現 EntityAlreadyExists 錯誤

[英]update aws_iam_policy with terraform in GitLab-CI fails with EntityAlreadyExists error

我正在嘗試更新 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 import though successful in importing the resources does not help here as terraform complains about "EntityAlreadyExists" Terraform taint does not work dues to a bug in the terraform version that I am using here.

我想要一個工作流,其中 IAM 角色一旦創建,其附加的內聯策略可以由 Ops 工程師更新,並且批准者將批准合並請求,這樣 IAM 角色將根據 Ops 工程師的需要添加服務。

有沒有辦法我們可以在這里更新 IAM 政策。 我了解更新 IAM 角色需要先分離策略,然后將新策略附加到該角色。

請幫忙

問題在於將 terraform.tfstate 文件傳遞到我錯過的計划階段。 我們運行“aws s3 cp s3://backend-bucket/keys”。 獲取狀態文件,這已經解決了問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM